Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.48k stars 4.81k forks source link

[QUERY] How to change the API version with Azure.AI.OpenAI? #45893

Open rstropek opened 2 months ago

rstropek commented 2 months ago

Library name and version

Azure.AI.OpenAI 2.0.0-beta.5

Query/Question

It is a common scenario to switch between API versions (e.g. testing the structured outputs feature which requires API version 2024-08-01-preview). How can I switch the API version with Azure.AI.OpenAI? Other SDKs (e.g. Python) have an easy-to-use parameter for that.

Environment

.NET 8, C#

github-actions[bot] commented 2 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jpalvarezl @ralph-msft @trrwilson.

rstropek commented 2 months ago

FYI - this is my workaround. Is this the intended way to go?

var azureClient = new AzureOpenAIClient(
        new Uri(options.Endpoint),
        new AzureKeyCredential(options.ApiKey ?? configuration["AZURE_OPENAI_KEY"]!),
        new AzureOpenAIClientOptions() {
            Transport = new ApiVersionSelectorTransport("2024-08-01-preview")
        });

// ...

class ApiVersionSelectorTransport(string apiVersion) : HttpClientPipelineTransport
{
    protected override void OnSendingRequest(PipelineMessage message, HttpRequestMessage httpRequest)
    {
        var uriBuilder = new UriBuilder(httpRequest.RequestUri!);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);
        query["api-version"] = apiVersion;
        uriBuilder.Query = query.ToString();
        httpRequest.RequestUri = uriBuilder.Uri;

        base.OnSendingRequest(message, httpRequest);
    }
}