diff --git a/app/MindWork AI Studio/Components/Pages/Chat.razor.cs b/app/MindWork AI Studio/Components/Pages/Chat.razor.cs index e4acea8..5b5b4c3 100644 --- a/app/MindWork AI Studio/Components/Pages/Chat.razor.cs +++ b/app/MindWork AI Studio/Components/Pages/Chat.razor.cs @@ -102,7 +102,7 @@ public partial class Chat : 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.JsRuntime, this.SettingsManager, new Model("gpt-4o"), this.chatThread); + await aiText.CreateFromProviderAsync(this.selectedProvider.UsedProvider.CreateProvider(this.selectedProvider.InstanceName), this.JsRuntime, this.SettingsManager, this.selectedProvider.Model, this.chatThread); // Disable the stream state: this.isStreaming = false; diff --git a/app/MindWork AI Studio/Components/Pages/Settings.razor b/app/MindWork AI Studio/Components/Pages/Settings.razor index a450312..2820b10 100644 --- a/app/MindWork AI Studio/Components/Pages/Settings.razor +++ b/app/MindWork AI Studio/Components/Pages/Settings.razor @@ -7,20 +7,23 @@ - + + # + Instance Name Provider - Name + Model Actions - - @context.UsedProvider + @context.Num @context.InstanceName + @context.UsedProvider + @context.Model Edit diff --git a/app/MindWork AI Studio/Components/Pages/Settings.razor.cs b/app/MindWork AI Studio/Components/Pages/Settings.razor.cs index 94cc597..b287877 100644 --- a/app/MindWork AI Studio/Components/Pages/Settings.razor.cs +++ b/app/MindWork AI Studio/Components/Pages/Settings.razor.cs @@ -52,6 +52,8 @@ public partial class Settings : ComponentBase return; var addedProvider = (AIStudio.Settings.Provider)dialogResult.Data; + addedProvider = addedProvider with { Num = this.SettingsManager.ConfigurationData.NextProviderNum++ }; + this.SettingsManager.ConfigurationData.Providers.Add(addedProvider); await this.SettingsManager.StoreSettings(); } @@ -60,9 +62,11 @@ public partial class Settings : ComponentBase { var dialogParameters = new DialogParameters { + { x => x.DataNum, provider.Num }, { x => x.DataId, provider.Id }, { x => x.DataInstanceName, provider.InstanceName }, { x => x.DataProvider, provider.UsedProvider }, + { x => x.DataModel, provider.Model }, { x => x.IsEditing, true }, }; @@ -72,6 +76,12 @@ public partial class Settings : ComponentBase return; var editedProvider = (AIStudio.Settings.Provider)dialogResult.Data; + + // Set the provider number if it's not set. This is important for providers + // added before we started saving the provider number. + if(editedProvider.Num == 0) + editedProvider = editedProvider with { Num = this.SettingsManager.ConfigurationData.NextProviderNum++ }; + this.SettingsManager.ConfigurationData.Providers[this.SettingsManager.ConfigurationData.Providers.IndexOf(provider)] = editedProvider; await this.SettingsManager.StoreSettings(); } @@ -88,9 +98,7 @@ public partial class Settings : ComponentBase if (dialogResult.Canceled) return; - var providerInstance = provider.UsedProvider.CreateProvider(); - providerInstance.InstanceName = provider.InstanceName; - + var providerInstance = provider.UsedProvider.CreateProvider(provider.InstanceName); var deleteSecretResponse = await this.SettingsManager.DeleteAPIKey(this.JsRuntime, providerInstance); if(deleteSecretResponse.Success) { diff --git a/app/MindWork AI Studio/MindWork AI Studio.csproj b/app/MindWork AI Studio/MindWork AI Studio.csproj index 4dfe22b..aa0d895 100644 --- a/app/MindWork AI Studio/MindWork AI Studio.csproj +++ b/app/MindWork AI Studio/MindWork AI Studio.csproj @@ -20,12 +20,18 @@ false true true - IL2026 + + + IL2026, CS8974 + diff --git a/app/MindWork AI Studio/Program.cs b/app/MindWork AI Studio/Program.cs index a64921c..72f6f22 100644 --- a/app/MindWork AI Studio/Program.cs +++ b/app/MindWork AI Studio/Program.cs @@ -1,15 +1,16 @@ -using System.Reflection; - using AIStudio; using AIStudio.Components; using AIStudio.Settings; using AIStudio.Tools; -using Microsoft.Extensions.FileProviders; - using MudBlazor; using MudBlazor.Services; +#if !DEBUG +using System.Reflection; +using Microsoft.Extensions.FileProviders; +#endif + var builder = WebApplication.CreateBuilder(); builder.Services.AddMudServices(config => { diff --git a/app/MindWork AI Studio/Provider/IProvider.cs b/app/MindWork AI Studio/Provider/IProvider.cs index 15403bf..7c5baeb 100644 --- a/app/MindWork AI Studio/Provider/IProvider.cs +++ b/app/MindWork AI Studio/Provider/IProvider.cs @@ -53,7 +53,7 @@ public interface IProvider /// The settings manager to access the API key. /// The cancellation token. /// The list of text models. - public Task> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default); + public Task> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default); /// /// Load all possible image models that can be used with this provider. @@ -62,5 +62,5 @@ public interface IProvider /// The settings manager to access the API key. /// The cancellation token. /// The list of image models. - public Task> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default); + public Task> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default); } \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/ImageURL.cs b/app/MindWork AI Studio/Provider/ImageURL.cs index 8d86e3b..2faef58 100644 --- a/app/MindWork AI Studio/Provider/ImageURL.cs +++ b/app/MindWork AI Studio/Provider/ImageURL.cs @@ -3,5 +3,5 @@ namespace AIStudio.Provider; /// /// An image URL. /// -/// The image URL. -public readonly record struct ImageURL(string url); \ No newline at end of file +/// The image URL. +public readonly record struct ImageURL(string URL); \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Model.cs b/app/MindWork AI Studio/Provider/Model.cs index d8f8d05..af39709 100644 --- a/app/MindWork AI Studio/Provider/Model.cs +++ b/app/MindWork AI Studio/Provider/Model.cs @@ -4,4 +4,11 @@ namespace AIStudio.Provider; /// The data model for the model to use. /// /// The model's ID. -public readonly record struct Model(string Id); \ No newline at end of file +public readonly record struct Model(string Id) +{ + #region Overrides of ValueType + + public override string ToString() => string.IsNullOrWhiteSpace(this.Id) ? "no model selected" : this.Id; + + #endregion +} \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/NoProvider.cs b/app/MindWork AI Studio/Provider/NoProvider.cs index e505395..d65ea57 100644 --- a/app/MindWork AI Studio/Provider/NoProvider.cs +++ b/app/MindWork AI Studio/Provider/NoProvider.cs @@ -17,9 +17,9 @@ public class NoProvider : IProvider public string InstanceName { get; set; } = "None"; - public Task> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) => Task.FromResult>(new List()); + public Task> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) => Task.FromResult>([]); - public Task> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) => Task.FromResult>(new List()); + public Task> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) => Task.FromResult>([]); public async IAsyncEnumerable StreamChatCompletion(IJSRuntime jsRuntime, SettingsManager settings, Model chatModel, ChatThread chatChatThread, [EnumeratorCancellation] CancellationToken token = default) { diff --git a/app/MindWork AI Studio/Provider/OpenAI/ProviderOpenAI.cs b/app/MindWork AI Studio/Provider/OpenAI/ProviderOpenAI.cs index ed5abfe..8abb5f3 100644 --- a/app/MindWork AI Studio/Provider/OpenAI/ProviderOpenAI.cs +++ b/app/MindWork AI Studio/Provider/OpenAI/ProviderOpenAI.cs @@ -155,34 +155,33 @@ public sealed class ProviderOpenAI() : BaseProvider("https://api.openai.com/v1/" #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously /// - public async Task> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) + public Task> GetTextModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) { - return await this.LoadModels(jsRuntime, settings, "gpt-", token); + return this.LoadModels(jsRuntime, settings, "gpt-", token); } /// - public async Task> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) + public Task> GetImageModels(IJSRuntime jsRuntime, SettingsManager settings, CancellationToken token = default) { - return await this.LoadModels(jsRuntime, settings, "dall-e-", token); + return this.LoadModels(jsRuntime, settings, "dall-e-", token); } #endregion - private async Task> LoadModels(IJSRuntime jsRuntime, SettingsManager settings, string prefix, CancellationToken token) + private async Task> LoadModels(IJSRuntime jsRuntime, SettingsManager settings, string prefix, CancellationToken token) { var requestedSecret = await settings.GetAPIKey(jsRuntime, this); - if(!requestedSecret.Success) - return new List(); + if (!requestedSecret.Success) + return []; var request = new HttpRequestMessage(HttpMethod.Get, "models"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", requestedSecret.Secret); - - var emptyList = new List(); + var response = await this.httpClient.SendAsync(request, token); if(!response.IsSuccessStatusCode) - return emptyList; + return []; var modelResponse = await response.Content.ReadFromJsonAsync(token); - return modelResponse.Data.Where(n => n.Id.StartsWith(prefix, StringComparison.InvariantCulture)).ToList(); + return modelResponse.Data.Where(n => n.Id.StartsWith(prefix, StringComparison.InvariantCulture)); } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Provider/Providers.cs b/app/MindWork AI Studio/Provider/Providers.cs index 83d3ad9..0c93f42 100644 --- a/app/MindWork AI Studio/Provider/Providers.cs +++ b/app/MindWork AI Studio/Provider/Providers.cs @@ -25,17 +25,19 @@ public static class ExtensionsProvider { Providers.OPEN_AI => "OpenAI", + Providers.NONE => "No provider selected", _ => "Unknown", }; - + /// /// Creates a new provider instance based on the provider value. /// /// The provider value. + /// The used instance name. /// The provider instance. - public static IProvider CreateProvider(this Providers provider) => provider switch + public static IProvider CreateProvider(this Providers provider, string instanceName) => provider switch { - Providers.OPEN_AI => new ProviderOpenAI(), + Providers.OPEN_AI => new ProviderOpenAI { InstanceName = instanceName }, _ => new NoProvider(), }; diff --git a/app/MindWork AI Studio/Settings/Data.cs b/app/MindWork AI Studio/Settings/Data.cs index ff89a31..465ebe6 100644 --- a/app/MindWork AI Studio/Settings/Data.cs +++ b/app/MindWork AI Studio/Settings/Data.cs @@ -14,7 +14,12 @@ public sealed class Data /// /// List of configured providers. /// - public List Providers { get; init; } = new(); + public List Providers { get; init; } = []; + + /// + /// The next provider number to use. + /// + public uint NextProviderNum { get; set; } = 1; /// /// Should we save energy? When true, we will update content streamed diff --git a/app/MindWork AI Studio/Settings/Provider.cs b/app/MindWork AI Studio/Settings/Provider.cs index 4d32f01..d1f6194 100644 --- a/app/MindWork AI Studio/Settings/Provider.cs +++ b/app/MindWork AI Studio/Settings/Provider.cs @@ -5,10 +5,12 @@ namespace AIStudio.Settings; /// /// Data model for configured providers. /// +/// The provider's number. /// The provider's ID. /// The provider's instance name. Useful for multiple instances of the same provider, e.g., to distinguish between different OpenAI API keys. /// The provider used. -public readonly record struct Provider(string Id, string InstanceName, Providers UsedProvider) +/// The LLM model to use for chat. +public readonly record struct Provider(uint Num, string Id, string InstanceName, Providers UsedProvider, Model Model) { #region Overrides of ValueType @@ -19,7 +21,7 @@ public readonly record struct Provider(string Id, string InstanceName, Providers /// A string that represents the current provider in a human-readable format. public override string ToString() { - return $"{this.InstanceName} ({this.UsedProvider.ToName()})"; + return $"{this.InstanceName} ({this.UsedProvider.ToName()}, {this.Model})"; } #endregion diff --git a/app/MindWork AI Studio/Settings/ProviderDialog.razor b/app/MindWork AI Studio/Settings/ProviderDialog.razor index ed5a048..0fac746 100644 --- a/app/MindWork AI Studio/Settings/ProviderDialog.razor +++ b/app/MindWork AI Studio/Settings/ProviderDialog.razor @@ -9,6 +9,7 @@ T="string" @bind-Text="@this.DataInstanceName" Label="Instance Name" + Class="mb-3" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Lightbulb" AdornmentColor="Color.Info" @@ -16,7 +17,7 @@ /> @* ReSharper disable once CSharpWarnings::CS8974 *@ - + @foreach (Providers provider in Enum.GetValues(typeof(Providers))) { @provider @@ -28,12 +29,24 @@ T="string" @bind-Text="@this.dataAPIKey" Label="API Key" + Class="mb-3" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.VpnKey" AdornmentColor="Color.Info" InputType="InputType.Password" Validation="@this.ValidatingAPIKey" /> + + + Reload + + @foreach (var model in this.availableModels) + { + @model + } + + + @if (this.dataIssues.Any()) diff --git a/app/MindWork AI Studio/Settings/ProviderDialog.razor.cs b/app/MindWork AI Studio/Settings/ProviderDialog.razor.cs index 51e602f..1a267ca 100644 --- a/app/MindWork AI Studio/Settings/ProviderDialog.razor.cs +++ b/app/MindWork AI Studio/Settings/ProviderDialog.razor.cs @@ -16,6 +16,12 @@ public partial class ProviderDialog : ComponentBase { [CascadingParameter] private MudDialogInstance MudDialog { get; set; } = null!; + + /// + /// The provider's number in the list. + /// + [Parameter] + public uint DataNum { get; set; } /// /// The provider's ID. @@ -35,6 +41,12 @@ public partial class ProviderDialog : ComponentBase [Parameter] public Providers DataProvider { get; set; } = Providers.NONE; + /// + /// The LLM model to use, e.g., GPT-4o. + /// + [Parameter] + public Model DataModel { get; set; } + /// /// Should the dialog be in editing mode? /// @@ -50,7 +62,7 @@ public partial class ProviderDialog : ComponentBase /// /// The list of used instance names. We need this to check for uniqueness. /// - private List usedInstanceNames { get; set; } = []; + private List UsedInstanceNames { get; set; } = []; private bool dataIsValid; private string[] dataIssues = []; @@ -60,28 +72,33 @@ public partial class ProviderDialog : ComponentBase // We get the form reference from Blazor code to validate it manually: private MudForm form = null!; + + private readonly List availableModels = new(); #region Overrides of ComponentBase protected override async Task OnInitializedAsync() { // Load the used instance names: - this.usedInstanceNames = this.SettingsManager.ConfigurationData.Providers.Select(x => x.InstanceName.ToLowerInvariant()).ToList(); + this.UsedInstanceNames = this.SettingsManager.ConfigurationData.Providers.Select(x => x.InstanceName.ToLowerInvariant()).ToList(); // When editing, we need to load the data: if(this.IsEditing) { this.dataEditingPreviousInstanceName = this.DataInstanceName.ToLowerInvariant(); - var provider = this.DataProvider.CreateProvider(); + var provider = this.DataProvider.CreateProvider(this.DataInstanceName); if(provider is NoProvider) return; - provider.InstanceName = this.DataInstanceName; - // Load the API key: var requestedSecret = await this.SettingsManager.GetAPIKey(this.JsRuntime, provider); if(requestedSecret.Success) + { this.dataAPIKey = requestedSecret.Secret; + + // Now, we try to load the list of available models: + await this.ReloadModels(); + } else { this.dataAPIKeyStorageIssue = $"Failed to load the API key from the operating system. The message was: {requestedSecret.Issue}. You might ignore this message and provide the API key again."; @@ -118,14 +135,15 @@ public partial class ProviderDialog : ComponentBase // 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, }; // We need to instantiate the provider to store the API key: - var provider = this.DataProvider.CreateProvider(); - provider.InstanceName = this.DataInstanceName; + var provider = this.DataProvider.CreateProvider(this.DataInstanceName); // Store the API key in the OS secure storage: var storeResponse = await this.SettingsManager.SetAPIKey(this.JsRuntime, provider, this.dataAPIKey); @@ -147,6 +165,14 @@ public partial class ProviderDialog : ComponentBase return null; } + private string? ValidatingModel(Model model) + { + if (model == default) + return "Please select a model."; + + return null; + } + [GeneratedRegex("^[a-zA-Z0-9 ]+$")] private static partial Regex InstanceNameRegex(); @@ -170,7 +196,7 @@ public partial class ProviderDialog : ComponentBase // The instance name must be unique: var lowerInstanceName = instanceName.ToLowerInvariant(); - if (lowerInstanceName != this.dataEditingPreviousInstanceName && this.usedInstanceNames.Contains(lowerInstanceName)) + if (lowerInstanceName != this.dataEditingPreviousInstanceName && this.UsedInstanceNames.Contains(lowerInstanceName)) return "The instance name must be unique; the chosen name is already in use."; return null; @@ -188,4 +214,21 @@ public partial class ProviderDialog : ComponentBase } private void Cancel() => this.MudDialog.Cancel(); + + private bool CanLoadModels => !string.IsNullOrWhiteSpace(this.dataAPIKey) && this.DataProvider != Providers.NONE && !string.IsNullOrWhiteSpace(this.DataInstanceName); + + private async Task ReloadModels() + { + var provider = this.DataProvider.CreateProvider(this.DataInstanceName); + if(provider is NoProvider) + return; + + var models = await provider.GetTextModels(this.JsRuntime, this.SettingsManager); + + // Order descending by ID means that the newest models probably come first: + var orderedModels = models.OrderByDescending(n => n.Id); + + this.availableModels.Clear(); + this.availableModels.AddRange(orderedModels); + } } \ No newline at end of file