|
1 | 1 | using System.ComponentModel; |
| 2 | +using Devlooped.Extensions.AI; |
2 | 3 | using Devlooped.Extensions.AI.OpenAI; |
3 | 4 | using Microsoft.Extensions.AI; |
4 | 5 | using Microsoft.Extensions.Configuration; |
5 | | -using Microsoft.Extensions.DependencyInjection; |
6 | 6 | using Microsoft.Extensions.DependencyInjection.Extensions; |
7 | 7 | using Microsoft.Extensions.Hosting; |
8 | 8 | using Microsoft.Extensions.Logging; |
9 | 9 | using OpenAI; |
10 | 10 |
|
11 | | -namespace Devlooped.Extensions.AI; |
| 11 | +namespace Microsoft.Extensions.DependencyInjection; |
12 | 12 |
|
13 | 13 | /// <summary> |
14 | 14 | /// Adds configuration-driven chat clients to an application host or service collection. |
15 | 15 | /// </summary> |
16 | 16 | [EditorBrowsable(EditorBrowsableState.Never)] |
17 | | -public static class AddChatClientsExtensions |
| 17 | +public static class ConfigurableChatClientExtensions |
18 | 18 | { |
19 | 19 | /// <summary> |
20 | 20 | /// Adds configuration-driven chat clients to the host application builder. |
@@ -69,11 +69,19 @@ public static IServiceCollection AddChatClients(this IServiceCollection services |
69 | 69 | return client; |
70 | 70 | }, |
71 | 71 | options?.Lifetime ?? ServiceLifetime.Singleton)); |
| 72 | + |
| 73 | + services.TryAdd(new ServiceDescriptor(typeof(IChatClient), new ServiceKey(id), |
| 74 | + factory: (sp, _) => sp.GetRequiredKeyedService<IChatClient>(id), |
| 75 | + options?.Lifetime ?? ServiceLifetime.Singleton)); |
72 | 76 | } |
73 | 77 |
|
74 | 78 | return services; |
75 | 79 | } |
76 | 80 |
|
| 81 | + /// <summary>Gets a chat client by id (case-insensitive) from the service provider.</summary> |
| 82 | + public static IChatClient? GetChatClient(this IServiceProvider services, string id) |
| 83 | + => services.GetKeyedService<IChatClient>(id) ?? services.GetKeyedService<IChatClient>(new ServiceKey(id)); |
| 84 | + |
77 | 85 | internal class ChatClientOptions : OpenAIClientOptions |
78 | 86 | { |
79 | 87 | public string? ApiKey { get; set; } |
|
0 commit comments