AzureOpenAIClient CreateClientFromConfigSection()

in src/WebJobs.Extensions.OpenAI/OpenAIClientFactory.cs [95:129]


    AzureOpenAIClient CreateClientFromConfigSection(string aiConnectionName)
    {
        IConfigurationSection section = this.configuration.GetSection(aiConnectionName);

        if (!section.Exists())
        {
            this.logger.LogInformation($"Configuration section for Azure OpenAI not found, trying fallback to environment variables - AZURE_OPENAI_ENDPOINT and/or AZURE_OPENAI_KEY");
        }

        this.aiEndpoint = section?.GetValue<string>("Endpoint") ?? Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
        string? azureOpenAIKey = section?.GetValue<string>("Key") ?? Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");

        if (!string.IsNullOrEmpty(this.aiEndpoint))
        {
            this.logger.LogInformation($"Using Azure OpenAI endpoint: {this.aiEndpoint}");
            if (!string.IsNullOrEmpty(azureOpenAIKey))
            {
                this.logger.LogInformation($"Authenticating using Azure OpenAI Key.");
                return this.CreateAzureOpenAIClient(this.aiEndpoint, azureOpenAIKey);
            }
            else
            {
                this.logger.LogInformation($"Authenticating using Azure OpenAI TokenCredential.");

                TokenCredential tokenCredential = section.Exists() ?
                    this.azureComponentFactory.CreateTokenCredential(section) :
                    new DefaultAzureCredential();
                return this.CreateAzureOpenAIClientWithTokenCredential(this.aiEndpoint, tokenCredential);
            }
        }

        string errorMessage = $"Configuration section '{aiConnectionName}' is missing required 'Endpoint' and/or 'Key' values.";
        this.logger.LogError(errorMessage);
        throw new InvalidOperationException(errorMessage);
    }