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.38k stars 4.79k forks source link

[BUG] HTTP 404 error when accessing Azure OpenAI Assistant API #45392

Closed rstropek closed 1 month ago

rstropek commented 2 months ago

Library name and version

Azure.AI.OpenAI 2.0.0-beta.2

Describe the bug

If you follow the documentation at https://www.nuget.org/packages/Azure.AI.OpenAI/2.0.0-beta.2#readme-body-tab, you get HTTP 404 (not found) errors when accessing the Azure OpenAI API (e.g. reading all assistants).

Expected behavior

It should be possible to access the Azure OpenAI API without an error.

Actual behavior

HTTP 404 error when accessing the Azure OpenAI API

Reproduction Steps

using Azure;
using Azure.AI.OpenAI;
#pragma warning disable OPENAI001

/* This is what the docs at https://www.nuget.org/packages/Azure.AI.OpenAI/2.0.0-beta.2#readme-body-tab says:

AzureOpenAIClient azureClient = new(
    new Uri("https://your-azure-openai-resource.com"),
    new AzureKeyCredential(keyFromEnvironment));
...
*/

const string endpoint = "https://myendpoint.openai.azure.com";
const string apiKey = "8fd...";

var azureClient = new AzureOpenAIClient(
        new Uri(endpoint),
        new AzureKeyCredential(apiKey));

var openAIClient = azureClient.GetAssistantClient();
await foreach (var assistantPage in openAIClient.GetAssistantsAsync())
{
    foreach (var assistant in assistantPage.Values)
    {
        Console.WriteLine(assistant.Id);
    }
}

If you run this program, you get a System.ClientModel.ClientResultException because of an HTTP 404 error.

In order to make it work, you first have to change the endpoint as follows (note the /openai at the end). Otherwise, the API tries to HTTP GET the assistants from https://myendpoint.openai.azure.com/assistants instead of https://myendpoint.openai.azure.com/openai/assistants. For me, this is not a critical error. It should just be fixed in the docs.

const string endpoint = "https://myendpoint.openai.azure.com/openai";

However, the console app still does not work because the API version (which is mandatory in Azure) is not appended to the URLs. This can be fixed e.g. by adding a custom HttpClientPipelineTransport that changes the request URI accordingly:

...
var azureClient = new AzureOpenAIClient(
        new Uri(endpoint),
        new AzureKeyCredential(apiKey),
        new AzureOpenAIClientOptions() { // <<< this is new
            Transport = new MyTransport()
        });

var openAIClient = azureClient.GetAssistantClient();
await foreach (var assistantPage in openAIClient.GetAssistantsAsync())
{
    ...
}

//  === This is new
class MyTransport : HttpClientPipelineTransport
{
    protected override void OnSendingRequest(PipelineMessage message, HttpRequestMessage httpRequest)
    {
        httpRequest.RequestUri = new Uri(httpRequest.RequestUri!.ToString() + "?api-version=2024-05-01-preview");
        base.OnSendingRequest(message, httpRequest);
    }
}

After these changes, the program works and it can successfully access Azure OpenAI.

This shouldn't be necessary, should it?

Environment

.NET SDK:
 Version:           8.0.301
 Commit:            1a0e9c0300
 Workload version:  8.0.300-manifests.011fccd5
 MSBuild version:   17.10.4+10fbfbf2e

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         linux-x64

.NET workloads installed:
 [aspire]
   Installation Source: SDK 8.0.300
   Manifest Version:    8.0.1/8.0.100

Host:
  Version:      8.0.6
  Architecture: x64
  Commit:       3b8b000a0e

.NET SDKs installed:
  8.0.301 [...]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.6 [/home/rainer/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.6 [/home/rainer/.dotnet/shared/Microsoft.NETCore.App]
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 1 month ago

I can confirm the the problem is gone after upgrading to the new versions of the NuGet packages (Azure.AI.OpenAI version 2.0.0-beta.4 and OpenAI version 2.0.0-beta.10).