Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Components/Aspire.OpenAI/AspireOpenAIExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ OpenAIClient ConfigureOpenAI(IServiceProvider serviceProvider)
{
if (settings.Key is not null)
{
var options = serviceProvider.GetRequiredService<IOptions<OpenAIClientOptions>>().Value;
var options = serviceKey is null ?
serviceProvider.GetRequiredService<IOptions<OpenAIClientOptions>>().Value :
serviceProvider.GetRequiredService<IOptionsMonitor<OpenAIClientOptions>>().Get(serviceKey);

return new OpenAIClient(new ApiKeyCredential(settings.Key), options);
}
else
Expand Down
34 changes: 34 additions & 0 deletions tests/Aspire.OpenAI.Tests/AspireOpenAIExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,40 @@ public void ReadsFromConnectionStringsFormats(string connectionString)
Assert.NotNull(client);
}

[Theory]
[InlineData("Endpoint=http://domain.com:12345;Key=abc123", false)]
[InlineData("Endpoint=http://domain.com:12345;Key=abc123", true)]
public void ReadsEndpointFromConnectionStrings(string connectionString, bool useKeyed)
{
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Configuration.AddInMemoryCollection([
new KeyValuePair<string, string?>("ConnectionStrings:openai", connectionString)
]);

if (useKeyed)
{
builder.AddKeyedOpenAIClient("openai");
}
else
{
builder.AddOpenAIClient("openai");
}

using var host = builder.Build();
var client = useKeyed ?
host.Services.GetRequiredKeyedService<OpenAIClient>("openai") :
host.Services.GetRequiredService<OpenAIClient>();

Assert.NotNull(client);

var endpointField = client.GetType().GetField("_endpoint", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
Assert.NotNull(endpointField);

var endpoint = endpointField.GetValue(client);
Assert.NotNull(endpoint);
Assert.Equal("http://domain.com:12345/", endpoint.ToString());
}

[Theory]
[InlineData("")]
[InlineData("Endpoint=http://domain.com:12345")]
Expand Down
Loading