DI with IChatCompletionService with multiple providers #9493
-
I have a use case where the user would like to choose either OpenAI or AzureOpenAI or Ollama and then Chat with that ChatCompletionService. In Program.cs I have now using DI "IChatCompletionService chatCompletionService" how can I get the correct chatCompletionService based on what the user has selected (AzureOpenAI or OpenAI or Ollama)? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
@saroshwadia - take a look at this notebook - https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks/00-getting-started.ipynb you can see the multiple services and you could have the user provide input on the one they want to use and then enable the service based on that input |
Beta Was this translation helpful? Give feedback.
-
We also have a Demonstration on how to use and route between different Connectors and Models with Semantic Kernel.
|
Beta Was this translation helpful? Give feedback.
-
Finally used (DI) keyed services and did the following: (serviceId:) builder.Services.AddAzureOpenAIChatCompletion(
deploymentName: builder.Configuration["AzureOpenAI:DeploymentName"]!,
endpoint: builder.Configuration["AzureOpenAI:Endpoint"]!,
apiKey: builder.Configuration["AzureOpenAI:ApiKey"]!,
serviceId: "Azure");
builder.Services.AddOpenAIChatCompletion(
modelId: builder.Configuration["OpenAI:ChatModel"]!,
apiKey: builder.Configuration["OpenAI:ApiKey"]!,
serviceId: "OpenAI");
builder.Services.AddOllamaChatCompletion(
modelId: builder.Configuration["OllamaAI:ChatModel"]!,
endpoint: new Uri(builder.Configuration["OllamaAI:Endpoint"]!),
serviceId: "Ollama");
// Then injected IServiceProvider serviceProvider
private string selectedService = "OpenAI";
private IChatCompletionService? chatCompletionService { get; set; }
// and used
var chatCompletionService = serviceProvider.GetRequiredKeyedService<IChatCompletionService>(selectedService);
var response = chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory: history); Please let me know if there is a better/cleaner/faster way to do this. |
Beta Was this translation helpful? Give feedback.
Finally used (DI) keyed services and did the following: (serviceId:)