Skip to content

Commit a318659

Browse files
shethaaditAdit ShethrogerbarretoSergeyMenshykh
authored
.Net: AddOpenAIEmbeddingGenerator now respects HttpClient.BaseAddress for endpoint. (#12810)
### Description This PR fixes an issue where AddOpenAIEmbeddingGenerator() ignored the BaseAddress of the provided HttpClient, always using the default OpenAI endpoint. Now, if a custom BaseAddress is set on the HttpClient, it is passed as the endpoint parameter, ensuring requests are sent to the correct server. This aligns the behavior with other OpenAI service registrations and enables proxy or custom endpoint scenarios. FIxes #12806 --------- Co-authored-by: Adit Sheth <[email protected]> Co-authored-by: Roger Barreto <[email protected]> Co-authored-by: SergeyMenshykh <[email protected]>
1 parent 99f09b4 commit a318659

File tree

3 files changed

+19
-30
lines changed

3 files changed

+19
-30
lines changed

dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Extensions/KernelBuilderExtensionsTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ public void ItCanAddAudioToTextServiceWithOpenAIClient()
144144
Assert.Equal("model", service.Attributes[AIServiceExtensions.ModelIdKey]);
145145
}
146146

147+
[Fact]
148+
public void ItCanAddEmbeddingGeneratorWithHttpClient()
149+
{
150+
// Arrange
151+
var customEndpoint = new Uri("https://custom.proxy.url/openai/v1/");
152+
using var httpClient = new System.Net.Http.HttpClient { BaseAddress = customEndpoint };
153+
var sut = Kernel.CreateBuilder();
154+
155+
// Act
156+
var kernel = sut.AddOpenAIEmbeddingGenerator("model", "key", httpClient: httpClient)
157+
.Build();
158+
var service = kernel.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>();
159+
160+
// Assert
161+
Assert.NotNull(service);
162+
Assert.Equal(customEndpoint, service.GetService<EmbeddingGeneratorMetadata>()!.ProviderUri);
163+
}
164+
147165
[Fact]
148166
[Obsolete(ObsoleteMessage)]
149167
public void ItCanAddFileService()

dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,7 @@ internal static OpenAIClientOptions GetOpenAIClientOptions(HttpClient? httpClien
199199
UserAgentApplicationId = HttpHeaderConstant.Values.UserAgent,
200200
};
201201

202-
if (endpoint is not null)
203-
{
204-
options.Endpoint = endpoint;
205-
}
202+
options.Endpoint ??= endpoint ?? httpClient?.BaseAddress;
206203

207204
options.AddPolicy(CreateRequestHeaderPolicy(HttpHeaderConstant.Names.SemanticKernelVersion, HttpHeaderConstant.Values.GetAssemblyVersion(typeof(ClientCore))), PipelinePosition.PerCall);
208205

dotnet/src/Connectors/Connectors.OpenAI/Extensions/OpenAIServiceCollectionExtensions.DependencyInjection.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using System;
44
using System.ClientModel;
5-
using System.ClientModel.Primitives;
65
using System.Diagnostics.CodeAnalysis;
76
using System.Net.Http;
87
using Microsoft.Extensions.AI;
@@ -275,29 +274,4 @@ public static IServiceCollection AddOpenAIEmbeddingGenerator(this IServiceCollec
275274
});
276275
}
277276
#endregion
278-
279-
private static OpenAIClientOptions GetClientOptions(
280-
Uri? endpoint = null,
281-
string? orgId = null,
282-
HttpClient? httpClient = null)
283-
{
284-
OpenAIClientOptions options = new();
285-
286-
if (endpoint is not null)
287-
{
288-
options.Endpoint = endpoint;
289-
}
290-
291-
if (orgId is not null)
292-
{
293-
options.OrganizationId = orgId;
294-
}
295-
296-
if (httpClient is not null)
297-
{
298-
options.Transport = new HttpClientPipelineTransport(httpClient);
299-
}
300-
301-
return options;
302-
}
303277
}

0 commit comments

Comments
 (0)