diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs index c4743350c2b8..4a7adba2ac6a 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs @@ -53,14 +53,12 @@ IChatClient Factory(IServiceProvider serviceProvider, object? _) { var loggerFactory = serviceProvider.GetService(); - ClientCore.GetOpenAIClientOptions( - endpoint: null, - orgId: orgId, - httpClient: HttpClientProvider.GetHttpClient(httpClient, serviceProvider)); - var builder = new OpenAIClient( credential: new ApiKeyCredential(apiKey ?? SingleSpace), - options: ClientCore.GetOpenAIClientOptions(HttpClientProvider.GetHttpClient(httpClient, serviceProvider))) + options: ClientCore.GetOpenAIClientOptions( + httpClient: HttpClientProvider.GetHttpClient(httpClient, serviceProvider), + endpoint: null, + orgId: orgId)) .GetChatClient(modelId) .AsIChatClient() .AsBuilder() @@ -153,10 +151,34 @@ IChatClient Factory(IServiceProvider serviceProvider, object? _) { var loggerFactory = serviceProvider.GetService(); + // Get or create HttpClient with proper BaseAddress for the endpoint + HttpClient innerHttpClient; + if (httpClient is not null) + { + innerHttpClient = httpClient; + } + else + { + var defaultClient = HttpClientProvider.GetHttpClient(serviceProvider); + // If using default client and it doesn't have BaseAddress set or BaseAddress doesn't match the endpoint, create one with the endpoint + if (defaultClient.BaseAddress is null || defaultClient.BaseAddress != endpoint) + { + Verify.NotNull(endpoint); + + // A new one needs to be created as we can't cross boundaries and modify an existing client + innerHttpClient = HttpClientProvider.GetHttpClient(); + innerHttpClient.BaseAddress = endpoint; + } + else + { + innerHttpClient = defaultClient; + } + } + var builder = new OpenAIClient( credential: new ApiKeyCredential(apiKey ?? SingleSpace), options: ClientCore.GetOpenAIClientOptions( - httpClient: HttpClientProvider.GetHttpClient(httpClient, serviceProvider), + httpClient: innerHttpClient, endpoint: endpoint, orgId: orgId)) .GetChatClient(modelId) diff --git a/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs b/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs index 21504eda40c2..b3045c3e9f5d 100644 --- a/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs +++ b/dotnet/src/InternalUtilities/src/Diagnostics/ModelDiagnostics.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using System.Text; using System.Text.Json; using Microsoft.SemanticKernel.ChatCompletion;