-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Import old chats
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using ChatUiT2.Models; | ||
using Microsoft.JSInterop; | ||
using System.Text.Json; | ||
|
||
|
||
namespace ChatUiT2.Services; | ||
|
||
public class LocalStorageService | ||
{ | ||
private readonly IJSRuntime JSRuntime; | ||
public LocalStorageService(IJSRuntime jsRuntime) | ||
{ | ||
JSRuntime = jsRuntime; | ||
} | ||
|
||
public async Task<string> GetItemAsync(string key) | ||
{ | ||
return await JSRuntime.InvokeAsync<string>("localStorage.getItem", key); | ||
} | ||
|
||
public async Task<List<Conversation>> GetConversationHistoryAsync() | ||
{ | ||
string json = await GetItemAsync("conversationHistory"); | ||
if (string.IsNullOrEmpty(json)) | ||
{ | ||
return null; | ||
} | ||
return JsonSerializer.Deserialize<List<Conversation>>(json); | ||
} | ||
|
||
public async Task<List<WorkItemChat>> GetLocalConversations() | ||
{ | ||
List<WorkItemChat> conversationHistory = new List<WorkItemChat>(); | ||
Console.WriteLine("Getting history"); | ||
var history = await GetConversationHistoryAsync(); | ||
if (history == null) | ||
{ | ||
Console.WriteLine("No history found"); | ||
return conversationHistory; | ||
} | ||
Console.WriteLine("Got history"); | ||
foreach (var conversation in history) | ||
{ | ||
Console.WriteLine($"Conversation: {conversation.name}"); | ||
|
||
if (conversation.messages.Count == 0) | ||
{ | ||
continue; | ||
} | ||
|
||
var settings = new ChatSettings { MaxTokens = 4096, Model = "gpt-4o-mini", Prompt = conversation.prompt, Temperature = (float)conversation.temperature }; | ||
var messages = new List<ChatMessage>(); | ||
|
||
messages = conversation.messages.Select(m => new ChatMessage | ||
{ | ||
Role = m.role == "user" ? ChatMessageRole.User : ChatMessageRole.Assistant, | ||
Content = m.content, | ||
Status = ChatMessageStatus.Done | ||
}).ToList(); | ||
|
||
var newConversation = new WorkItemChat | ||
{ | ||
Name = conversation.name, | ||
Settings = settings, | ||
Messages = messages, | ||
Persistant = false | ||
}; | ||
|
||
conversationHistory.Add(newConversation); | ||
} | ||
|
||
|
||
return conversationHistory; | ||
} | ||
} | ||
|
||
public class Conversation | ||
{ | ||
public string id { get; set; } // Matches "id" | ||
Check warning on line 79 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
public string name { get; set; } // Matches "name" | ||
Check warning on line 80 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
public List<Message> messages { get; set; } // Matches "messages" | ||
Check warning on line 81 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
public OldModel model { get; set; } // Matches "model" | ||
Check warning on line 82 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
public string prompt { get; set; } // Matches "prompt" | ||
Check warning on line 83 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
public double temperature { get; set; } // Matches "temperature" | ||
public string folderId { get; set; } // Matches "folderId" | ||
Check warning on line 85 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
} | ||
public class Message | ||
{ | ||
public string role { get; set; } // Matches "role" | ||
Check warning on line 89 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
public string content { get; set; } // Matches "content" | ||
Check warning on line 90 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
} | ||
public class OldModel | ||
{ | ||
public string id { get; set; } // Matches "id" | ||
Check warning on line 94 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
public string name { get; set; } // Matches "name" | ||
Check warning on line 95 in ChatUiT2/Services/LocalStorageService.cs GitHub Actions / build
|
||
public int maxLength { get; set; } // Matches "maxLength" | ||
public int tokenLimit { get; set; } // Matches "tokenLimit" | ||
} |