Skip to content

Commit

Permalink
Using the built-in method to register a HttpClient in .NET for Ollama…
Browse files Browse the repository at this point in the history
…Sharp

This means that we can leverage the configurations applied to HttpClient via ServiceDefault around stuff like resiliancy, rather than creating a HttpClient in isolation that is not aware of any of that configuration.

No new tests are added as there's not we could easily test with this, but all existing tests should continue to pass

Fixes #474
  • Loading branch information
aaronpowell committed Mar 6, 2025
1 parent 0202521 commit e3f59b9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<PackageVersion Include="Microsoft.Extensions.AI" Version="$(MEAIVersion)" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="$(DotNetExtensionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.DependencyModel" Version="$(DotNetExtensionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="$(DotNetExtensionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(DotNetExtensionsVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.3" />
<!-- .NET packages -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ private static AspireOllamaApiClientBuilder AddOllamaClientInternal(

configureSettings?.Invoke(settings);

string httpClientKey = $"{connectionName}_httpClient";

builder.Services.AddHttpClient(httpClientKey, client =>
{
if (settings.Endpoint is not null)
{
client.BaseAddress = settings.Endpoint;
}
else
{
throw new InvalidOperationException(
$"An OllamaApiClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or either " +
$"{nameof(settings.Endpoint)} must be provided " +
$"in the '{configurationSectionName}' configuration section.");
}
});

if (serviceKey is not null)
{
builder.Services.AddKeyedSingleton(serviceKey, (sp, _) => ConfigureOllamaClient(sp));
Expand All @@ -147,22 +164,16 @@ private static AspireOllamaApiClientBuilder AddOllamaClientInternal(

IOllamaApiClient ConfigureOllamaClient(IServiceProvider serviceProvider)
{
if (settings.Endpoint is not null)
HttpClient httpClient = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(httpClientKey);

OllamaApiClient client = new(httpClient);
if (!string.IsNullOrWhiteSpace(settings.SelectedModel))
{
var client = new OllamaApiClient(new HttpClient { BaseAddress = settings.Endpoint });
if (!string.IsNullOrWhiteSpace(settings.SelectedModel))
{
client.SelectedModel = settings.SelectedModel;
client.Config.Model = settings.SelectedModel;
}

return client;
client.SelectedModel = settings.SelectedModel;
client.Config.Model = settings.SelectedModel;
}

throw new InvalidOperationException(
$"An OllamaApiClient could not be configured. Ensure valid connection information was provided in 'ConnectionStrings:{connectionName}' or either " +
$"{nameof(settings.Endpoint)} must be provided " +
$"in the '{configurationSectionName}' configuration section.");
return client;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.Extensions.AI" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
<PackageReference Include="OllamaSharp" />
</ItemGroup>
Expand Down

0 comments on commit e3f59b9

Please sign in to comment.