| | | 1 | | namespace Intentum.AI.OpenAI; |
| | | 2 | | |
| | | 3 | | public sealed class OpenAIOptions |
| | | 4 | | { |
| | | 5 | | private const string DefaultBaseUrl = "https://api.openai.com/v1/"; |
| | | 6 | | private const string UrlTrailingSlash = "/"; |
| | | 7 | | |
| | 21 | 8 | | public required string ApiKey { get; init; } |
| | 30 | 9 | | public string EmbeddingModel { get; init; } = "text-embedding-3-large"; |
| | 18 | 10 | | public string? BaseUrl { get; init; } |
| | | 11 | | |
| | | 12 | | public void Validate() |
| | 10 | 13 | | { |
| | 10 | 14 | | if (string.IsNullOrWhiteSpace(ApiKey)) |
| | 2 | 15 | | throw new ArgumentException("OpenAI ApiKey is required."); |
| | 8 | 16 | | if (string.IsNullOrWhiteSpace(EmbeddingModel)) |
| | 1 | 17 | | throw new ArgumentException("OpenAI EmbeddingModel is required."); |
| | 7 | 18 | | if (string.IsNullOrWhiteSpace(BaseUrl)) |
| | 3 | 19 | | throw new ArgumentException("OpenAI BaseUrl is required."); |
| | 4 | 20 | | } |
| | | 21 | | |
| | | 22 | | public static OpenAIOptions FromEnvironment() |
| | 0 | 23 | | { |
| | 0 | 24 | | var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") |
| | 0 | 25 | | ?? throw new InvalidOperationException("OPENAI_API_KEY is not set. Copy .env.example to .env and set OPENAI_ |
| | | 26 | | |
| | 0 | 27 | | var baseUrl = Environment.GetEnvironmentVariable("OPENAI_BASE_URL") |
| | 0 | 28 | | ?? DefaultBaseUrl; |
| | | 29 | | |
| | 0 | 30 | | var normalizedBaseUrl = baseUrl.TrimEnd(UrlTrailingSlash[0]) + UrlTrailingSlash; |
| | | 31 | | |
| | 0 | 32 | | return new OpenAIOptions |
| | 0 | 33 | | { |
| | 0 | 34 | | ApiKey = apiKey, |
| | 0 | 35 | | EmbeddingModel = Environment.GetEnvironmentVariable("OPENAI_EMBEDDING_MODEL") ?? "text-embedding-3-large", |
| | 0 | 36 | | BaseUrl = normalizedBaseUrl |
| | 0 | 37 | | }; |
| | 0 | 38 | | } |
| | | 39 | | } |