From 4e7d19d8db5ee41843447cdb02fff14ec057b8be Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 1 Dec 2024 12:29:56 +0100 Subject: [PATCH] Refactored LLM provider related actions --- .../Dialogs/ProviderDialog.razor | 14 +-- .../Dialogs/ProviderDialog.razor.cs | 98 ------------------ .../Provider/LLMProvidersExtensions.cs | 99 +++++++++++++++++++ 3 files changed, 106 insertions(+), 105 deletions(-) diff --git a/app/MindWork AI Studio/Dialogs/ProviderDialog.razor b/app/MindWork AI Studio/Dialogs/ProviderDialog.razor index efd5513b..20fe7778 100644 --- a/app/MindWork AI Studio/Dialogs/ProviderDialog.razor +++ b/app/MindWork AI Studio/Dialogs/ProviderDialog.razor @@ -12,7 +12,7 @@ @provider } - Create account + Create account @* ReSharper disable once CSharpWarnings::CS8974 *@ @@ -20,7 +20,7 @@ T="string" @bind-Text="@this.dataAPIKey" Label="@this.APIKeyText" - Disabled="@(!this.NeedAPIKey)" + Disabled="@(!this.DataLLMProvider.IsAPIKeyNeeded(this.DataHost))" Class="mb-3" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.VpnKey" @@ -33,7 +33,7 @@ T="string" @bind-Text="@this.DataHostname" Label="Hostname" - Disabled="@(!this.NeedHostname)" + Disabled="@(!this.DataLLMProvider.IsHostnameNeeded())" Class="mb-3" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Dns" @@ -42,7 +42,7 @@ UserAttributes="@SPELLCHECK_ATTRIBUTES" /> - + @foreach (Host host in Enum.GetValues(typeof(Host))) { @host.Name() @@ -50,9 +50,9 @@ - @if (this.ProvideModelManually) + @if (this.DataLLMProvider.IsLLMModelProvidedManually()) { - Show available models + Show available models Load + Load @foreach (var model in this.availableModels) { diff --git a/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs b/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs index f100b79b..80634f6c 100644 --- a/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs +++ b/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs @@ -324,109 +324,11 @@ public partial class ProviderDialog : ComponentBase this.availableModels.AddRange(orderedModels); } - private bool CanLoadModels() - { - if (this.DataLLMProvider is LLMProviders.SELF_HOSTED) - { - switch (this.DataHost) - { - case Host.NONE: - return false; - - case Host.LLAMACPP: - return false; - - case Host.LM_STUDIO: - return true; - - case Host.OLLAMA: - return true; - - default: - return false; - } - } - - if(this.DataLLMProvider is LLMProviders.NONE) - return false; - - if(string.IsNullOrWhiteSpace(this.dataAPIKey)) - return false; - - return true; - } - - private bool ShowRegisterButton => this.DataLLMProvider switch - { - LLMProviders.OPEN_AI => true, - LLMProviders.MISTRAL => true, - LLMProviders.ANTHROPIC => true, - LLMProviders.GOOGLE => true, - - LLMProviders.GROQ => true, - LLMProviders.FIREWORKS => true, - - _ => false, - }; - - private bool NeedAPIKey => this.DataLLMProvider switch - { - LLMProviders.OPEN_AI => true, - LLMProviders.MISTRAL => true, - LLMProviders.ANTHROPIC => true, - LLMProviders.GOOGLE => true, - - LLMProviders.GROQ => true, - LLMProviders.FIREWORKS => true, - - LLMProviders.SELF_HOSTED => this.DataHost is Host.OLLAMA, - - _ => false, - }; - private string APIKeyText => this.DataLLMProvider switch { LLMProviders.SELF_HOSTED => "(Optional) API Key", _ => "API Key", }; - - private bool NeedHostname => this.DataLLMProvider switch - { - LLMProviders.SELF_HOSTED => true, - _ => false, - }; - - private bool NeedHost => this.DataLLMProvider switch - { - LLMProviders.SELF_HOSTED => true, - _ => false, - }; - - private bool ProvideModelManually => this.DataLLMProvider switch - { - LLMProviders.FIREWORKS => true, - _ => false, - }; - - private string GetModelOverviewURL() => this.DataLLMProvider switch - { - LLMProviders.FIREWORKS => "https://fireworks.ai/models?show=Serverless", - - _ => string.Empty, - }; - - private string GetProviderCreationURL() => this.DataLLMProvider switch - { - LLMProviders.OPEN_AI => "https://platform.openai.com/signup", - LLMProviders.MISTRAL => "https://console.mistral.ai/", - LLMProviders.ANTHROPIC => "https://console.anthropic.com/dashboard", - LLMProviders.GOOGLE => "https://console.cloud.google.com/", - - LLMProviders.GROQ => "https://console.groq.com/", - LLMProviders.FIREWORKS => "https://fireworks.ai/login", - - _ => string.Empty, - }; private bool IsNoneProvider => this.DataLLMProvider is LLMProviders.NONE; } \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/LLMProvidersExtensions.cs b/app/MindWork AI Studio/Provider/LLMProvidersExtensions.cs index c2420cc3..725c16ad 100644 --- a/app/MindWork AI Studio/Provider/LLMProvidersExtensions.cs +++ b/app/MindWork AI Studio/Provider/LLMProvidersExtensions.cs @@ -7,6 +7,8 @@ using AIStudio.Provider.OpenAI; using AIStudio.Provider.SelfHosted; using AIStudio.Settings; +using Host = AIStudio.Provider.SelfHosted.Host; + namespace AIStudio.Provider; public static class LLMProvidersExtensions @@ -125,4 +127,101 @@ public static class LLMProvidersExtensions return new NoProvider(); } } + + public static string GetCreationURL(this LLMProviders provider) => provider switch + { + LLMProviders.OPEN_AI => "https://platform.openai.com/signup", + LLMProviders.MISTRAL => "https://console.mistral.ai/", + LLMProviders.ANTHROPIC => "https://console.anthropic.com/dashboard", + LLMProviders.GOOGLE => "https://console.cloud.google.com/", + + LLMProviders.GROQ => "https://console.groq.com/", + LLMProviders.FIREWORKS => "https://fireworks.ai/login", + + _ => string.Empty, + }; + + public static string GetModelsOverviewURL(this LLMProviders provider) => provider switch + { + LLMProviders.FIREWORKS => "https://fireworks.ai/models?show=Serverless", + _ => string.Empty, + }; + + public static bool IsLLMModelProvidedManually(this LLMProviders provider) => provider switch + { + LLMProviders.FIREWORKS => true, + _ => false, + }; + + public static bool IsEmbeddingModelProvidedManually(this LLMProviders provider) => provider switch + { + LLMProviders.SELF_HOSTED => true, + _ => false, + }; + + public static bool IsHostNeeded(this LLMProviders provider) => provider switch + { + LLMProviders.SELF_HOSTED => true, + _ => false, + }; + + public static bool IsHostnameNeeded(this LLMProviders provider) => provider switch + { + LLMProviders.SELF_HOSTED => true, + _ => false, + }; + + public static bool IsAPIKeyNeeded(this LLMProviders provider, Host host) => provider switch + { + LLMProviders.OPEN_AI => true, + LLMProviders.MISTRAL => true, + LLMProviders.ANTHROPIC => true, + LLMProviders.GOOGLE => true, + + LLMProviders.GROQ => true, + LLMProviders.FIREWORKS => true, + + LLMProviders.SELF_HOSTED => host is Host.OLLAMA, + + _ => false, + }; + + public static bool ShowRegisterButton(this LLMProviders provider) => provider switch + { + LLMProviders.OPEN_AI => true, + LLMProviders.MISTRAL => true, + LLMProviders.ANTHROPIC => true, + LLMProviders.GOOGLE => true, + + LLMProviders.GROQ => true, + LLMProviders.FIREWORKS => true, + + _ => false, + }; + + public static bool CanLoadModels(this LLMProviders provider, Host host, string? apiKey) + { + if (provider is LLMProviders.SELF_HOSTED) + { + switch (host) + { + case Host.NONE: + case Host.LLAMACPP: + default: + return false; + + case Host.OLLAMA: + case Host.LM_STUDIO: + return true; + } + } + + if(provider is LLMProviders.NONE) + return false; + + if(string.IsNullOrWhiteSpace(apiKey)) + return false; + + return true; + } } \ No newline at end of file