| | | 1 | | using JetBrains.Annotations; |
| | | 2 | | |
| | | 3 | | namespace Intentum.AI.Gemini; |
| | | 4 | | |
| | | 5 | | public sealed class GeminiOptions |
| | | 6 | | { |
| | | 7 | | private const string DefaultBaseUrl = "https://generativelanguage.googleapis.com/v1beta/"; |
| | | 8 | | private const string UrlTrailingSlash = "/"; |
| | | 9 | | |
| | 11 | 10 | | public required string ApiKey { get; init; } |
| | 15 | 11 | | public string EmbeddingModel { get; init; } = "text-embedding-004"; |
| | 8 | 12 | | public string? BaseUrl { get; init; } |
| | | 13 | | |
| | | 14 | | public void Validate() |
| | 5 | 15 | | { |
| | 5 | 16 | | if (string.IsNullOrWhiteSpace(ApiKey)) |
| | 1 | 17 | | throw new ArgumentException("Gemini ApiKey is required."); |
| | 4 | 18 | | if (string.IsNullOrWhiteSpace(EmbeddingModel)) |
| | 1 | 19 | | throw new ArgumentException("Gemini EmbeddingModel is required."); |
| | 3 | 20 | | if (string.IsNullOrWhiteSpace(BaseUrl)) |
| | 1 | 21 | | throw new ArgumentException("Gemini BaseUrl is required."); |
| | 2 | 22 | | } |
| | | 23 | | |
| | | 24 | | [UsedImplicitly] |
| | | 25 | | public static GeminiOptions FromEnvironment() |
| | 0 | 26 | | { |
| | 0 | 27 | | var apiKey = Environment.GetEnvironmentVariable("GEMINI_API_KEY") |
| | 0 | 28 | | ?? throw new InvalidOperationException("GEMINI_API_KEY is not set. Copy .env.example to .env and set GEMINI_ |
| | | 29 | | |
| | 0 | 30 | | var baseUrl = Environment.GetEnvironmentVariable("GEMINI_BASE_URL") |
| | 0 | 31 | | ?? DefaultBaseUrl; |
| | | 32 | | |
| | 0 | 33 | | var normalizedBaseUrl = baseUrl.TrimEnd(UrlTrailingSlash[0]) + UrlTrailingSlash; |
| | | 34 | | |
| | 0 | 35 | | return new GeminiOptions |
| | 0 | 36 | | { |
| | 0 | 37 | | ApiKey = apiKey, |
| | 0 | 38 | | EmbeddingModel = Environment.GetEnvironmentVariable("GEMINI_EMBEDDING_MODEL") ?? "text-embedding-004", |
| | 0 | 39 | | BaseUrl = normalizedBaseUrl |
| | 0 | 40 | | }; |
| | 0 | 41 | | } |
| | | 42 | | } |