microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.9k stars 3.26k forks source link

Net: New Feature: serviceId to named httpclient option in IChatCompletionService #8497

Closed cisionmarkwalls closed 2 months ago

cisionmarkwalls commented 2 months ago

It would be really useful to be able to define a named HttpClient based on the serviceId of the IChatCompletionService (like the httpclient parameter for the kernel). That way I could have specialized http clients for different types of completion services. If I have one type of request that takes 30 seconds on average then I don't want to up the timeout on all http client's, just the one that would be pulled in by that service.

I'd be happy to write the code for this, it would be fairly minor and left as an optional parameter, but I'm curious if that matches design goals before I try to build it.

markwallace-microsoft commented 2 months ago

Hi @cisionmarkwalls thanks for creating this issue and the offer to create a contribution. The requirement makes sense to me, I'll discuss with the team today.

RogerBarreto commented 2 months ago

Hi @cisionmarkwalls Thanks for reaching out, currently as this is a very specific requirement and our APIs currently allow you to do that with a simple Extension method implemented by the customer. We would appreciate as a contribution for that to be a helper on our Concepts or Demonstration projects.

Following some of the code below you might be able to achieve the requirement.

DependencyInjection - NamedHttpClient Registration

public static class OpenAIExtension
{
    public static IServiceCollection AddOpenAIChatCompletionService(this IServiceCollection services, string serviceId)
    {
        services.AddTransient<IChatCompletionService>((sp) => {
            var clientFactory = sp.GetRequiredService<IHttpClientFactory>();
            return new OpenAIChatCompletionService("model-id", "api-key", httpClient: clientFactory.CreateClient(serviceId));
        });

        return services;
    }
}