Skip to content

[New article]: Add how-to that shows how to switch model providers with Microsoft.Extensions.AI #48552

@luisquintanilla

Description

@luisquintanilla

Proposed topic or title

How to switch model providers with Microsoft.Extensions.AI

Location in table of contents.

https://github.com/dotnet/docs/tree/main/docs/ai/how-to

Reason for the article

Existing articles assume you're working with a single provider. However, there are scenarios where you might want or need to use different providers.

Scenarios:

  • Local testing vs Production deployment
  • Smart routing (choosing a different provider based on query complexity)
  • Evaluations (batch testing prompts using different model providers)

Article abstract

https://devblogs.microsoft.com/dotnet/ai-vector-data-dotnet-extensions-ga/#portability-across-model-and-vector-store-providers

Expand on this article, specifically these sections.

IChatClient chatClient = 
    environment == "Development"
        ? new OllamaApiClient("YOUR-OLLAMA-ENDPOINT", "qwen3")
        : new AzureOpenAIClient("YOUR-AZURE-OPENAI-ENDPOINT", new DefaultAzureCredential())
            .GetChatClient("gpt-4.1")
            .AsIChatClient();

await foreach (var message in chatClient.GetStreamingResponseAsync("What is AI?"))
{
    Console.Write($"{message.Text}");
};

IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator = 
    environment == "Development"
        ? new OllamaApiClient("YOUR-OLLAMA-ENDPOINT", "all-minilm")
        : new AzureOpenAIClient("YOUR-AZURE-OPENAI-ENDPOINT", new DefaultAzureCredential())
            .GetEmbeddingClient("text-embedding-3-small")
            .AsIEmbeddingGenerator();

var embedding = await embeddingGenerator.GenerateAsync("What is AI?");

Relevant searches

  • How to use multiple model providers with Microsoft.Extensions.AI
  • Working with local and hosted models with Microsoft.Extensions.AI
  • Switch model providers with Microsoft.Extensions.AI

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions