2024-06-19 17:25:18 -07:00
using System.Text.Json.Serialization ;
2023-06-15 13:57:47 +02:00
using Microsoft.Extensions.Configuration ;
using Microsoft.Extensions.Logging ;
2024-06-19 17:25:18 -07:00
internal sealed class KernelSettings
2023-06-15 13:57:47 +02:00
{
public const string DefaultConfigFile = "config/appsettings.json" ;
public const string OpenAI = "OPENAI" ;
public const string AzureOpenAI = "AZUREOPENAI" ;
public const string Qdrant = "QDRANT" ;
[JsonPropertyName("serviceType")]
public string ServiceType { get ; set ; } = string . Empty ;
[JsonPropertyName("serviceId")]
public string ServiceId { get ; set ; } = string . Empty ;
[JsonPropertyName("deploymentOrModelId")]
public string DeploymentOrModelId { get ; set ; } = string . Empty ;
[JsonPropertyName("embeddingDeploymentOrModelId")]
public string EmbeddingDeploymentOrModelId { get ; set ; } = string . Empty ;
[JsonPropertyName("endpoint")]
public string Endpoint { get ; set ; } = string . Empty ;
[JsonPropertyName("apiKey")]
public string ApiKey { get ; set ; } = string . Empty ;
[JsonPropertyName("orgId")]
public string OrgId { get ; set ; } = string . Empty ;
2023-09-21 21:16:21 +02:00
[JsonPropertyName("qdrantEndpoint")]
2023-08-28 20:57:56 +02:00
public string QdrantEndpoint { get ; set ; } = string . Empty ;
2023-06-15 13:57:47 +02:00
[JsonPropertyName("logLevel")]
public LogLevel ? LogLevel { get ; set ; }
/// <summary>
/// Load the kernel settings from settings.json if the file exists and if not attempt to use user secrets.
/// </summary>
internal static KernelSettings LoadSettings ( )
{
try
{
if ( File . Exists ( DefaultConfigFile ) )
{
return FromFile ( DefaultConfigFile ) ;
}
C onsole . WriteLine ( $"Semantic kernel settings '{DefaultConfigFile}' not found, attempting to load configuration from user secrets." ) ;
return FromUserSecrets ( ) ;
}
catch ( InvalidDataException ide )
{
Console . Error . WriteLine (
"Unable to load semantic kernel settings, please provide configuration settings using instructions in the README.\n" +
"Please refer to: https://github.com/microsoft/semantic-kernel-starters/blob/main/sk-csharp-hello-world/README.md#configuring-the-starter"
) ;
throw new InvalidOperationException ( ide . Message ) ;
}
}
/// <summary>
/// Load the kernel settings from the specified configuration file if it exists.
/// </summary>
internal static KernelSettings FromFile ( string configFile = DefaultConfigFile )
{
if ( ! File . Exists ( configFile ) )
{
throw new FileNotFoundException ( $"Configuration not found: {configFile}" ) ;
}
var configuration = new ConfigurationBuilder ( )
2024-03-28 13:34:33 +01:00
. SetBasePath ( Directory . GetCurrentDirectory ( ) )
2023-06-15 13:57:47 +02:00
. AddJsonFile ( configFile , optional : true , reloadOnChange : true )
2023-08-28 20:57:56 +02:00
. AddEnvironmentVariables ( )
2023-06-15 13:57:47 +02:00
. Build ( ) ;
return configuration . Get < KernelSettings > ( )
? ? throw new InvalidDataException ( $"Invalid semantic kernel settings in '{configFile}', please provide configuration settings using instructions in the README." ) ;
}
/// <summary>
/// Load the kernel settings from user secrets.
/// </summary>
internal static KernelSettings FromUserSecrets ( )
{
var configuration = new ConfigurationBuilder ( )
. AddUserSecrets < KernelSettings > ( )
. AddEnvironmentVariables ( )
. Build ( ) ;
return configuration . Get < KernelSettings > ( )
? ? throw new InvalidDataException ( "Invalid semantic kernel settings in user secrets, please provide configuration settings using instructions in the README." ) ;
}
}