Skip to content

Add custom service key support to AddKeyedOllamaApiClient #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 2, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 25, 2025

This PR adds the ability to specify custom service keys when registering keyed Ollama API clients, resolving the limitation where users couldn't differentiate between multiple clients using the same connection but different models.

Problem

Previously, AddKeyedOllamaApiClient automatically used the connection name as the service key, making it impossible to register multiple Ollama clients for different use cases (e.g., chat vs vision models) on the same connection:

// BEFORE: Impossible - both would use "LocalAI" as service key
builder.AddKeyedOllamaApiClient("LocalAI", settings => settings.SelectedModel = "llama3.2");
builder.AddKeyedOllamaApiClient("LocalAI", settings => settings.SelectedModel = "llava"); // Conflict!

Solution

Added two new overloads following the established RavenDB pattern that accept an explicit object serviceKey parameter:

// NEW: Custom service key with connection name
public static AspireOllamaApiClientBuilder AddKeyedOllamaApiClient(
    this IHostApplicationBuilder builder, 
    object serviceKey, 
    string connectionName, 
    Action<OllamaSharpSettings>? configureSettings = null)

// NEW: Custom service key with direct settings
public static AspireOllamaApiClientBuilder AddKeyedOllamaApiClient(
    this IHostApplicationBuilder builder, 
    object serviceKey, 
    OllamaSharpSettings settings)

Usage

Now users can differentiate between multiple models using custom service keys:

// AFTER: Possible with custom service keys
builder.AddKeyedOllamaApiClient("ChatModel", "LocalAI", settings => 
    settings.SelectedModel = "llama3.2").AddKeyedChatClient();
    
builder.AddKeyedOllamaApiClient("VisionModel", "LocalAI", settings => 
    settings.SelectedModel = "llava").AddKeyedChatClient();

// Retrieve specific clients for different purposes
var chatClient = services.GetRequiredKeyedService<IChatClient>("ChatModel");
var visionClient = services.GetRequiredKeyedService<IChatClient>("VisionModel");

Changes Made

  • Added new AddKeyedOllamaApiClient overloads with explicit object serviceKey parameter
  • Updated AspireOllamaApiClientBuilder to use object? for service key instead of string
  • Updated related extension methods (AddKeyedChatClient, AddKeyedEmbeddingGenerator) to handle the new type
  • Added comprehensive tests covering the new functionality
  • Updated API surface documentation
  • Maintained full backward compatibility

Testing

Added tests to verify:

  • Multiple keyed clients with custom service keys work correctly
  • Chat clients work with custom service keys
  • Settings overload works correctly
  • The exact scenario from the issue (same connection, different models) works
  • Backward compatibility is maintained

Fixes #717.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: curl -I -sSL --retry 5 --retry-delay 2 --connect-timeout 15 REDACTED (dns block)
  • dnceng.pkgs.visualstudio.com
    • Triggering command: dotnet build src/CommunityToolkit.Aspire.OllamaSharp/CommunityToolkit.Aspire.OllamaSharp.csproj (dns block)
    • Triggering command: dotnet test tests/CommunityToolkit.Aspire.OllamaSharp.Tests/ --no-build --verbosity minimal (dns block)
    • Triggering command: dotnet build --no-restore (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits June 25, 2025 05:50
Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] AddKeyedOllamaApiClient doesn't let you specify the key. Add custom service key support to AddKeyedOllamaApiClient Jun 25, 2025
@Copilot Copilot AI requested a review from aaronpowell June 25, 2025 05:54
Copilot finished work on behalf of aaronpowell June 25, 2025 05:54
@aaronpowell
Copy link
Member

@copilot can we also support keys passed to AddKeyedChatClient and the embedding generator method

…ddingGenerator

Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 26, 2025

@copilot can we also support keys passed to AddKeyedChatClient and the embedding generator method

Added support for custom service keys in both AddKeyedChatClient and AddKeyedEmbeddingGenerator methods. You can now register multiple chat clients or embedding generators with different service keys that all use the same underlying OllamaApiClient.

New overloads:

  • AddKeyedChatClient(object serviceKey)
  • AddKeyedEmbeddingGenerator(object serviceKey)

This allows scenarios like:

builder.AddKeyedOllamaApiClient("OllamaKey", "LocalAI")
    .AddKeyedChatClient("ChatKey1")
    .AddKeyedChatClient("ChatKey2")
    .AddKeyedEmbeddingGenerator("EmbedKey");

