-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
85 lines (71 loc) · 3.61 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
85 lines (71 loc) · 3.61 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
using Azure.AI.OpenAI;
using EssentialCSharp.Chat.Common.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
namespace EssentialCSharp.Chat.Common.Extensions;
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds Azure OpenAI and related AI services to the service collection
/// </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>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddAzureOpenAIServices(this IServiceCollection services, AIOptions aiOptions, string postgresConnectionString)
{
if (string.IsNullOrEmpty(aiOptions.Endpoint) ||
string.IsNullOrEmpty(aiOptions.ApiKey))
// Register Azure OpenAI services
#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.AddAzureOpenAIEmbeddingGenerator(
aiOptions.VectorGenerationDeploymentName,
aiOptions.Endpoint,
aiOptions.ApiKey);
services.AddAzureOpenAIChatClient(
aiOptions.ChatDeploymentName,
aiOptions.Endpoint,
aiOptions.ApiKey);
services.AddSingleton(provider =>
new AzureOpenAIClient(new Uri(aiOptions.Endpoint), new Azure.AzureKeyCredential(aiOptions.ApiKey)));
// Register Azure OpenAI services
services.AddAzureOpenAIEmbeddingGenerator(
aiOptions.VectorGenerationDeploymentName,
aiOptions.Endpoint,
aiOptions.ApiKey);
services.AddAzureOpenAIChatCompletion(
aiOptions.ChatDeploymentName,
aiOptions.Endpoint,
aiOptions.ApiKey);
// Add PostgreSQL vector store
services.AddPostgresVectorStore(postgresConnectionString);
#pragma warning restore SKEXP0010
// Register shared AI services
services.AddSingleton<EmbeddingService>();
services.AddSingleton<AISearchService>();
services.AddSingleton<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>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddAzureOpenAIServices(this IServiceCollection services, IConfiguration configuration)
{
// 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);
}
}