| | | 1 | | using JetBrains.Annotations; |
| | | 2 | | |
| | | 3 | | namespace Intentum.AI.AzureOpenAI; |
| | | 4 | | |
| | | 5 | | public sealed class AzureOpenAIOptions |
| | | 6 | | { |
| | | 7 | | private const string UrlTrailingSlash = "/"; |
| | | 8 | | |
| | 12 | 9 | | public required string Endpoint { get; init; } |
| | 11 | 10 | | public required string ApiKey { get; init; } |
| | 17 | 11 | | public string EmbeddingDeployment { get; init; } = "embedding"; |
| | 16 | 12 | | public string ApiVersion { get; init; } = "2023-05-15"; |
| | | 13 | | |
| | | 14 | | public void Validate() |
| | 6 | 15 | | { |
| | 6 | 16 | | if (string.IsNullOrWhiteSpace(Endpoint)) |
| | 1 | 17 | | throw new ArgumentException("AzureOpenAI Endpoint is required."); |
| | 5 | 18 | | if (string.IsNullOrWhiteSpace(ApiKey)) |
| | 1 | 19 | | throw new ArgumentException("AzureOpenAI ApiKey is required."); |
| | 4 | 20 | | if (string.IsNullOrWhiteSpace(EmbeddingDeployment)) |
| | 1 | 21 | | throw new ArgumentException("AzureOpenAI EmbeddingDeployment is required."); |
| | 3 | 22 | | if (string.IsNullOrWhiteSpace(ApiVersion)) |
| | 1 | 23 | | throw new ArgumentException("AzureOpenAI ApiVersion is required."); |
| | 2 | 24 | | } |
| | | 25 | | |
| | | 26 | | [UsedImplicitly] |
| | | 27 | | public static AzureOpenAIOptions FromEnvironment() |
| | 0 | 28 | | { |
| | 0 | 29 | | var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") |
| | 0 | 30 | | ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set. Copy .env.example to .env and set |
| | 0 | 31 | | var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") |
| | 0 | 32 | | ?? throw new InvalidOperationException("AZURE_OPENAI_API_KEY is not set. Copy .env.example to .env and set A |
| | | 33 | | |
| | 0 | 34 | | var normalizedEndpoint = endpoint.TrimEnd(UrlTrailingSlash[0]) + UrlTrailingSlash; |
| | | 35 | | |
| | 0 | 36 | | return new AzureOpenAIOptions |
| | 0 | 37 | | { |
| | 0 | 38 | | Endpoint = normalizedEndpoint, |
| | 0 | 39 | | ApiKey = apiKey, |
| | 0 | 40 | | EmbeddingDeployment = Environment.GetEnvironmentVariable("AZURE_OPENAI_EMBEDDING_DEPLOYMENT") ?? "embedding" |
| | 0 | 41 | | ApiVersion = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_VERSION") ?? "2023-05-15" |
| | 0 | 42 | | }; |
| | 0 | 43 | | } |
| | | 44 | | } |