srcnalt / OpenAI-Unity

An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.
MIT License
683 stars 153 forks source link

Sending base64 encoded Image to GPT-4o via API in Unity #125

Open Zaf01 opened 2 months ago

Zaf01 commented 2 months ago

Hi,

How could I possibly send a base64 encoded image to gpt 4o using the API? Currently for sending text requests and getting responses from ChatGPT I have this function:

//Method to send text requests to chatgpt public async void AskChatGPT(string newText) { debugText.SetText("AskChatGpt called!");

    Debug.Log("AskChatGPT function called with input: " + newText);

    ChatMessage newMessage = new ChatMessage();
    newMessage.Content = newText;
    newMessage.Role = "user";

    messages.Add(newMessage);

    CreateChatCompletionRequest request = new CreateChatCompletionRequest();
    request.Messages = messages;
    request.Model = "gpt-4o";

    var response = await openAI.CreateChatCompletion(request);

    if (response.Choices != null && response.Choices.Count > 0)
    {
        var chatResponse = response.Choices[0].Message;

        messages.Add(chatResponse);

        Debug.Log(chatResponse.Content);

        waitingForResponse = true;
        OnResponse.Invoke(chatResponse.Content);

        Debug.Log("Reponse Finished");

        waitingForResponse = false;
    }
}

I have the images already encoded as base64, but would appreciate some insights on how to feed it to gpt 4o using the APIs and specify what we are sending is an Image data.