From 9e4ad9cce7bac9897f629e90a46792e9fd1fba26 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 15 Jul 2024 16:29:26 +0200 Subject: [PATCH] Refactored settings for self-hosted providers to configure a host --- .../Components/AssistantBase.razor.cs | 4 +- .../Components/Pages/Chat.razor | 2 +- .../Components/Pages/Chat.razor.cs | 8 +-- .../IconFinder/AssistantIconFinder.razor | 2 +- .../Components/Pages/Settings.razor.cs | 3 +- .../AssistantTextSummarizer.razor | 2 +- .../Translator/AssistantTranslator.razor | 2 +- app/MindWork AI Studio/Provider/Providers.cs | 14 ++-- .../Provider/SelfHosted/Host.cs | 42 +++++++++++ .../Provider/SelfHosted/ProviderSelfHosted.cs | 4 +- app/MindWork AI Studio/Settings/Data.cs | 2 +- app/MindWork AI Studio/Settings/Provider.cs | 14 +++- .../Settings/ProviderDialog.razor | 8 +++ .../Settings/ProviderDialog.razor.cs | 72 ++++++++++++++----- .../Settings/SettingsMigrations.cs | 39 +++++++++- app/MindWork AI Studio/Settings/Version.cs | 1 + 16 files changed, 175 insertions(+), 44 deletions(-) create mode 100644 app/MindWork AI Studio/Provider/SelfHosted/Host.cs diff --git a/app/MindWork AI Studio/Components/AssistantBase.razor.cs b/app/MindWork AI Studio/Components/AssistantBase.razor.cs index dd99e08a..aa1fcf05 100644 --- a/app/MindWork AI Studio/Components/AssistantBase.razor.cs +++ b/app/MindWork AI Studio/Components/AssistantBase.razor.cs @@ -25,7 +25,7 @@ public abstract partial class AssistantBase : ComponentBase private protected virtual RenderFragment? Body => null; - protected AIStudio.Settings.Provider selectedProvider; + protected AIStudio.Settings.Provider providerSettings; protected MudForm? form; protected bool inputIsValid; @@ -99,6 +99,6 @@ public abstract partial class AssistantBase : ComponentBase // Use the selected provider to get the AI response. // By awaiting this line, we wait for the entire // content to be streamed. - await aiText.CreateFromProviderAsync(this.selectedProvider.UsedProvider.CreateProvider(this.selectedProvider.InstanceName, this.selectedProvider.Hostname), this.JsRuntime, this.SettingsManager, this.selectedProvider.Model, this.chatThread); + await aiText.CreateFromProviderAsync(this.providerSettings.CreateProvider(), this.JsRuntime, this.SettingsManager, this.providerSettings.Model, this.chatThread); } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Pages/Chat.razor b/app/MindWork AI Studio/Components/Pages/Chat.razor index edd85516..d56106a5 100644 --- a/app/MindWork AI Studio/Components/Pages/Chat.razor +++ b/app/MindWork AI Studio/Components/Pages/Chat.razor @@ -15,7 +15,7 @@ } - + @foreach (var provider in this.SettingsManager.ConfigurationData.Providers) { diff --git a/app/MindWork AI Studio/Components/Pages/Chat.razor.cs b/app/MindWork AI Studio/Components/Pages/Chat.razor.cs index 67076370..09dd7083 100644 --- a/app/MindWork AI Studio/Components/Pages/Chat.razor.cs +++ b/app/MindWork AI Studio/Components/Pages/Chat.razor.cs @@ -32,7 +32,7 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable private const Placement TOOLBAR_TOOLTIP_PLACEMENT = Placement.Bottom; private static readonly Dictionary USER_INPUT_ATTRIBUTES = new(); - private AIStudio.Settings.Provider selectedProvider; + private AIStudio.Settings.Provider providerSettings; private ChatThread? chatThread; private bool hasUnsavedChanges; private bool isStreaming; @@ -61,11 +61,11 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable #endregion - private bool IsProviderSelected => this.selectedProvider.UsedProvider != Providers.NONE; + private bool IsProviderSelected => this.providerSettings.UsedProvider != Providers.NONE; private string ProviderPlaceholder => this.IsProviderSelected ? "Type your input here..." : "Select a provider first"; - private string InputLabel => this.IsProviderSelected ? $"Your Prompt (use selected instance '{this.selectedProvider.InstanceName}', provider '{this.selectedProvider.UsedProvider.ToName()}')" : "Select a provider first"; + private string InputLabel => this.IsProviderSelected ? $"Your Prompt (use selected instance '{this.providerSettings.InstanceName}', provider '{this.providerSettings.UsedProvider.ToName()}')" : "Select a provider first"; private bool CanThreadBeSaved => this.chatThread is not null && this.chatThread.Blocks.Count > 0; @@ -151,7 +151,7 @@ public partial class Chat : MSGComponentBase, IAsyncDisposable // Use the selected provider to get the AI response. // By awaiting this line, we wait for the entire // content to be streamed. - await aiText.CreateFromProviderAsync(this.selectedProvider.UsedProvider.CreateProvider(this.selectedProvider.InstanceName, this.selectedProvider.Hostname), this.JsRuntime, this.SettingsManager, this.selectedProvider.Model, this.chatThread); + await aiText.CreateFromProviderAsync(this.providerSettings.CreateProvider(), this.JsRuntime, this.SettingsManager, this.providerSettings.Model, this.chatThread); // Save the chat: if (this.SettingsManager.ConfigurationData.WorkspaceStorageBehavior is WorkspaceStorageBehavior.STORE_CHATS_AUTOMATICALLY) diff --git a/app/MindWork AI Studio/Components/Pages/IconFinder/AssistantIconFinder.razor b/app/MindWork AI Studio/Components/Pages/IconFinder/AssistantIconFinder.razor index fb042b98..d50d231e 100644 --- a/app/MindWork AI Studio/Components/Pages/IconFinder/AssistantIconFinder.razor +++ b/app/MindWork AI Studio/Components/Pages/IconFinder/AssistantIconFinder.razor @@ -17,7 +17,7 @@ } - + @foreach (var provider in this.SettingsManager.ConfigurationData.Providers) { diff --git a/app/MindWork AI Studio/Components/Pages/Settings.razor.cs b/app/MindWork AI Studio/Components/Pages/Settings.razor.cs index e6df7480..23c14d31 100644 --- a/app/MindWork AI Studio/Components/Pages/Settings.razor.cs +++ b/app/MindWork AI Studio/Components/Pages/Settings.razor.cs @@ -53,6 +53,7 @@ public partial class Settings : ComponentBase { x => x.DataHostname, provider.Hostname }, { x => x.IsSelfHosted, provider.IsSelfHosted }, { x => x.IsEditing, true }, + { x => x.DataHost, provider.Host }, }; var dialogReference = await this.DialogService.ShowAsync("Edit Provider", dialogParameters, DialogOptions.FULLSCREEN); @@ -83,7 +84,7 @@ public partial class Settings : ComponentBase if (dialogResult.Canceled) return; - var providerInstance = provider.UsedProvider.CreateProvider(provider.InstanceName, provider.Hostname); + var providerInstance = provider.CreateProvider(); var deleteSecretResponse = await this.SettingsManager.DeleteAPIKey(this.JsRuntime, providerInstance); if(deleteSecretResponse.Success) { diff --git a/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor b/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor index 56da6e60..f67ac4cc 100644 --- a/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor +++ b/app/MindWork AI Studio/Components/Pages/TextSummarizer/AssistantTextSummarizer.razor @@ -31,7 +31,7 @@ } - + @foreach (var provider in this.SettingsManager.ConfigurationData.Providers) { diff --git a/app/MindWork AI Studio/Components/Pages/Translator/AssistantTranslator.razor b/app/MindWork AI Studio/Components/Pages/Translator/AssistantTranslator.razor index 80004e94..09dc855f 100644 --- a/app/MindWork AI Studio/Components/Pages/Translator/AssistantTranslator.razor +++ b/app/MindWork AI Studio/Components/Pages/Translator/AssistantTranslator.razor @@ -38,7 +38,7 @@ else } - + @foreach (var provider in this.SettingsManager.ConfigurationData.Providers) { diff --git a/app/MindWork AI Studio/Provider/Providers.cs b/app/MindWork AI Studio/Provider/Providers.cs index db99cc3e..3f0d88e3 100644 --- a/app/MindWork AI Studio/Provider/Providers.cs +++ b/app/MindWork AI Studio/Provider/Providers.cs @@ -45,17 +45,15 @@ public static class ExtensionsProvider /// /// Creates a new provider instance based on the provider value. /// - /// The provider value. - /// The used instance name. - /// The hostname of the provider. + /// The provider settings. /// The provider instance. - public static IProvider CreateProvider(this Providers provider, string instanceName, string hostname = "http://localhost:1234") => provider switch + public static IProvider CreateProvider(this Settings.Provider providerSettings) => providerSettings.UsedProvider switch { - Providers.OPEN_AI => new ProviderOpenAI { InstanceName = instanceName }, - Providers.ANTHROPIC => new ProviderAnthropic { InstanceName = instanceName }, - Providers.MISTRAL => new ProviderMistral { InstanceName = instanceName }, + Providers.OPEN_AI => new ProviderOpenAI { InstanceName = providerSettings.InstanceName }, + Providers.ANTHROPIC => new ProviderAnthropic { InstanceName = providerSettings.InstanceName }, + Providers.MISTRAL => new ProviderMistral { InstanceName = providerSettings.InstanceName }, - Providers.SELF_HOSTED => new ProviderSelfHosted(hostname) { InstanceName = instanceName }, + Providers.SELF_HOSTED => new ProviderSelfHosted(providerSettings) { InstanceName = providerSettings.InstanceName }, _ => new NoProvider(), }; diff --git a/app/MindWork AI Studio/Provider/SelfHosted/Host.cs b/app/MindWork AI Studio/Provider/SelfHosted/Host.cs new file mode 100644 index 00000000..b3de3863 --- /dev/null +++ b/app/MindWork AI Studio/Provider/SelfHosted/Host.cs @@ -0,0 +1,42 @@ +namespace AIStudio.Provider.SelfHosted; + +public enum Host +{ + NONE, + + LM_STUDIO, + LLAMACPP, + OLLAMA, +} + +public static class HostExtensions +{ + public static string Name(this Host host) => host switch + { + Host.NONE => "None", + + Host.LM_STUDIO => "LM Studio", + Host.LLAMACPP => "llama.cpp", + Host.OLLAMA => "ollama", + + _ => "Unknown", + }; + + public static string BaseURL(this Host host) => host switch + { + Host.LM_STUDIO => "/v1/", + Host.LLAMACPP => "/v1/", + Host.OLLAMA => "/api/", + + _ => "/v1/", + }; + + public static string ChatURL(this Host host) => host switch + { + Host.LM_STUDIO => "chat/completions", + Host.LLAMACPP => "chat/completions", + Host.OLLAMA => "chat", + + _ => "chat/completions", + }; +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/SelfHosted/ProviderSelfHosted.cs b/app/MindWork AI Studio/Provider/SelfHosted/ProviderSelfHosted.cs index 4093224c..63fb1051 100644 --- a/app/MindWork AI Studio/Provider/SelfHosted/ProviderSelfHosted.cs +++ b/app/MindWork AI Studio/Provider/SelfHosted/ProviderSelfHosted.cs @@ -8,7 +8,7 @@ using AIStudio.Settings; namespace AIStudio.Provider.SelfHosted; -public sealed class ProviderSelfHosted(string hostname) : BaseProvider($"{hostname}/v1/"), IProvider +public sealed class ProviderSelfHosted(Settings.Provider provider) : BaseProvider($"{provider.Hostname}{provider.Host.BaseURL()}"), IProvider { private static readonly JsonSerializerOptions JSON_SERIALIZER_OPTIONS = new() { @@ -62,7 +62,7 @@ public sealed class ProviderSelfHosted(string hostname) : BaseProvider($"{hostna }, JSON_SERIALIZER_OPTIONS); // Build the HTTP post request: - var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions"); + var request = new HttpRequestMessage(HttpMethod.Post, provider.Host.ChatURL()); // Set the content: request.Content = new StringContent(providerChatRequest, Encoding.UTF8, "application/json"); diff --git a/app/MindWork AI Studio/Settings/Data.cs b/app/MindWork AI Studio/Settings/Data.cs index e4efb078..ea774876 100644 --- a/app/MindWork AI Studio/Settings/Data.cs +++ b/app/MindWork AI Studio/Settings/Data.cs @@ -9,7 +9,7 @@ public sealed class Data /// The version of the settings file. Allows us to upgrade the settings /// when a new version is available. /// - public Version Version { get; init; } = Version.V2; + public Version Version { get; init; } = Version.V3; /// /// List of configured providers. diff --git a/app/MindWork AI Studio/Settings/Provider.cs b/app/MindWork AI Studio/Settings/Provider.cs index 48133267..a59eb9bd 100644 --- a/app/MindWork AI Studio/Settings/Provider.cs +++ b/app/MindWork AI Studio/Settings/Provider.cs @@ -1,5 +1,7 @@ using AIStudio.Provider; +using Host = AIStudio.Provider.SelfHosted.Host; + namespace AIStudio.Settings; /// @@ -12,7 +14,15 @@ namespace AIStudio.Settings; /// Whether the provider is self-hosted. /// The hostname of the provider. Useful for self-hosted providers. /// The LLM model to use for chat. -public readonly record struct Provider(uint Num, string Id, string InstanceName, Providers UsedProvider, Model Model, bool IsSelfHosted = false, string Hostname = "http://localhost:1234") +public readonly record struct Provider( + uint Num, + string Id, + string InstanceName, + Providers UsedProvider, + Model Model, + bool IsSelfHosted = false, + string Hostname = "http://localhost:1234", + Host Host = Host.NONE) { #region Overrides of ValueType @@ -24,7 +34,7 @@ public readonly record struct Provider(uint Num, string Id, string InstanceName, public override string ToString() { if(this.IsSelfHosted) - return $"{this.InstanceName} ({this.UsedProvider.ToName()}, {this.Hostname}, {this.Model})"; + return $"{this.InstanceName} ({this.UsedProvider.ToName()}, {this.Host}, {this.Hostname}, {this.Model})"; return $"{this.InstanceName} ({this.UsedProvider.ToName()}, {this.Model})"; } diff --git a/app/MindWork AI Studio/Settings/ProviderDialog.razor b/app/MindWork AI Studio/Settings/ProviderDialog.razor index 82bb5fc1..2a7d32c3 100644 --- a/app/MindWork AI Studio/Settings/ProviderDialog.razor +++ b/app/MindWork AI Studio/Settings/ProviderDialog.razor @@ -1,4 +1,5 @@ @using AIStudio.Provider +@using AIStudio.Provider.SelfHosted @using MudBlazor @@ -41,6 +42,13 @@ Validation="@this.ValidatingHostname" /> + + @foreach (Host host in Enum.GetValues(typeof(Host))) + { + @host.Name() + } + + Load diff --git a/app/MindWork AI Studio/Settings/ProviderDialog.razor.cs b/app/MindWork AI Studio/Settings/ProviderDialog.razor.cs index 3febabcb..5c4d7f47 100644 --- a/app/MindWork AI Studio/Settings/ProviderDialog.razor.cs +++ b/app/MindWork AI Studio/Settings/ProviderDialog.razor.cs @@ -4,6 +4,8 @@ using AIStudio.Provider; using Microsoft.AspNetCore.Components; +using Host = AIStudio.Provider.SelfHosted.Host; + namespace AIStudio.Settings; /// @@ -38,6 +40,12 @@ public partial class ProviderDialog : ComponentBase [Parameter] public string DataHostname { get; set; } = string.Empty; + /// + /// The local host to use, e.g., llama.cpp. + /// + [Parameter] + public Host DataHost { get; set; } = Host.NONE; + /// /// Is this provider self-hosted? /// @@ -85,6 +93,18 @@ public partial class ProviderDialog : ComponentBase private MudForm form = null!; private readonly List availableModels = new(); + + private Provider CreateProviderSettings() => new() + { + Num = this.DataNum, + Id = this.DataId, + InstanceName = this.DataInstanceName, + UsedProvider = this.DataProvider, + Model = this.DataModel, + IsSelfHosted = this.DataProvider is Providers.SELF_HOSTED, + Hostname = this.DataHostname, + Host = this.DataHost, + }; #region Overrides of ComponentBase @@ -100,9 +120,23 @@ public partial class ProviderDialog : ComponentBase if(this.IsEditing) { this.dataEditingPreviousInstanceName = this.DataInstanceName.ToLowerInvariant(); - var provider = this.DataProvider.CreateProvider(this.DataInstanceName); - if(provider is NoProvider) + + // + // We cannot load the API key nor models for self-hosted providers: + // + if (this.DataProvider is Providers.SELF_HOSTED) + { + await base.OnInitializedAsync(); return; + } + + var loadedProviderSettings = this.CreateProviderSettings(); + var provider = loadedProviderSettings.CreateProvider(); + if(provider is NoProvider) + { + await base.OnInitializedAsync(); + return; + } // Load the API key: var requestedSecret = await this.SettingsManager.GetAPIKey(this.JsRuntime, provider); @@ -111,8 +145,7 @@ public partial class ProviderDialog : ComponentBase this.dataAPIKey = requestedSecret.Secret; // Now, we try to load the list of available models: - if(this.DataProvider is not Providers.SELF_HOSTED) - await this.ReloadModels(); + await this.ReloadModels(); } else { @@ -148,19 +181,10 @@ public partial class ProviderDialog : ComponentBase // Use the data model to store the provider. // We just return this data to the parent component: - var addedProvider = new Provider - { - Num = this.DataNum, - Id = this.DataId, - InstanceName = this.DataInstanceName, - UsedProvider = this.DataProvider, - Model = this.DataModel, - IsSelfHosted = this.DataProvider is Providers.SELF_HOSTED, - Hostname = this.DataHostname, - }; + var addedProviderSettings = this.CreateProviderSettings(); // We need to instantiate the provider to store the API key: - var provider = this.DataProvider.CreateProvider(this.DataInstanceName); + var provider = addedProviderSettings.CreateProvider(); // Store the API key in the OS secure storage: var storeResponse = await this.SettingsManager.SetAPIKey(this.JsRuntime, provider, this.dataAPIKey); @@ -171,7 +195,7 @@ public partial class ProviderDialog : ComponentBase return; } - this.MudDialog.Close(DialogResult.Ok(addedProvider)); + this.MudDialog.Close(DialogResult.Ok(addedProviderSettings)); } private string? ValidatingProvider(Providers provider) @@ -182,6 +206,17 @@ public partial class ProviderDialog : ComponentBase return null; } + private string? ValidatingHost(Host host) + { + if(this.DataProvider is not Providers.SELF_HOSTED) + return null; + + if (host == Host.NONE) + return "Please select a host."; + + return null; + } + private string? ValidatingModel(Model model) { if(this.DataProvider is Providers.SELF_HOSTED) @@ -196,7 +231,7 @@ public partial class ProviderDialog : ComponentBase [GeneratedRegex(@"^[a-zA-Z0-9\-_. ]+$")] private static partial Regex InstanceNameRegex(); - private static readonly string[] RESERVED_NAMES = { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" }; + private static readonly string[] RESERVED_NAMES = ["CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"]; private string? ValidatingInstanceName(string instanceName) { @@ -270,7 +305,8 @@ public partial class ProviderDialog : ComponentBase private async Task ReloadModels() { - var provider = this.DataProvider.CreateProvider("temp"); + var currentProviderSettings = this.CreateProviderSettings(); + var provider = currentProviderSettings.CreateProvider(); if(provider is NoProvider) return; diff --git a/app/MindWork AI Studio/Settings/SettingsMigrations.cs b/app/MindWork AI Studio/Settings/SettingsMigrations.cs index e5783787..516a1c13 100644 --- a/app/MindWork AI Studio/Settings/SettingsMigrations.cs +++ b/app/MindWork AI Studio/Settings/SettingsMigrations.cs @@ -1,3 +1,5 @@ +using Host = AIStudio.Provider.SelfHosted.Host; + namespace AIStudio.Settings; public static class SettingsMigrations @@ -7,7 +9,11 @@ public static class SettingsMigrations switch (previousData.Version) { case Version.V1: - return MigrateFromV1(previousData); + previousData = MigrateV1ToV2(previousData); + return MigrateV2ToV3(previousData); + + case Version.V2: + return MigrateV2ToV3(previousData); default: Console.WriteLine("No migration needed."); @@ -15,7 +21,7 @@ public static class SettingsMigrations } } - private static Data MigrateFromV1(Data previousData) + private static Data MigrateV1ToV2(Data previousData) { // // Summary: @@ -36,4 +42,33 @@ public static class SettingsMigrations UpdateBehavior = previousData.UpdateBehavior, }; } + + private static Data MigrateV2ToV3(Data previousData) + { + // + // Summary: + // In v2, self-hosted providers had no host (LM Studio, llama.cpp, ollama, etc.) + // + + Console.WriteLine("Migrating from v2 to v3..."); + return new() + { + Version = Version.V3, + Providers = previousData.Providers.Select(provider => + { + if(provider.IsSelfHosted) + return provider with { Host = Host.LM_STUDIO }; + + return provider with { Host = Host.NONE }; + }).ToList(), + + EnableSpellchecking = previousData.EnableSpellchecking, + IsSavingEnergy = previousData.IsSavingEnergy, + NextProviderNum = previousData.NextProviderNum, + ShortcutSendBehavior = previousData.ShortcutSendBehavior, + UpdateBehavior = previousData.UpdateBehavior, + WorkspaceStorageBehavior = previousData.WorkspaceStorageBehavior, + WorkspaceStorageTemporaryMaintenancePolicy = previousData.WorkspaceStorageTemporaryMaintenancePolicy, + }; + } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Settings/Version.cs b/app/MindWork AI Studio/Settings/Version.cs index 04e54efd..cb30052d 100644 --- a/app/MindWork AI Studio/Settings/Version.cs +++ b/app/MindWork AI Studio/Settings/Version.cs @@ -10,4 +10,5 @@ public enum Version V1, V2, + V3, } \ No newline at end of file