Skip to content

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

Description

@Zaf01

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions