openai / openai-dotnet

The official .NET library for the OpenAI API
https://www.nuget.org/packages/OpenAI
MIT License
1.54k stars 156 forks source link

GPT Vision request not working (Error 500 from API) #260

Open KRIShark opened 1 month ago

KRIShark commented 1 month ago

Service

OpenAI

Describe the bug

Receiving a 500 error when using the OpenAI API to create image descriptions (Vision) in C#. The error occurs on the line making the API call using model "gpt-4o-mini". The code fails when sending a request with an image part, leading to an internal server error.

Error message: The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error

Steps to reproduce

Where the text is bold, the error will occur.

  1. Load an image from the local drive and convert it to a byte array.
  2. Create a list of chat messages, including system instructions and a user message containing both text and an image.
  3. Utilize the GPT-4o-mini Model and submit a request to the OpenAI API to request a response based on the provided messages.
  4. Output the assistant's response to the console.

Code snippets

async Task TestGPT()
{
    //Convert image to byte array
    var imageJpg = "C:\\Users\\<myPath>";
    byte[] image = File.ReadAllBytes(imageJpg);

    List<ChatMessage> messages = new List<ChatMessage>() 
    {
        new SystemChatMessage("Describe, briefly, the image."),
        new UserChatMessage(ChatMessageContentPart.CreateTextPart("Hello, what can you see on the image"), ChatMessageContentPart.CreateImagePart(new BinaryData(image),"image/jpeg", ChatImageDetailLevel.Low))
    };

    ChatCompletion res = await _client.GetChatClient("gpt-4o-mini").CompleteChatAsync(messages , null);

    Console.WriteLine($"Asystent: {res.Content[0].Text}");

    Console.ReadKey();
}

OS

windows 11

.NET version

.NET 8

Library version

2.0.0

joseharriaga commented 1 month ago

Thank you for reaching out, @KRIShark ! Error code 500 suggests that the service was having an issue and not that there was something wrong with your code. Are you still having this issue by any chance?

KRIShark commented 1 month ago

I found out that the issue was that the prompt contained an image and the options to set some parameters, and if both were given, then the error 500 occurred. I tested it now with both, and now it is working. So it may have been an issue with the OpenAI API.