Implemented in commit 2c95517.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dnceng.pkgs.visualstudio.com
    • Triggering command: dotnet build src/CommunityToolkit.Aspire.OllamaSharp/CommunityToolkit.Aspire.OllamaSharp.csproj --no-restore (dns block)
    • Triggering command: dotnet test tests/CommunityToolkit.Aspire.OllamaSharp.Tests/ --no-build --verbosity minimal (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of aaronpowell June 26, 2025 02:28
@aaronpowell aaronpowell marked this pull request as ready for review June 27, 2025 04:06
Copy link
Member

@Alirexaa Alirexaa Jun 27, 2025

Choose a reason for hiding this comment

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

This file should not be change at this PR. We have a seperate workflow for changing this file.

Copy link
Member

Choose a reason for hiding this comment

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

ah yes, sorry I keep forgetting that in the review

Copy link
Member

Choose a reason for hiding this comment

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

@copilot undo the changes in this file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted the changes to the API surface file as requested. This auto-generated file should not be manually modified. Fixed in commit 63729e3.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted the changes to the API surface file as requested. This auto-generated file should not be manually modified. Fixed in commit 63729e3.

Copy link
Member

Choose a reason for hiding this comment

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

@copilot undo the changes in this file

Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 30, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dnceng.pkgs.visualstudio.com
    • Triggering command: dotnet build src/CommunityToolkit.Aspire.OllamaSharp/CommunityToolkit.Aspire.OllamaSharp.csproj --no-restore (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Copilot Copilot AI requested a review from aaronpowell June 30, 2025 01:48
Copilot finished work on behalf of aaronpowell June 30, 2025 01:48
Copy link
Contributor

github-actions bot commented Jul 2, 2025

Code Coverage

Package Line Rate Branch Rate Complexity Health
CommunityToolkit.Aspire.EventStore 100% 100% 46
CommunityToolkit.Aspire.GoFeatureFlag 100% 97% 74
CommunityToolkit.Aspire.Hosting.ActiveMQ 80% 40% 100
CommunityToolkit.Aspire.Hosting.ActiveMQ.MassTransit 1% 0% 14
CommunityToolkit.Aspire.Hosting.Adminer 73% 50% 20
CommunityToolkit.Aspire.Hosting.Azure.Dapr 67% 50% 828
CommunityToolkit.Aspire.Hosting.Azure.Dapr.Redis 97% 88% 36
CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder 100% 100% 22
CommunityToolkit.Aspire.Hosting.Bun 82% 71% 54
CommunityToolkit.Aspire.Hosting.Dapr 63% 51% 724
CommunityToolkit.Aspire.Hosting.DbGate 94% 50% 18
CommunityToolkit.Aspire.Hosting.Deno 84% 75% 72
CommunityToolkit.Aspire.Hosting.EventStore 94% 100% 18
CommunityToolkit.Aspire.Hosting.GoFeatureFlag 93% 50% 18
CommunityToolkit.Aspire.Hosting.Golang 90% 70% 24
CommunityToolkit.Aspire.Hosting.Java 69% 72% 120
CommunityToolkit.Aspire.Hosting.k6 58% 12% 20
CommunityToolkit.Aspire.Hosting.LavinMQ 78% 50% 18
CommunityToolkit.Aspire.Hosting.LavinMQ.MassTransit 1% 0% 14
CommunityToolkit.Aspire.Hosting.MailPit 91% 50% 14
CommunityToolkit.Aspire.Hosting.Meilisearch 73% 57% 50
CommunityToolkit.Aspire.Hosting.Minio 93% 75% 48
CommunityToolkit.Aspire.Hosting.MongoDB.Extensions 96% 83% 36
CommunityToolkit.Aspire.Hosting.MySql.Extensions 100% 88% 76
CommunityToolkit.Aspire.Hosting.Ngrok 52% 35% 82
CommunityToolkit.Aspire.Hosting.NodeJS.Extensions 47% 52% 136
CommunityToolkit.Aspire.Hosting.Ollama 67% 70% 174
CommunityToolkit.Aspire.Hosting.PapercutSmtp 92% 50% 10
CommunityToolkit.Aspire.Hosting.PostgreSQL.Extensions 98% 88% 92
CommunityToolkit.Aspire.Hosting.Python.Extensions 68% 55% 90
CommunityToolkit.Aspire.Hosting.RavenDB 63% 49% 136
CommunityToolkit.Aspire.Hosting.Redis.Extensions 100% 80% 40
CommunityToolkit.Aspire.Hosting.Rust 94% 83% 16
CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects 76% 64% 154
CommunityToolkit.Aspire.Hosting.Sqlite 96% 89% 42
CommunityToolkit.Aspire.Hosting.SqlServer.Extensions 100% 85% 76
CommunityToolkit.Aspire.MassTransit.RabbitMQ 100% 100% 30
CommunityToolkit.Aspire.Meilisearch 97% 92% 68
CommunityToolkit.Aspire.Microsoft.Data.Sqlite 89% 85% 52
CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite 50% 50% 88
CommunityToolkit.Aspire.Minio.Client 73% 59% 88
CommunityToolkit.Aspire.OllamaSharp 74% 69% 120
CommunityToolkit.Aspire.RavenDB.Client 60% 53% 237
Summary 71% (6601 / 9261) 60% (2110 / 3526) 4195

Minimum allowed line rate is 60%

@aaronpowell aaronpowell temporarily deployed to azure-artifacts July 2, 2025 01:36 — with GitHub Actions Inactive
@aaronpowell aaronpowell merged commit 0c23ed1 into main Jul 2, 2025
93 of 94 checks passed
@aaronpowell aaronpowell deleted the copilot/fix-717 branch July 2, 2025 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AddKeyedOllamaApiClient doesn't let you specify the key.
3 participants