Skip to content

Conversation

ViveliDuCh
Copy link
Member

@ViveliDuCh ViveliDuCh commented Sep 25, 2025

Fixes #6331

Restore standard global HTTP client resilience configuration in ServiceDefaults/Program.cs, removing Ollama-specific timeouts and settings. Apply extended timeouts and custom resilience handlers only to chat_httpClient and embeddings_httpClient in Web/Program.cs using RemoveAllResilienceHandlers and AddStandardResilienceHandler. Scope Ollama configuration to the web project to prevent unintended global changes and align with .NET Aspire template conventions.

Microsoft Reviewers: Open in CodeFlow

@ViveliDuCh ViveliDuCh requested a review from jozkee September 25, 2025 14:56
@ViveliDuCh ViveliDuCh self-assigned this Sep 25, 2025
@ViveliDuCh ViveliDuCh added area-ai Microsoft.Extensions.AI libraries area-ai-templates Microsoft.Extensions.AI.Templates and removed area-ai Microsoft.Extensions.AI libraries labels Sep 25, 2025
builder.Services.AddSingleton<SemanticSearch>();
#if (IsOllama)
// Get the IHttpClientBuilder for chat_httpClient
var chatClientBuilder = builder.Services.AddHttpClient("chat_httpClient");
Copy link
Member

@jozkee jozkee Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how are these named http clients used by ollama?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed offline, this is not ideal as chat_httpClient is an implementation detail, we shouldn't depend on it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it would be nice to not rely on the internal behavior of another library like this.

That said, I don't currently see another option. We need a way to get the IHttpClientBuilder. This might be a feature request for https://github.com/CommunityToolkit/Aspire.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't currently see another option

I think the intent of #6331 was to just move the following to Web/Program.cs

// Turn on resilience by default
http.AddStandardResilienceHandler(config =>
{
// Extend the HTTP Client timeout for Ollama
config.AttemptTimeout.Timeout = TimeSpan.FromMinutes(3);
// Must be at least double the AttemptTimeout to pass options validation
config.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(10);
config.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(10);
});

And remove that #if (IsOllama) block, @MackinnonBuck, wouldn't that suffice? As per @jongalloway, that would narrow it down to configure only the clients in Web.csproj.

We would have two ConfigureHttpClientDefaults calls, though, not sure if that's OK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, we tried to narrow it down further checking the IHttpClientBuilder names:

builder.Services.ConfigureHttpClientDefaults(http =>
{
    if (http.Name.Contains("chat") || http.Name.Contains("embeddings"))
    {
#pragma warning disable EXTEXP0001 // RemoveAllResilienceHandlers is experimental
        http.RemoveAllResilienceHandlers();
#pragma warning restore EXTEXP0001

        // Turn on resilience by default
        http.AddStandardResilienceHandler(config =>
        {
            // Extend the HTTP Client timeout for Ollama
            config.AttemptTimeout.Timeout = TimeSpan.FromMinutes(3);

            // Must be at least double the AttemptTimeout to pass options validation
            config.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(10);
            config.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(10);
        });

        // Turn on service discovery by default
        http.AddServiceDiscovery();
    }
});

But this didn't work, we only observed builders with null names. We would need to dig deeper to understand what's happening, just letting you know in case it rings a bell.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, my main request is that we scope to the web project as opposed to the entire Aspire solution. Could also add a comment explaining the behavior and recommending removing the block of code before deploying to production (since Ollama won't be used in production).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. Thanks for the clarification.

My main suggestion would be to consider putting this logic somewhere other than Program.cs - maybe another file defining an additional extension method that gets called from Program.cs.

@ViveliDuCh ViveliDuCh requested a review from jozkee September 29, 2025 22:08
@ViveliDuCh ViveliDuCh marked this pull request as ready for review September 29, 2025 22:48
@ViveliDuCh ViveliDuCh requested a review from a team as a code owner September 29, 2025 22:48
@Copilot Copilot AI review requested due to automatic review settings September 29, 2025 22:48
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR scopes Ollama-specific HTTP resilience settings to the web project only, restoring standard global HTTP client configuration in ServiceDefaults. The change prevents unintended global configuration changes and aligns with .NET Aspire template conventions by keeping Ollama-specific timeouts isolated to where they're needed.

Key changes:

  • Moved Ollama-specific resilience configuration from ServiceDefaults to Web project
  • Created dedicated extension method for Ollama HTTP client resilience settings
  • Updated template configuration to conditionally include the new extension file

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
Program.Aspire.cs Adds conditional call to Ollama-specific HTTP resilience configuration
AspireOllamaResilienceHandlerExtensions.cs New extension method containing Ollama-specific HTTP client resilience settings
Extensions.cs Removes Ollama-specific configuration, restores standard resilience handler
template.json Updates template configuration to conditionally include new extension file

…ndler settings instead of the hardcoded calls in Web/Program.cs
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-ai-templates Microsoft.Extensions.AI.Templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AI Chat Template - Ollama resiliency modification should be in web app, not service defaults
4 participants