Skip to content

Commit 79d4eed

Browse files
Steinblockeerhardt
andauthored
fix for wrong OpenAIClientOptions used with keyed OpenAIClient(s) (#11003)
* fix for wrong OpenAIClientOptions used with keyed OpenAIClient(s) with keyed OpenAIClients ``` builder.AddKeyedOpenAIClient("openai"); builder.AddKeyedOpenAIClient("ionos"); ``` assuming that at least one keyed OpenAIClient has a differnt endpoint, ``` { "ConnectionStrings": { "openai": "Key={account_key};" "ionos": "Endpoint=https://{openai_rest_api_url};Key={account_key};" } } ``` getting the client with ``` var client = serviceProvider.GetRequiredKeyedService<OpenAIClient>("ionos") ``` the client uses the key from the connectionstring but it still has the default enpoint https://api.openai.com/v1 The issue is that ConfigureOpenAI ignores the serviceKey and always get's the default OpenAIClientOptions instead of the one created with optionsName = servicekey. This makes using keyed OpenAIClients virtually impossible because the main reason having multiple OpenAIClients is having different endpoints. Fix #9543 * added unit test * PR feedback --------- Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
1 parent ad5031b commit 79d4eed

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Components/Aspire.OpenAI/AspireOpenAIExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ OpenAIClient ConfigureOpenAI(IServiceProvider serviceProvider)
124124
{
125125
if (settings.Key is not null)
126126
{
127-
var options = serviceProvider.GetRequiredService<IOptions<OpenAIClientOptions>>().Value;
127+
var options = serviceKey is null ?
128+
serviceProvider.GetRequiredService<IOptions<OpenAIClientOptions>>().Value :
129+
serviceProvider.GetRequiredService<IOptionsMonitor<OpenAIClientOptions>>().Get(serviceKey);
130+
128131
return new OpenAIClient(new ApiKeyCredential(settings.Key), options);
129132
}
130133
else

tests/Aspire.OpenAI.Tests/AspireOpenAIExtensionsTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,40 @@ public void ReadsFromConnectionStringsFormats(string connectionString)
8686
Assert.NotNull(client);
8787
}
8888

89+
[Theory]
90+
[InlineData("Endpoint=http://domain.com:12345;Key=abc123", false)]
91+
[InlineData("Endpoint=http://domain.com:12345;Key=abc123", true)]
92+
public void ReadsEndpointFromConnectionStrings(string connectionString, bool useKeyed)
93+
{
94+
var builder = Host.CreateEmptyApplicationBuilder(null);
95+
builder.Configuration.AddInMemoryCollection([
96+
new KeyValuePair<string, string?>("ConnectionStrings:openai", connectionString)
97+
]);
98+
99+
if (useKeyed)
100+
{
101+
builder.AddKeyedOpenAIClient("openai");
102+
}
103+
else
104+
{
105+
builder.AddOpenAIClient("openai");
106+
}
107+
108+
using var host = builder.Build();
109+
var client = useKeyed ?
110+
host.Services.GetRequiredKeyedService<OpenAIClient>("openai") :
111+
host.Services.GetRequiredService<OpenAIClient>();
112+
113+
Assert.NotNull(client);
114+
115+
var endpointField = client.GetType().GetField("_endpoint", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
116+
Assert.NotNull(endpointField);
117+
118+
var endpoint = endpointField.GetValue(client);
119+
Assert.NotNull(endpoint);
120+
Assert.Equal("http://domain.com:12345/", endpoint.ToString());
121+
}
122+
89123
[Theory]
90124
[InlineData("")]
91125
[InlineData("Endpoint=http://domain.com:12345")]

0 commit comments

Comments
 (0)