2024-04-19 21:27:38 +00:00
using System.Text.RegularExpressions ;
2024-04-19 19:25:44 +00:00
using AIStudio.Provider ;
using Microsoft.AspNetCore.Components ;
2024-07-16 08:28:13 +00:00
using Host = AIStudio . Provider . SelfHosted . Host ;
2024-04-19 19:25:44 +00:00
namespace AIStudio.Settings ;
2024-05-04 08:55:00 +00:00
/// <summary>
/// The provider settings dialog.
/// </summary>
2024-04-19 19:25:44 +00:00
public partial class ProviderDialog : ComponentBase
{
[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>
/// The local host to use, e.g., llama.cpp.
/// </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]
public Providers DataProvider { get ; set ; } = Providers . 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]
private SettingsManager SettingsManager { get ; set ; } = null ! ;
[Inject]
private IJSRuntime JsRuntime { get ; set ; } = 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-07-16 08:28:13 +00:00
private Provider CreateProviderSettings ( ) = > new ( )
{
Num = this . DataNum ,
Id = this . DataId ,
InstanceName = this . DataInstanceName ,
UsedProvider = this . DataProvider ,
2024-07-25 13:29:44 +00:00
Model = this . DataProvider is Providers . FIREWORKS ? new Model ( this . dataManuallyModel ) : this . DataModel ,
2024-07-16 08:28:13 +00:00
IsSelfHosted = this . DataProvider is Providers . SELF_HOSTED ,
2024-07-24 17:27:25 +00:00
Hostname = this . DataHostname . EndsWith ( '/' ) ? this . DataHostname [ . . ^ 1 ] : this . DataHostname ,
2024-07-16 08:28:13 +00:00
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
//
// We cannot load the API key for self-hosted providers:
//
if ( this . DataProvider is Providers . SELF_HOSTED )
{
await this . ReloadModels ( ) ;
await base . OnInitializedAsync ( ) ;
return ;
}
var loadedProviderSettings = this . CreateProviderSettings ( ) ;
var provider = loadedProviderSettings . CreateProvider ( ) ;
2024-04-20 15:06:50 +00:00
if ( provider is NoProvider )
2024-07-16 08:28:13 +00:00
{
await base . OnInitializedAsync ( ) ;
2024-04-20 15:06:50 +00:00
return ;
2024-07-16 08:28:13 +00:00
}
2024-04-20 15:06:50 +00:00
2024-05-04 08:55:00 +00:00
// Load the API key:
2024-04-20 15:06:50 +00:00
var requestedSecret = await this . SettingsManager . GetAPIKey ( this . JsRuntime , provider ) ;
if ( requestedSecret . Success )
2024-05-19 14:12:07 +00:00
{
2024-04-20 15:06:50 +00:00
this . dataAPIKey = requestedSecret . Secret ;
2024-05-19 14:12:07 +00:00
// Now, we try to load the list of available models:
2024-07-16 08:28:13 +00:00
await this . ReloadModels ( ) ;
2024-05-19 14:12:07 +00:00
}
2024-04-20 15:06:50 +00:00
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." ;
await this . form . Validate ( ) ;
}
}
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
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 ( ) ;
if ( addedProviderSettings . UsedProvider ! = Providers . SELF_HOSTED )
2024-04-19 19:25:44 +00:00
{
2024-07-16 08:28:13 +00:00
// We need to instantiate the provider to store the API key:
var provider = addedProviderSettings . CreateProvider ( ) ;
2024-04-20 15:06:50 +00:00
2024-07-16 08:28:13 +00:00
// Store the API key in the OS secure storage:
var storeResponse = await this . SettingsManager . SetAPIKey ( this . JsRuntime , provider , this . dataAPIKey ) ;
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
}
private string? ValidatingProvider ( Providers provider )
{
if ( provider = = Providers . NONE )
return "Please select a provider." ;
return null ;
}
2024-04-19 21:27:38 +00:00
2024-07-16 08:28:13 +00:00
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 ;
}
2024-07-25 13:29:44 +00:00
private string? ValidateManuallyModel ( string manuallyModel )
{
if ( this . DataProvider is Providers . FIREWORKS & & string . IsNullOrWhiteSpace ( manuallyModel ) )
return "Please enter a model name." ;
return null ;
}
2024-05-19 14:12:07 +00:00
private string? ValidatingModel ( Model model )
{
2024-07-16 08:28:13 +00:00
if ( this . DataProvider is Providers . SELF_HOSTED & & this . DataHost = = Host . LLAMACPP )
2024-07-03 18:31:04 +00:00
return null ;
2024-05-19 14:12:07 +00:00
if ( model = = default )
return "Please select a model." ;
return null ;
}
2024-07-03 18:31:04 +00:00
[GeneratedRegex(@"^[a-zA-Z0-9\-_. ] + $ ")]
2024-04-20 15:06:50 +00:00
private static partial Regex InstanceNameRegex ( ) ;
2024-07-16 08:28:13 +00:00
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" ] ;
2024-07-03 18:31:04 +00:00
2024-04-19 21:27:38 +00:00
private string? ValidatingInstanceName ( string instanceName )
{
2024-07-03 18:31:04 +00:00
if ( string . IsNullOrWhiteSpace ( instanceName ) )
2024-04-20 15:06:50 +00:00
return "Please enter an instance name." ;
2024-07-03 18:31:04 +00:00
if ( instanceName . StartsWith ( ' ' ) | | instanceName . StartsWith ( '.' ) )
return "The instance name must not start with a space or a dot." ;
if ( instanceName . EndsWith ( ' ' ) | | instanceName . EndsWith ( '.' ) )
return "The instance name must not end with a space or a dot." ;
if ( instanceName . StartsWith ( '-' ) | | instanceName . StartsWith ( '_' ) )
return "The instance name must not start with a hyphen or an underscore." ;
2024-04-19 21:27:38 +00:00
2024-07-03 18:31:04 +00:00
if ( instanceName . Length > 255 )
return "The instance name must not exceed 255 characters." ;
2024-04-19 21:27:38 +00:00
if ( ! InstanceNameRegex ( ) . IsMatch ( instanceName ) )
2024-07-03 18:31:04 +00:00
return "The instance name must only contain letters, numbers, spaces, hyphens, underscores, and dots." ;
2024-04-19 21:27:38 +00:00
2024-07-03 18:31:04 +00:00
if ( instanceName . Contains ( " " ) )
2024-04-19 21:27:38 +00:00
return "The instance name must not contain consecutive spaces." ;
2024-07-03 18:31:04 +00:00
if ( RESERVED_NAMES . Contains ( instanceName . ToUpperInvariant ( ) ) )
return "This name is reserved and cannot be used." ;
if ( instanceName . Any ( c = > Path . GetInvalidFileNameChars ( ) . Contains ( c ) ) )
return "The instance name contains invalid characters." ;
2024-04-19 21:27:38 +00:00
// The instance name must be unique:
2024-04-20 15:06:50 +00:00
var lowerInstanceName = instanceName . ToLowerInvariant ( ) ;
2024-05-19 14:12:50 +00:00
if ( lowerInstanceName ! = this . dataEditingPreviousInstanceName & & this . UsedInstanceNames . Contains ( lowerInstanceName ) )
2024-04-19 21:27:38 +00:00
return "The instance name must be unique; the chosen name is already in use." ;
return null ;
}
2024-04-20 15:06:50 +00:00
private string? ValidatingAPIKey ( string apiKey )
{
2024-07-03 18:31:04 +00:00
if ( this . DataProvider is Providers . SELF_HOSTED )
return null ;
2024-04-20 15:06:50 +00:00
if ( ! string . IsNullOrWhiteSpace ( this . dataAPIKeyStorageIssue ) )
return this . dataAPIKeyStorageIssue ;
if ( string . IsNullOrWhiteSpace ( apiKey ) )
return "Please enter an API key." ;
return null ;
}
2024-04-19 19:25:44 +00:00
2024-07-03 18:31:04 +00:00
private string? ValidatingHostname ( string hostname )
{
if ( this . DataProvider ! = Providers . SELF_HOSTED )
return null ;
if ( string . IsNullOrWhiteSpace ( hostname ) )
return "Please enter a hostname, e.g., http://localhost:1234" ;
if ( ! hostname . StartsWith ( "http://" , StringComparison . InvariantCultureIgnoreCase ) & & ! hostname . StartsWith ( "https://" , StringComparison . InvariantCultureIgnoreCase ) )
return "The hostname must start with either http:// or https://" ;
if ( ! Uri . TryCreate ( hostname , UriKind . Absolute , out _ ) )
return "The hostname is not a valid HTTP(S) URL." ;
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 ( ) ;
var provider = currentProviderSettings . CreateProvider ( ) ;
2024-05-19 14:12:07 +00:00
if ( provider is NoProvider )
return ;
2024-07-24 17:27:25 +00:00
2024-06-03 17:43:13 +00:00
var models = await provider . GetTextModels ( this . JsRuntime , this . SettingsManager , 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-07-16 08:28:13 +00:00
private bool CanLoadModels ( )
{
if ( this . DataProvider is Providers . 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 . DataProvider is Providers . NONE )
return false ;
if ( string . IsNullOrWhiteSpace ( this . dataAPIKey ) )
return false ;
return true ;
}
2024-07-25 13:29:44 +00:00
private bool ShowRegisterButton = > this . DataProvider switch
{
Providers . OPEN_AI = > true ,
Providers . MISTRAL = > true ,
Providers . ANTHROPIC = > true ,
Providers . FIREWORKS = > true ,
_ = > false ,
} ;
private bool NeedAPIKey = > this . DataProvider switch
{
Providers . OPEN_AI = > true ,
Providers . MISTRAL = > true ,
Providers . ANTHROPIC = > true ,
Providers . FIREWORKS = > true ,
_ = > false ,
} ;
private bool NeedHostname = > this . DataProvider switch
{
Providers . SELF_HOSTED = > true ,
_ = > false ,
} ;
private bool NeedHost = > this . DataProvider switch
{
Providers . SELF_HOSTED = > true ,
_ = > false ,
} ;
2024-07-03 18:31:04 +00:00
2024-07-25 13:29:44 +00:00
private bool ProvideModelManually = > this . DataProvider switch
{
Providers . FIREWORKS = > true ,
_ = > false ,
} ;
2024-07-16 08:28:13 +00:00
2024-07-25 13:29:44 +00:00
private string GetModelOverviewURL ( ) = > this . DataProvider switch
{
Providers . FIREWORKS = > "https://fireworks.ai/models?show=Serverless" ,
_ = > string . Empty ,
} ;
2024-07-03 18:31:04 +00:00
private string GetProviderCreationURL ( ) = > this . DataProvider switch
{
Providers . OPEN_AI = > "https://platform.openai.com/signup" ,
Providers . MISTRAL = > "https://console.mistral.ai/" ,
Providers . ANTHROPIC = > "https://console.anthropic.com/dashboard" ,
2024-07-25 13:29:44 +00:00
Providers . FIREWORKS = > "https://fireworks.ai/login" ,
2024-07-03 18:31:04 +00:00
_ = > string . Empty ,
} ;
2024-07-25 13:29:44 +00:00
private bool IsNoneProvider = > this . DataProvider is Providers . NONE ;
2024-04-19 19:25:44 +00:00
}