2024-04-19 19:25:44 +00:00
using AIStudio.Provider ;
2024-09-01 18:10:03 +00:00
using AIStudio.Settings ;
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
using RustService = AIStudio . Tools . RustService ;
2024-07-16 08:28:13 +00:00
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>
2024-12-03 14:24:40 +00:00
public partial class ProviderDialog : ComponentBase , ISecretId
2024-04-19 19:25:44 +00:00
{
[CascadingParameter]
private MudDialogInstance 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 ;
2024-07-03 18:31:04 +00:00
/// <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 ;
2024-07-03 18:31:04 +00:00
/// <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]
2024-09-13 19:50:00 +00:00
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 ; }
[Inject]
2024-09-01 18:10:03 +00:00
private SettingsManager SettingsManager { get ; init ; } = null ! ;
2024-04-20 15:06:50 +00:00
[Inject]
2024-09-01 18:10:03 +00:00
private ILogger < ProviderDialog > Logger { get ; init ; } = null ! ;
[Inject]
private RustService RustService { get ; init ; } = null ! ;
2024-04-19 19:25:44 +00:00
2024-07-16 08:28:13 +00:00
private static readonly Dictionary < string , object? > SPELLCHECK_ATTRIBUTES = new ( ) ;
2024-06-01 18:00:17 +00:00
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 ;
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 ,
} ;
}
2024-09-01 18:10:03 +00:00
2024-11-14 05:39:35 +00:00
private Settings . Provider CreateProviderSettings ( )
2024-07-16 08:28:13 +00:00
{
2024-11-14 05:39:35 +00:00
var cleanedHostname = this . DataHostname . Trim ( ) ;
return new ( )
{
Num = this . DataNum ,
Id = this . DataId ,
InstanceName = this . DataInstanceName ,
UsedLLMProvider = this . DataLLMProvider ,
Model = this . DataLLMProvider is LLMProviders . FIREWORKS ? new Model ( this . dataManuallyModel , null ) : this . DataModel ,
IsSelfHosted = this . DataLLMProvider is LLMProviders . SELF_HOSTED ,
Hostname = cleanedHostname . EndsWith ( '/' ) ? cleanedHostname [ . . ^ 1 ] : cleanedHostname ,
Host = this . DataHost ,
} ;
}
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 ( )
{
2024-06-01 18:00:17 +00:00
// Configure the spellchecking for the instance name input:
2024-07-16 08:28:13 +00:00
this . SettingsManager . InjectSpellchecking ( SPELLCHECK_ATTRIBUTES ) ;
2024-06-01 18:00:17 +00:00
2024-05-04 08:55:00 +00:00
// Load the used instance names:
2024-05-19 14:12:50 +00:00
this . UsedInstanceNames = this . SettingsManager . ConfigurationData . Providers . Select ( x = > x . InstanceName . ToLowerInvariant ( ) ) . ToList ( ) ;
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
2024-09-08 19:01:51 +00:00
// When using Fireworks, we must copy the model name:
2024-09-13 19:50:00 +00:00
if ( this . DataLLMProvider is LLMProviders . FIREWORKS )
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:
//
2024-10-07 11:26:25 +00:00
if ( this . DataLLMProvider is LLMProviders . SELF_HOSTED & & this . DataHost is not Host . OLLAMA )
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:
2024-12-03 14:24:40 +00:00
var requestedSecret = await this . RustService . GetAPIKey ( this , isTrying : this . DataLLMProvider is LLMProviders . SELF_HOSTED ) ;
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
{
2024-10-07 11:26:25 +00:00
this . dataAPIKey = string . Empty ;
if ( this . DataLLMProvider is not LLMProviders . SELF_HOSTED )
{
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." ;
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
}
await base . OnInitializedAsync ( ) ;
}
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 ;
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 ( ) ;
2024-10-07 11:26:25 +00:00
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:
2024-12-03 14:24:40 +00:00
var storeResponse = await this . RustService . SetAPIKey ( this , this . dataAPIKey ) ;
2024-07-16 08:28:13 +00:00
if ( ! storeResponse . Success )
{
this . dataAPIKeyStorageIssue = $"Failed to store the API key in the operating system. The message was: {storeResponse.Issue}. Please try again." ;
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 )
{
2024-09-13 19:50:00 +00:00
if ( this . DataLLMProvider is LLMProviders . FIREWORKS & & string . IsNullOrWhiteSpace ( manuallyModel ) )
2024-07-25 13:29:44 +00:00
return "Please enter a model name." ;
return null ;
}
2024-05-19 14:12:07 +00:00
2024-07-03 18:31:04 +00:00
private void Cancel ( ) = > this . MudDialog . Cancel ( ) ;
2024-05-19 17:42:15 +00:00
2024-05-19 14:12:07 +00:00
private async Task ReloadModels ( )
{
2024-07-16 08:28:13 +00:00
var currentProviderSettings = this . CreateProviderSettings ( ) ;
2024-09-01 18:10:03 +00:00
var provider = currentProviderSettings . CreateProvider ( this . Logger ) ;
2024-05-19 14:12:07 +00:00
if ( provider is NoProvider )
return ;
2024-07-24 17:27:25 +00:00
2024-09-01 18:10:03 +00:00
var models = await provider . GetTextModels ( this . dataAPIKey ) ;
2024-05-19 14:12:07 +00:00
// 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 ) ;
}
2024-07-03 18:31:04 +00:00
2024-10-07 11:26:25 +00:00
private string APIKeyText = > this . DataLLMProvider switch
{
LLMProviders . SELF_HOSTED = > "(Optional) API Key" ,
_ = > "API Key" ,
} ;
2024-07-25 13:29:44 +00:00
2024-09-13 19:50:00 +00:00
private bool IsNoneProvider = > this . DataLLMProvider is LLMProviders . NONE ;
2024-04-19 19:25:44 +00:00
}