-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
243 lines (208 loc) · 11.5 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
243 lines (208 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using Azure.AI.OpenAI;
using Azure.Core;
using Azure.Identity;
using EssentialCSharp.Chat.Common.Services;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.SemanticKernel;
using Npgsql;
namespace EssentialCSharp.Chat.Common.Extensions;
public static class ServiceCollectionExtensions
{
private static readonly string[] _PostgresScopes = ["https://ossrdbms-aad.database.windows.net/.default"];
/// <summary>
/// Resolves the AI mode once and applies environment-specific enforcement.
/// Development allows Disabled, Local, or Azure modes. Non-Development requires Azure.
/// </summary>
public static IHostApplicationBuilder AddAIServices(
this IHostApplicationBuilder builder,
IConfiguration configuration,
AIConfigurationState? aiConfigurationState = null)
{
builder.Services.Configure<AIOptions>(configuration.GetSection("AIOptions"));
aiConfigurationState ??= AIConfigurationState.From(configuration.GetSection("AIOptions").Get<AIOptions>());
builder.Services.AddSingleton(aiConfigurationState);
if (!builder.Environment.IsDevelopment() && !aiConfigurationState.UsesAzureAI)
{
throw new InvalidOperationException(
"Non-Development environments require AIOptions:Endpoint to be configured. Local AI is only supported in Development.");
}
if (aiConfigurationState.UsesLocalAI)
{
builder.AddLocalAIServices(configuration);
}
else if (aiConfigurationState.UsesAzureAI)
{
builder.Services.AddAzureOpenAIServices(configuration);
}
else
{
builder.Services.AddSingleton<IAIChatService, UnavailableAIChatService>();
}
return builder;
}
/// <summary>
/// Registers the Ollama-backed local AI services. Uses IChatClient from
/// CommunityToolkit.Aspire.OllamaSharp. Vector search (RAG) is disabled in Phase 1
/// due to the embedding dimension mismatch (Ollama nomic-embed-text = 768 dims,
/// pgvector schema expects 1536).
/// </summary>
public static IHostApplicationBuilder AddLocalAIServices(
this IHostApplicationBuilder builder,
IConfiguration configuration)
{
builder.Services.Configure<AIOptions>(configuration.GetSection("AIOptions"));
// Registers IChatClient backed by the Ollama "ollama-chat" resource.
// Connection string injected by Aspire: Endpoint=http://...:11434;Model=qwen2.5-coder:7b
builder.AddOllamaApiClient("ollama-chat")
.AddChatClient();
// NOTE: ollama-embed (nomic-embed-text, 768 dims) not registered in Phase 1.
// The pgvector schema hardcodes 1536 dims — incompatible without schema migration.
// Phase 2: register IEmbeddingGenerator + configure VectorStoreCollectionDefinition.
builder.Services.AddSingleton<IAIChatService, LocalAIChatService>();
return builder;
}
/// <summary>
/// Adds Azure OpenAI and related AI services to the service collection using Managed Identity
/// </summary>
/// <param name="services">The service collection to add services to</param>
/// <param name="aiOptions">The AI configuration options</param>
/// <param name="postgresConnectionString">The PostgreSQL connection string for the vector store</param>
/// <param name="credential">The token credential to use for authentication. If null, DefaultAzureCredential will be used.</param>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddAzureOpenAIServices(
this IServiceCollection services,
AIOptions aiOptions,
string postgresConnectionString,
TokenCredential? credential = null)
{
// Use DefaultAzureCredential if no credential is provided
// This works both locally (using Azure CLI, Visual Studio, etc.) and in Azure (using Managed Identity)
credential ??= new DefaultAzureCredential();
if (string.IsNullOrEmpty(aiOptions.Endpoint))
{
throw new InvalidOperationException("AIOptions.Endpoint is required.");
}
var endpoint = new Uri(aiOptions.Endpoint);
// Register Azure OpenAI services with Managed Identity authentication
#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
services.AddAzureOpenAIChatClient(
aiOptions.ChatDeploymentName,
endpoint.ToString(),
credential);
services.AddSingleton(provider =>
new AzureOpenAIClient(endpoint, credential));
services.AddAzureOpenAIChatCompletion(
aiOptions.ChatDeploymentName,
aiOptions.Endpoint,
credential);
// Add PostgreSQL vector store with managed identity support
services.AddPostgresVectorStoreWithManagedIdentity(postgresConnectionString, credential);
services.AddEmbeddingGenerator(sp =>
sp.GetRequiredService<AzureOpenAIClient>()
.GetEmbeddingClient(aiOptions.VectorGenerationDeploymentName)
.AsIEmbeddingGenerator())
.UseLogging()
.UseOpenTelemetry();
#pragma warning restore SKEXP0010
// Register shared AI services — forward IAIChatService to the concrete instance
// so the CLI tool (GetRequiredService<AIChatService>()) and the web app
// (GetRequiredService<IAIChatService>()) share the same singleton.
services.AddSingleton<EmbeddingService>();
services.AddSingleton<AISearchService>();
services.AddSingleton<AIChatService>();
services.AddSingleton<IAIChatService>(sp => sp.GetRequiredService<AIChatService>());
services.AddSingleton<MarkdownChunkingService>();
return services;
}
/// <summary>
/// Adds Azure OpenAI and related AI services to the service collection using configuration
/// </summary>
/// <param name="services">The service collection to add services to</param>
/// <param name="configuration">The configuration to read AIOptions from</param>
/// <param name="credential">Optional token credential to use for authentication. If null, DefaultAzureCredential will be used.</param>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddAzureOpenAIServices(
this IServiceCollection services,
IConfiguration configuration,
TokenCredential? credential = null)
{
// Configure AI options from configuration
services.Configure<AIOptions>(configuration.GetSection("AIOptions"));
var aiOptions = configuration.GetSection("AIOptions").Get<AIOptions>();
if (aiOptions == null)
{
throw new InvalidOperationException("AIOptions section is missing from configuration.");
}
// Get PostgreSQL connection string using the standard method
var postgresConnectionString = configuration.GetConnectionString("PostgresVectorStore") ??
throw new InvalidOperationException("Connection string 'PostgresVectorStore' not found.");
return services.AddAzureOpenAIServices(aiOptions, postgresConnectionString, credential);
}
/// <summary>
/// Adds PostgreSQL vector store with managed identity authentication support.
/// Uses per-connection token refresh via <c>UsePasswordProvider</c>, which calls
/// <see cref="TokenCredential.GetTokenAsync"/> on every new physical connection.
/// <see cref="DefaultAzureCredential"/> caches tokens internally and auto-refreshes
/// ~5 minutes before expiry, so this does not add Azure AD overhead.
/// </summary>
/// <param name="services">The service collection to add services to</param>
/// <param name="connectionString">The PostgreSQL connection string (without password)</param>
/// <param name="credential">The token credential to use for authentication. If null, DefaultAzureCredential will be used.</param>
/// <returns>The service collection for chaining</returns>
private static IServiceCollection AddPostgresVectorStoreWithManagedIdentity(
this IServiceCollection services,
string connectionString,
TokenCredential? credential = null)
{
credential ??= new DefaultAzureCredential();
// Register NpgsqlDataSource with UseVector() enabled - this is critical for pgvector support
services.AddSingleton<NpgsqlDataSource>(sp =>
{
var connBuilder = new NpgsqlConnectionStringBuilder(connectionString);
bool isAzurePostgres = connBuilder.Host?.Contains(".postgres.database.azure.com",
StringComparison.OrdinalIgnoreCase) ?? false;
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
// IMPORTANT: UseVector() must be called to enable pgvector support
dataSourceBuilder.UseVector();
if (isAzurePostgres && string.IsNullOrEmpty(connBuilder.Password))
{
// Ensure SSL is enabled for Azure PostgreSQL
if (dataSourceBuilder.ConnectionStringBuilder.SslMode < SslMode.Require)
{
dataSourceBuilder.ConnectionStringBuilder.SslMode = SslMode.Require;
}
var tokenRequestContext = new TokenRequestContext(_PostgresScopes);
// UsePasswordProvider is called for every new physical connection.
// DefaultAzureCredential caches tokens internally and auto-refreshes ~5 min before
// expiry — no extra Azure AD load. This is the approach recommended by the Npgsql
// docs for cloud providers that implement their own caching (Azure MI does).
// UsePeriodicPasswordProvider is only for token sources without built-in caching.
// See: https://www.npgsql.org/doc/security.html
// See: https://github.com/npgsql/npgsql/issues/5186
//
// Note: The username is expected to be set in the connection string already
// (Aspire sets it during deployment for Azure PostgreSQL Flexible Server).
// If a standalone username-extraction fallback is ever needed, use the
// Microsoft.Azure.PostgreSQL.Auth package (UseEntraAuthentication extension)
// once it ships on NuGet.
dataSourceBuilder.UsePasswordProvider(
passwordProvider: _ => credential.GetToken(tokenRequestContext, default).Token,
passwordProviderAsync: async (_, ct) =>
(await credential.GetTokenAsync(tokenRequestContext, ct)).Token);
// Recycle pooled connections after 50 min, well before the 60-min JWT token TTL.
// Combined with UsePasswordProvider (called on every new physical connection),
// this ensures no pooled connection ever holds an expired token.
dataSourceBuilder.ConnectionStringBuilder.ConnectionLifetime = 3000;
}
return dataSourceBuilder.Build();
});
// Register the vector store using the NpgsqlDataSource from DI
#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
services.AddPostgresVectorStore();
#pragma warning restore SKEXP0010
return services;
}
}