|
| 1 | +using Devlooped.Extensions.AI.Grok; |
| 2 | +using Devlooped.Extensions.AI.OpenAI; |
| 3 | +using Microsoft.Extensions.AI; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using OpenAI; |
| 7 | + |
| 8 | +namespace Devlooped.Extensions.AI; |
| 9 | + |
| 10 | +public static class UseChatClientsExtensions |
| 11 | +{ |
| 12 | + public static IServiceCollection UseChatClients(this IServiceCollection services, IConfiguration configuration, Action<string, ChatClientBuilder>? configure = default, string prefix = "ai:clients") |
| 13 | + { |
| 14 | + foreach (var entry in configuration.AsEnumerable().Where(x => |
| 15 | + x.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) && |
| 16 | + x.Key.EndsWith("modelid", StringComparison.OrdinalIgnoreCase))) |
| 17 | + { |
| 18 | + var section = string.Join(':', entry.Key.Split(':')[..^1]); |
| 19 | + // ID == section after clients:, with optional overridable id |
| 20 | + var id = configuration[$"{section}:id"] ?? section[(prefix.Length + 1)..]; |
| 21 | + |
| 22 | + var options = configuration.GetSection(section).Get<ChatClientOptions>(); |
| 23 | + Throw.IfNullOrEmpty(options?.ModelId, entry.Key); |
| 24 | + |
| 25 | + var apikey = options!.ApiKey; |
| 26 | + // If the key contains a section-like value, get it from config |
| 27 | + if (apikey?.Contains('.') == true || apikey?.Contains(':') == true) |
| 28 | + apikey = configuration[apikey.Replace('.', ':')] ?? configuration[apikey.Replace('.', ':') + ":apikey"]; |
| 29 | + |
| 30 | + var keysection = section; |
| 31 | + // ApiKey inheritance by section parents. |
| 32 | + // i.e. section ai:clients:grok:router does not need to have its own key, |
| 33 | + // it will inherit from ai:clients:grok:key, for example. |
| 34 | + while (string.IsNullOrEmpty(apikey)) |
| 35 | + { |
| 36 | + keysection = string.Join(':', keysection.Split(':')[..^1]); |
| 37 | + if (string.IsNullOrEmpty(keysection)) |
| 38 | + break; |
| 39 | + apikey = configuration[$"{keysection}:apikey"]; |
| 40 | + } |
| 41 | + |
| 42 | + Throw.IfNullOrEmpty(apikey, $"{section}:apikey"); |
| 43 | + |
| 44 | + var builder = services.AddKeyedChatClient(id, services => |
| 45 | + { |
| 46 | + if (options.Endpoint?.Host == "api.x.ai") |
| 47 | + return new GrokChatClient(apikey, options.ModelId, options); |
| 48 | + |
| 49 | + return new OpenAIChatClient(apikey, options.ModelId, options); |
| 50 | + }, options.Lifetime); |
| 51 | + |
| 52 | + configure?.Invoke(id, builder); |
| 53 | + } |
| 54 | + |
| 55 | + return services; |
| 56 | + } |
| 57 | + |
| 58 | + class ChatClientOptions : OpenAIClientOptions |
| 59 | + { |
| 60 | + public string? ApiKey { get; set; } |
| 61 | + public string? ModelId { get; set; } |
| 62 | + public ServiceLifetime Lifetime { get; set; } = ServiceLifetime.Singleton; |
| 63 | + } |
| 64 | +} |
0 commit comments