AI-Studio/app/MindWork AI Studio/Dialogs/ProviderDialog.razor.cs

348 lines
12 KiB
C#
Raw Normal View History

2025-04-27 14:13:15 +00:00
using AIStudio.Components;
2024-04-19 19:25:44 +00:00
using AIStudio.Provider;
using AIStudio.Provider.HuggingFace;
using AIStudio.Tools.Services;
2024-12-03 14:24:40 +00:00
using AIStudio.Tools.Validation;
2024-04-19 19:25:44 +00:00
using Microsoft.AspNetCore.Components;
2024-07-16 08:28:13 +00:00
using Host = AIStudio.Provider.SelfHosted.Host;
2024-09-01 18:10:03 +00:00
namespace AIStudio.Dialogs;
2024-04-19 19:25:44 +00:00
2024-05-04 08:55:00 +00:00
/// <summary>
/// The provider settings dialog.
/// </summary>
2025-04-27 14:13:15 +00:00
public partial class ProviderDialog : MSGComponentBase, ISecretId
2024-04-19 19:25:44 +00:00
{
[CascadingParameter]
2025-03-12 18:12:56 +00:00
private IMudDialogInstance MudDialog { get; set; } = null!;
2024-05-19 18:28:25 +00:00
/// <summary>
/// The provider's number in the list.
/// </summary>
[Parameter]
public uint DataNum { get; set; }
2024-04-19 21:27:38 +00:00
2024-05-04 08:55:00 +00:00
/// <summary>
/// The provider's ID.
/// </summary>
2024-04-19 21:27:38 +00:00
[Parameter]
2024-04-20 15:06:50 +00:00
public string DataId { get; set; } = Guid.NewGuid().ToString();
2024-05-04 08:55:00 +00:00
/// <summary>
/// The user chosen instance name.
/// </summary>
2024-04-20 15:06:50 +00:00
[Parameter]
public string DataInstanceName { get; set; } = string.Empty;
/// <summary>
/// The chosen hostname for self-hosted providers.
/// </summary>
[Parameter]
public string DataHostname { get; set; } = string.Empty;
2024-07-16 08:28:13 +00:00
/// <summary>
2024-12-03 14:24:40 +00:00
/// The host to use, e.g., llama.cpp.
2024-07-16 08:28:13 +00:00
/// </summary>
[Parameter]
public Host DataHost { get; set; } = Host.NONE;
/// <summary>
/// The HFInstanceProvider to use, e.g., CEREBRAS.
/// </summary>
[Parameter]
2025-04-20 13:24:43 +00:00
public HFInferenceProvider HFInferenceProviderId { get; set; } = HFInferenceProvider.NONE;
/// <summary>
/// Is this provider self-hosted?
/// </summary>
[Parameter]
public bool IsSelfHosted { get; set; }
2024-05-04 08:55:00 +00:00
/// <summary>
/// The provider to use.
/// </summary>
2024-04-20 15:06:50 +00:00
[Parameter]
public LLMProviders DataLLMProvider { get; set; } = LLMProviders.NONE;
2024-05-04 08:55:00 +00:00
2024-05-19 14:12:07 +00:00
/// <summary>
/// The LLM model to use, e.g., GPT-4o.
/// </summary>
[Parameter]
public Model DataModel { get; set; }
2024-05-04 08:55:00 +00:00
/// <summary>
/// Should the dialog be in editing mode?
/// </summary>
2024-04-20 15:06:50 +00:00
[Parameter]
public bool IsEditing { get; init; }
[Parameter]
public string AdditionalJsonApiParameters { get; set; } = string.Empty;
2024-09-01 18:10:03 +00:00
[Inject]
private RustService RustService { get; init; } = null!;
2024-04-19 19:25:44 +00:00
[Inject]
private ILogger<ProviderDialog> Logger { get; init; } = null!;
2024-07-16 08:28:13 +00:00
private static readonly Dictionary<string, object?> SPELLCHECK_ATTRIBUTES = new();
2024-05-04 08:55:00 +00:00
/// <summary>
/// The list of used instance names. We need this to check for uniqueness.
/// </summary>
2024-05-19 14:12:50 +00:00
private List<string> UsedInstanceNames { get; set; } = [];
2024-04-20 15:06:50 +00:00
2024-04-19 19:25:44 +00:00
private bool dataIsValid;
private string[] dataIssues = [];
2024-04-20 15:06:50 +00:00
private string dataAPIKey = string.Empty;
2024-07-25 13:29:44 +00:00
private string dataManuallyModel = string.Empty;
2024-04-20 15:06:50 +00:00
private string dataAPIKeyStorageIssue = string.Empty;
private string dataEditingPreviousInstanceName = string.Empty;
private string dataLoadingModelsIssue = string.Empty;
private bool showExpertSettings;
2024-04-19 19:25:44 +00:00
2024-05-04 08:55:00 +00:00
// We get the form reference from Blazor code to validate it manually:
2024-04-19 19:25:44 +00:00
private MudForm form = null!;
2024-05-19 14:12:07 +00:00
private readonly List<Model> availableModels = new();
2024-09-01 18:10:03 +00:00
private readonly Encryption encryption = Program.ENCRYPTION;
2024-12-03 14:24:40 +00:00
private readonly ProviderValidation providerValidation;
public ProviderDialog()
{
this.providerValidation = new()
{
GetProvider = () => this.DataLLMProvider,
GetAPIKeyStorageIssue = () => this.dataAPIKeyStorageIssue,
GetPreviousInstanceName = () => this.dataEditingPreviousInstanceName,
GetUsedInstanceNames = () => this.UsedInstanceNames,
GetHost = () => this.DataHost,
IsModelProvidedManually = () => this.DataLLMProvider.IsLLMModelProvidedManually(),
2024-12-03 14:24:40 +00:00
};
}
2024-09-01 18:10:03 +00:00
2025-01-05 14:11:15 +00:00
private AIStudio.Settings.Provider CreateProviderSettings()
2024-07-16 08:28:13 +00:00
{
var cleanedHostname = this.DataHostname.Trim();
// Determine the model based on the provider and host configuration:
Model model;
if (this.DataLLMProvider.IsLLMModelSelectionHidden(this.DataHost))
{
// Use system model placeholder for hosts that don't support model selection (e.g., llama.cpp):
model = Model.SYSTEM_MODEL;
}
else if (this.DataLLMProvider is LLMProviders.FIREWORKS or LLMProviders.HUGGINGFACE)
{
// These providers require manual model entry:
model = new Model(this.dataManuallyModel, null);
}
else
model = this.DataModel;
return new()
{
Num = this.DataNum,
Id = this.DataId,
InstanceName = this.DataInstanceName,
UsedLLMProvider = this.DataLLMProvider,
Model = model,
IsSelfHosted = this.DataLLMProvider is LLMProviders.SELF_HOSTED,
IsEnterpriseConfiguration = false,
Hostname = cleanedHostname.EndsWith('/') ? cleanedHostname[..^1] : cleanedHostname,
Host = this.DataHost,
2025-04-20 13:24:43 +00:00
HFInferenceProvider = this.HFInferenceProviderId,
AdditionalJsonApiParameters = this.AdditionalJsonApiParameters,
};
}
2024-04-19 19:25:44 +00:00
2024-04-20 15:06:50 +00:00
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Call the base initialization first so that the I18N is ready:
await base.OnInitializedAsync();
// Configure the spellchecking for the instance name input:
2024-07-16 08:28:13 +00:00
this.SettingsManager.InjectSpellchecking(SPELLCHECK_ATTRIBUTES);
2024-05-04 08:55:00 +00:00
// Load the used instance names:
#pragma warning disable MWAIS0001
2024-05-19 14:12:50 +00:00
this.UsedInstanceNames = this.SettingsManager.ConfigurationData.Providers.Select(x => x.InstanceName.ToLowerInvariant()).ToList();
#pragma warning restore MWAIS0001
this.showExpertSettings = !string.IsNullOrWhiteSpace(this.AdditionalJsonApiParameters);
2024-04-20 15:06:50 +00:00
2024-05-04 08:55:00 +00:00
// When editing, we need to load the data:
2024-04-20 15:06:50 +00:00
if(this.IsEditing)
{
this.dataEditingPreviousInstanceName = this.DataInstanceName.ToLowerInvariant();
2024-07-16 08:28:13 +00:00
// When using Fireworks or Hugging Face, we must copy the model name:
if (this.DataLLMProvider.IsLLMModelProvidedManually())
2024-09-08 19:01:51 +00:00
this.dataManuallyModel = this.DataModel.Id;
2024-07-16 08:28:13 +00:00
//
// We cannot load the API key for self-hosted providers:
//
if (this.DataLLMProvider is LLMProviders.SELF_HOSTED && this.DataHost is not Host.OLLAMA && this.DataHost is not Host.VLLM)
2024-07-16 08:28:13 +00:00
{
await this.ReloadModels();
await base.OnInitializedAsync();
return;
}
2024-05-04 08:55:00 +00:00
// Load the API key:
var requestedSecret = await this.RustService.GetAPIKey(this, SecretStoreType.LLM_PROVIDER, isTrying: this.DataLLMProvider is LLMProviders.SELF_HOSTED);
2024-12-03 14:24:40 +00:00
if (requestedSecret.Success)
2024-09-01 18:10:03 +00:00
this.dataAPIKey = await requestedSecret.Secret.Decrypt(this.encryption);
2024-04-20 15:06:50 +00:00
else
{
this.dataAPIKey = string.Empty;
if (this.DataLLMProvider is not LLMProviders.SELF_HOSTED)
{
2025-04-27 14:13:15 +00:00
this.dataAPIKeyStorageIssue = string.Format(T("Failed to load the API key from the operating system. The message was: {0}. You might ignore this message and provide the API key again."), requestedSecret.Issue);
await this.form.Validate();
}
2024-04-20 15:06:50 +00:00
}
2024-12-03 14:24:40 +00:00
await this.ReloadModels();
2024-04-20 15:06:50 +00:00
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
2024-05-04 08:55:00 +00:00
// Reset the validation when not editing and on the first render.
// We don't want to show validation errors when the user opens the dialog.
2024-04-20 15:06:50 +00:00
if(!this.IsEditing && firstRender)
this.form.ResetValidation();
await base.OnAfterRenderAsync(firstRender);
}
#endregion
2024-12-03 14:24:40 +00:00
#region Implementation of ISecretId
public string SecretId => this.DataLLMProvider.ToName();
2024-04-20 15:06:50 +00:00
2024-12-03 14:24:40 +00:00
public string SecretName => this.DataInstanceName;
#endregion
2024-04-20 15:06:50 +00:00
private async Task Store()
2024-04-19 19:25:44 +00:00
{
await this.form.Validate();
2024-04-20 15:06:50 +00:00
if (!string.IsNullOrWhiteSpace(this.dataAPIKeyStorageIssue))
this.dataAPIKeyStorageIssue = string.Empty;
// Manually validate the model selection (needed when no models are loaded
// and the MudSelect is not rendered):
var modelValidationError = this.providerValidation.ValidatingModel(this.DataModel);
if (!string.IsNullOrWhiteSpace(modelValidationError))
{
this.dataIssues = [..this.dataIssues, modelValidationError];
this.dataIsValid = false;
}
2024-05-04 08:55:00 +00:00
// When the data is not valid, we don't store it:
2024-04-19 19:25:44 +00:00
if (!this.dataIsValid)
return;
2024-05-04 08:55:00 +00:00
// Use the data model to store the provider.
// We just return this data to the parent component:
2024-07-16 08:28:13 +00:00
var addedProviderSettings = this.CreateProviderSettings();
if (!string.IsNullOrWhiteSpace(this.dataAPIKey))
2024-04-19 19:25:44 +00:00
{
2024-07-16 08:28:13 +00:00
// Store the API key in the OS secure storage:
var storeResponse = await this.RustService.SetAPIKey(this, this.dataAPIKey, SecretStoreType.LLM_PROVIDER);
2024-07-16 08:28:13 +00:00
if (!storeResponse.Success)
{
2025-04-27 14:13:15 +00:00
this.dataAPIKeyStorageIssue = string.Format(T("Failed to store the API key in the operating system. The message was: {0}. Please try again."), storeResponse.Issue);
2024-07-16 08:28:13 +00:00
await this.form.Validate();
return;
}
2024-04-20 15:06:50 +00:00
}
2024-07-16 08:28:13 +00:00
this.MudDialog.Close(DialogResult.Ok(addedProviderSettings));
2024-04-19 19:25:44 +00:00
}
2024-07-25 13:29:44 +00:00
private string? ValidateManuallyModel(string manuallyModel)
{
if (this.DataLLMProvider.IsLLMModelProvidedManually() && string.IsNullOrWhiteSpace(manuallyModel))
2025-04-27 14:13:15 +00:00
return T("Please enter a model name.");
2024-07-25 13:29:44 +00:00
return null;
}
2024-05-19 14:12:07 +00:00
private void Cancel() => this.MudDialog.Cancel();
private async Task OnAPIKeyChanged(string apiKey)
{
this.dataAPIKey = apiKey;
if (!string.IsNullOrWhiteSpace(this.dataAPIKeyStorageIssue))
{
this.dataAPIKeyStorageIssue = string.Empty;
await this.form.Validate();
}
}
private void OnHostChanged(Host selectedHost)
{
// When the host changes, reset the model selection state:
this.DataHost = selectedHost;
this.DataModel = default;
this.dataManuallyModel = string.Empty;
this.availableModels.Clear();
this.dataLoadingModelsIssue = string.Empty;
}
2024-05-19 14:12:07 +00:00
private async Task ReloadModels()
{
this.dataLoadingModelsIssue = string.Empty;
2024-07-16 08:28:13 +00:00
var currentProviderSettings = this.CreateProviderSettings();
2025-09-03 19:25:17 +00:00
var provider = currentProviderSettings.CreateProvider();
if (provider is NoProvider)
2024-05-19 14:12:07 +00:00
return;
try
{
var models = await provider.GetTextModels(this.dataAPIKey);
// 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);
}
catch (Exception e)
{
this.Logger.LogError($"Failed to load models from provider '{this.DataLLMProvider}' (host={this.DataHost}, hostname='{this.DataHostname}'): {e.Message}");
this.dataLoadingModelsIssue = T("We are currently unable to communicate with the provider to load models. Please try again later.");
}
2024-05-19 14:12:07 +00:00
}
private string APIKeyText => this.DataLLMProvider switch
{
2025-04-27 14:13:15 +00:00
LLMProviders.SELF_HOSTED => T("(Optional) API Key"),
_ => T("API Key"),
};
private void ToggleExpertSettings() => this.showExpertSettings = !this.showExpertSettings;
private void OnInputChangeExpertSettings()
{
this.AdditionalJsonApiParameters = this.AdditionalJsonApiParameters.Trim().TrimEnd(',', ' ');
}
private string GetExpertStyles => this.showExpertSettings ? "border-2 border-dashed rounded pa-2" : string.Empty;
private static string GetPlaceholderExpertSettings =>
"""
"temperature": 0.5,
"top_p": 0.9,
"frequency_penalty": 0.0
""";
2024-04-19 19:25:44 +00:00
}