mirror of
https://github.com/tuxdotrs/highminded.git
synced 2025-08-22 23:51:03 +05:30
feat: add hot reload settings
This commit is contained in:
@@ -14,22 +14,11 @@ namespace highminded.ui.controls;
|
||||
public partial class ChatUserControl : UserControl
|
||||
{
|
||||
|
||||
// OpenAI
|
||||
private readonly OpenAI.Chat.ChatClient _client = null!;
|
||||
private readonly MarkdownPipeline _pipeline = null!;
|
||||
|
||||
public ChatUserControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_client = new ChatClient(
|
||||
model: InMemoryDb.Obj.settingsManager.Settings.Model,
|
||||
credential: new ApiKeyCredential(InMemoryDb.Obj.settingsManager.Settings.ApiKey),
|
||||
options: new OpenAIClientOptions
|
||||
{
|
||||
Endpoint = new Uri(InMemoryDb.Obj.settingsManager.Settings.ApiURL)
|
||||
});
|
||||
|
||||
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build();
|
||||
}
|
||||
|
||||
@@ -44,7 +33,7 @@ public partial class ChatUserControl : UserControl
|
||||
PromptBox.Clear();
|
||||
|
||||
AsyncCollectionResult<StreamingChatCompletionUpdate> completionUpdates =
|
||||
_client.CompleteChatStreamingAsync(prompt);
|
||||
InMemoryDb.Obj.ChatClient.CompleteChatStreamingAsync(prompt);
|
||||
|
||||
var responseBuilder = new StringBuilder();
|
||||
|
||||
|
@@ -1,10 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using highminded.utils;
|
||||
|
||||
namespace highminded.ui.controls;
|
||||
@@ -15,16 +10,16 @@ public partial class SettingsUserControl : UserControl
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
ModelTextBox.Text = InMemoryDb.Obj.settingsManager.Settings.Model;
|
||||
ApiUrlTextBox.Text = InMemoryDb.Obj.settingsManager.Settings.ApiURL;
|
||||
ApiKeyTextBox.Text = InMemoryDb.Obj.settingsManager.Settings.ApiKey;
|
||||
ModelTextBox.Text = InMemoryDb.Obj.SettingsManager.Settings.Model;
|
||||
ApiUrlTextBox.Text = InMemoryDb.Obj.SettingsManager.Settings.ApiURL;
|
||||
ApiKeyTextBox.Text = InMemoryDb.Obj.SettingsManager.Settings.ApiKey;
|
||||
}
|
||||
|
||||
private void SaveSettingsBtn_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
InMemoryDb.Obj.settingsManager.Settings.Model = ModelTextBox.Text;
|
||||
InMemoryDb.Obj.settingsManager.Settings.ApiURL= ApiUrlTextBox.Text;
|
||||
InMemoryDb.Obj.settingsManager.Settings.ApiKey = ApiKeyTextBox.Text;
|
||||
InMemoryDb.Obj.settingsManager.Save();
|
||||
InMemoryDb.Obj.SaveSettings(new AppSettings()
|
||||
{
|
||||
ApiKey = ApiKeyTextBox.Text, ApiURL = ApiUrlTextBox.Text, Model = ModelTextBox.Text
|
||||
});
|
||||
}
|
||||
}
|
@@ -1,15 +1,45 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Path = Avalonia.Controls.Shapes.Path;
|
||||
using System.ClientModel;
|
||||
using OpenAI;
|
||||
using OpenAI.Chat;
|
||||
|
||||
namespace highminded.utils;
|
||||
|
||||
public class InMemoryDb
|
||||
{
|
||||
internal ChatClient ChatClient;
|
||||
internal SettingsManager<AppSettings> SettingsManager;
|
||||
|
||||
// Initialize Singleton Class
|
||||
InMemoryDb() { }
|
||||
private InMemoryDb()
|
||||
{
|
||||
SettingsManager = new SettingsManager<AppSettings>();
|
||||
if (SettingsManager.Settings.ApiKey != string.Empty)
|
||||
{
|
||||
InitOpenAIClient();
|
||||
}
|
||||
}
|
||||
|
||||
public void InitOpenAIClient()
|
||||
{
|
||||
ChatClient = new ChatClient(
|
||||
model: SettingsManager.Settings.Model,
|
||||
credential: new ApiKeyCredential(SettingsManager.Settings.ApiKey),
|
||||
options: new OpenAIClientOptions
|
||||
{
|
||||
Endpoint = new Uri(SettingsManager.Settings.ApiURL)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void SaveSettings(AppSettings settings)
|
||||
{
|
||||
SettingsManager.Settings.ApiKey = settings.ApiKey;
|
||||
SettingsManager.Settings.ApiURL = settings.ApiURL;
|
||||
SettingsManager.Settings.Model = settings.Model;
|
||||
SettingsManager.Save();
|
||||
InitOpenAIClient();
|
||||
}
|
||||
|
||||
public static readonly InMemoryDb Obj = new InMemoryDb();
|
||||
|
||||
public SettingsManager<AppSettings> settingsManager = new SettingsManager<AppSettings>();
|
||||
}
|
@@ -19,10 +19,7 @@ public class SettingsManager<T> where T : class, new()
|
||||
|
||||
public SettingsManager(string appName = "highminded")
|
||||
{
|
||||
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
||||
var appFolder = Path.Combine(appData, appName);
|
||||
Directory.CreateDirectory(appFolder);
|
||||
_settingsPath = Path.Combine(appFolder, "settings.json");
|
||||
_settingsPath = Path.Combine(Environment.CurrentDirectory, "settings.json");
|
||||
Settings = Load();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user