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