|  | 
|  | 1 | +using EssentialCSharp.Chat.Common.Services; | 
|  | 2 | +using Microsoft.Extensions.Configuration; | 
|  | 3 | +using Microsoft.Extensions.DependencyInjection; | 
|  | 4 | +using Microsoft.SemanticKernel; | 
|  | 5 | + | 
|  | 6 | +namespace EssentialCSharp.Chat.Common.Extensions; | 
|  | 7 | + | 
|  | 8 | +public static class ServiceCollectionExtensions | 
|  | 9 | +{ | 
|  | 10 | +    /// <summary> | 
|  | 11 | +    /// Adds Azure OpenAI and related AI services to the service collection | 
|  | 12 | +    /// </summary> | 
|  | 13 | +    /// <param name="services">The service collection to add services to</param> | 
|  | 14 | +    /// <param name="aiOptions">The AI configuration options</param> | 
|  | 15 | +    /// <returns>The service collection for chaining</returns> | 
|  | 16 | +    public static IServiceCollection AddAzureOpenAIServices(this IServiceCollection services, AIOptions aiOptions) | 
|  | 17 | +    { | 
|  | 18 | +        // Validate required configuration | 
|  | 19 | +        if (aiOptions == null) | 
|  | 20 | +        { | 
|  | 21 | +            throw new InvalidOperationException("AIOptions cannot be null."); | 
|  | 22 | +        } | 
|  | 23 | + | 
|  | 24 | +        if (string.IsNullOrEmpty(aiOptions.Endpoint) || | 
|  | 25 | +            string.IsNullOrEmpty(aiOptions.ApiKey)) | 
|  | 26 | +        { | 
|  | 27 | +            throw new InvalidOperationException("Azure OpenAI Endpoint and ApiKey must be properly configured in AIOptions. Please update your configuration with valid values."); | 
|  | 28 | +        } | 
|  | 29 | + | 
|  | 30 | +        if (string.IsNullOrEmpty(aiOptions.PostgresConnectionString) || | 
|  | 31 | +            aiOptions.PostgresConnectionString.Contains("your-postgres-connection-string")) | 
|  | 32 | +        { | 
|  | 33 | +            throw new InvalidOperationException("PostgreSQL connection string must be properly configured in AIOptions for vector store. Please update your configuration with a valid connection string."); | 
|  | 34 | +        } | 
|  | 35 | + | 
|  | 36 | +#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. | 
|  | 37 | + | 
|  | 38 | +        // Register Azure OpenAI services | 
|  | 39 | +        services.AddAzureOpenAIEmbeddingGenerator( | 
|  | 40 | +            aiOptions.VectorGenerationDeploymentName, | 
|  | 41 | +            aiOptions.Endpoint, | 
|  | 42 | +            aiOptions.ApiKey); | 
|  | 43 | + | 
|  | 44 | +        services.AddAzureOpenAIChatClient( | 
|  | 45 | +            aiOptions.ChatDeploymentName, | 
|  | 46 | +            aiOptions.Endpoint, | 
|  | 47 | +            aiOptions.ApiKey); | 
|  | 48 | + | 
|  | 49 | +        // Add PostgreSQL vector store | 
|  | 50 | +        services.AddPostgresVectorStore(aiOptions.PostgresConnectionString); | 
|  | 51 | + | 
|  | 52 | +#pragma warning restore SKEXP0010 | 
|  | 53 | + | 
|  | 54 | +        // Register shared AI services | 
|  | 55 | +        services.AddSingleton<EmbeddingService>(); | 
|  | 56 | +        services.AddSingleton<AISearchService>(); | 
|  | 57 | +        services.AddSingleton<AIChatService>(); | 
|  | 58 | +        services.AddSingleton<MarkdownChunkingService>(); | 
|  | 59 | + | 
|  | 60 | +        return services; | 
|  | 61 | +    } | 
|  | 62 | + | 
|  | 63 | +    /// <summary> | 
|  | 64 | +    /// Adds Azure OpenAI and related AI services to the service collection using configuration | 
|  | 65 | +    /// </summary> | 
|  | 66 | +    /// <param name="services">The service collection to add services to</param> | 
|  | 67 | +    /// <param name="configuration">The configuration to read AIOptions from</param> | 
|  | 68 | +    /// <returns>The service collection for chaining</returns> | 
|  | 69 | +    public static IServiceCollection AddAzureOpenAIServices(this IServiceCollection services, IConfiguration configuration) | 
|  | 70 | +    { | 
|  | 71 | +        // Configure AI options from configuration | 
|  | 72 | +        services.Configure<AIOptions>(configuration.GetSection("AIOptions")); | 
|  | 73 | + | 
|  | 74 | +        var aiOptions = configuration.GetSection("AIOptions").Get<AIOptions>(); | 
|  | 75 | + | 
|  | 76 | +        return aiOptions == null | 
|  | 77 | +            ? throw new InvalidOperationException("AIOptions section is missing from configuration.") | 
|  | 78 | +            : services.AddAzureOpenAIServices(aiOptions); | 
|  | 79 | +    } | 
|  | 80 | +} | 
0 commit comments