microsoft / semantic-kernel

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

.Net - Request ability to configure api-version when using KernelBuilder #2833

Closed jenguriel closed 6 months ago

jenguriel commented 1 year ago

[!IMPORTANT]
Labeled High because it will not require a breaking change, but it's very important to complete by v1.0.0

Description .NET version of Kernel Builder allows for setting of model, endpoint and api key but does not provide a way to for configure api version. My Azure OpenAI sub requires a specific version number so without this change, I cannot use SK!

NOTE: I am working on a PR that would allow for (optional) setting of api version. I will associate it with this ticket when ready.

evchaki commented 11 months ago

@jenguriel - please tag your PR once you have it started!

luismanez commented 8 months ago

Just stressing how important this is, as today got an email from MS saying that some api-versions are gonna be deprecated in April. I cannot change my SK version, as have some conflicts with dependencies, so would need to upgrade my entire solution.

matthewbolanos commented 8 months ago

I've bumped this up in priority. In the meantime though, @markwallace-microsoft, do you know if it'd be possible to override the api-version by overriding headers like what some folks have done to support different endpoints?

jenguriel commented 8 months ago

I paused work on the PR because I found a workaround that, while not quite as pretty, works fine. I would suggest closing this issue or working it into your planned interface updates, if you want to be more user-friendly.

Here's a sample of the workaround:

var apiKey = System.Environment.GetEnvironmentVariable(KEY_NAME);
var options = new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2023_05_15);
var openAIClient = new OpenAIClient(new Uri(API_ENDPONT), new AzureKeyCredential(apiKey), options);

_kernelBuilder.WithAzureChatCompletionService(
      "GPT4",
       openAIClient
);
luismanez commented 8 months ago

Thanks @jenguriel the problem with that approach, is that the value has to be a valid value of the ServiceVersion enum. In my case, won't work, as my 1.0-beta version has not the latest value.

matthewbolanos commented 8 months ago

@jenguriel, thanks for sharing! I'll keep this issue open so we can make it easier to adjust with configuration.

@luismanez, can you share more about which dependencies are preventing you from being able to upgrade?

luismanez commented 8 months ago

@matthewbolanos I have a (pretty big) modular monolith in .net core 6 (including Az Functions v4 in-proc mode). So, for instance in Microsoft.Extensions.DependencyInjection.Abstractions we have:

Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)

I'm stuck with 6.0.0 versions.

Having this, let's make a step back. I'm using Semantic.Kernel 1.0.0-beta1. This version is setting the api-version querystring value to 2023-09-01-preview. The email from MS says the deprecated versions (April) will be:

My version is not in there, however 2023-09-15-preview is more recent than mine, and will be deprecated. Also, the email points to this URL: https://review.learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation?branch=pr-en-us-256331

and in there, there are less versions in the deprecated list (2023-09-15-preview is not in the website, but IS in the email). It is very confusing.

Anyone from MS can confirm the list of the deprecated versions? this will be really helpful, as maybe SK 1.0.0-beta1 is not being impacted.

Thanks a lot!

luismanez commented 8 months ago

@matthewbolanos still interested in confirmation of the deprecated versions.

In the meantime, this is another approach that is working for me using SK 1.0.0-beta1.

I've created a custom Delegated that checks the URL, and if the api-version querystring is set, overwrites the value:

public class ApiVersionHandler : DelegatingHandler
{
    private const string ApiVersionKey = "api-version";
    private const string NewApiVersion = "2023-12-01-preview";

    public ApiVersionHandler() : base(new HttpClientHandler())
    {

    }

    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, 
        CancellationToken cancellationToken)
    {
        var uriBuilder = new UriBuilder(request.RequestUri!);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);

        if (query[ApiVersionKey] == null) return await base.SendAsync(request, cancellationToken);

        query[ApiVersionKey] = NewApiVersion;
        uriBuilder.Query = query.ToString();
        request.RequestUri = uriBuilder.Uri;

        return await base.SendAsync(request, cancellationToken);
    }
}

Then, I create a new HttpClient using that custom handler. This code is just a sample (it's not recommended to create an HttpClient in this way, mi prod code is using asp.net HttpClientFactory)

var client = new HttpClient(new ApiVersionHandler());

finally, you pass the custom HttpClient to the Builder (I'm using SK 1.0.0-beta1)

var kernel = new KernelBuilder()
    .WithAzureChatCompletionService(
        "model",
        "endpoint",
        "key"
        ,httpClient: client
        )
    .Build();
markwallace-microsoft commented 6 months ago

All .Net issues prior to 1-Dec-2023 are being closed. Please re-open, if this issue is still relevant to the .Net Semantic Kernel 1.x release. In the future all issues that are inactive for more than 90 days will be labelled as 'stale' and closed 14 days later.

mohammedtabish0 commented 6 months ago

@matthewbolanos any updates on this? Is there a workaround in SK 1.5.0 or is there any ticket which I could track for this?

mohammedtabish0 commented 6 months ago

@luismanez I see that the issue isn't on the Semantic Kernel side but rather on the Azure.AI.OpenAI dependency. I am not sure but if the enum doesn't hold the value then SK connot provide this functionality https://github.com/Azure/azure-sdk-for-net/blob/Azure.AI.OpenAI_1.0.0-beta.14/sdk/openai/Azure.AI.OpenAI/src/Generated/OpenAIClientOptions.cs

luismanez commented 6 months ago

I see. Makes sense. However, they could do something internal with the HttpClient, as my workaround is doing from the "outside".

I don't know. They (SK, OpenAI SDK) should provide something, cos the versions are changing too fast.