You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

KernelSettings.cs 3.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Text.Json.Serialization;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Extensions.Logging;
  4. internal sealed class KernelSettings
  5. {
  6. public const string DefaultConfigFile = "config/appsettings.json";
  7. public const string OpenAI = "OPENAI";
  8. public const string AzureOpenAI = "AZUREOPENAI";
  9. public const string Qdrant = "QDRANT";
  10. [JsonPropertyName("serviceType")]
  11. public string ServiceType { get; set; } = string.Empty;
  12. [JsonPropertyName("serviceId")]
  13. public string ServiceId { get; set; } = string.Empty;
  14. [JsonPropertyName("deploymentOrModelId")]
  15. public string DeploymentOrModelId { get; set; } = string.Empty;
  16. [JsonPropertyName("embeddingDeploymentOrModelId")]
  17. public string EmbeddingDeploymentOrModelId { get; set; } = string.Empty;
  18. [JsonPropertyName("endpoint")]
  19. public string Endpoint { get; set; } = string.Empty;
  20. [JsonPropertyName("apiKey")]
  21. public string ApiKey { get; set; } = string.Empty;
  22. [JsonPropertyName("orgId")]
  23. public string OrgId { get; set; } = string.Empty;
  24. [JsonPropertyName("qdrantEndpoint")]
  25. public string QdrantEndpoint { get; set; } = string.Empty;
  26. [JsonPropertyName("logLevel")]
  27. public LogLevel? LogLevel { get; set; }
  28. /// <summary>
  29. /// Load the kernel settings from settings.json if the file exists and if not attempt to use user secrets.
  30. /// </summary>
  31. internal static KernelSettings LoadSettings()
  32. {
  33. try
  34. {
  35. if (File.Exists(DefaultConfigFile))
  36. {
  37. return FromFile(DefaultConfigFile);
  38. }
  39. Console.WriteLine($"Semantic kernel settings '{DefaultConfigFile}' not found, attempting to load configuration from user secrets.");
  40. return FromUserSecrets();
  41. }
  42. catch (InvalidDataException ide)
  43. {
  44. Console.Error.WriteLine(
  45. "Unable to load semantic kernel settings, please provide configuration settings using instructions in the README.\n" +
  46. "Please refer to: https://github.com/microsoft/semantic-kernel-starters/blob/main/sk-csharp-hello-world/README.md#configuring-the-starter"
  47. );
  48. throw new InvalidOperationException(ide.Message);
  49. }
  50. }
  51. /// <summary>
  52. /// Load the kernel settings from the specified configuration file if it exists.
  53. /// </summary>
  54. internal static KernelSettings FromFile(string configFile = DefaultConfigFile)
  55. {
  56. if (!File.Exists(configFile))
  57. {
  58. throw new FileNotFoundException($"Configuration not found: {configFile}");
  59. }
  60. var configuration = new ConfigurationBuilder()
  61. .SetBasePath(Directory.GetCurrentDirectory())
  62. .AddJsonFile(configFile, optional: true, reloadOnChange: true)
  63. .AddEnvironmentVariables()
  64. .Build();
  65. return configuration.Get<KernelSettings>()
  66. ?? throw new InvalidDataException($"Invalid semantic kernel settings in '{configFile}', please provide configuration settings using instructions in the README.");
  67. }
  68. /// <summary>
  69. /// Load the kernel settings from user secrets.
  70. /// </summary>
  71. internal static KernelSettings FromUserSecrets()
  72. {
  73. var configuration = new ConfigurationBuilder()
  74. .AddUserSecrets<KernelSettings>()
  75. .AddEnvironmentVariables()
  76. .Build();
  77. return configuration.Get<KernelSettings>()
  78. ?? throw new InvalidDataException("Invalid semantic kernel settings in user secrets, please provide configuration settings using instructions in the README.");
  79. }
  80. }