#Unity GPT3 Error: HTTP/1.1 415 Unsupported Media Type

25 messages · Page 1 of 1 (latest)

teal silo
#

I have been trying to call openapi but i am getting hit with this error,I'm not sure what is wrong with my payload.

I have tried to rewrite my code but I'm still not sure why this occurs

Error: HTTP/1.1 415 Unsupported Media Type

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class GPT3API : MonoBehaviour
{
    private string openaiApiKey = "MYKEY;
    private string model = "text-davinci-002";
    public string prompt = "Hello";
    public int maxTokens = 100;
    public void Start()
    {
        
    }

    public void callGPT3()
    {
        StartCoroutine(GetGPT3Response());
    }

    private IEnumerator GetGPT3Response()
    {
        string url = "https://api.openai.com/v1/engines/" + model + "/completions";

        // Create a JSON payload with the desired API parameters
        string payload = "{\"prompt\":\"" + prompt + "\", \"max_tokens\": " + maxTokens + "}";
        using (UnityWebRequest www = UnityWebRequest.Post(url, payload))
        {
            // Set the API authorization header using your API key
            www.SetRequestHeader("Authorization", "Bearer " + openaiApiKey);

            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log("Error: " + www.error);
            }
            else
            {
                string response = www.downloadHandler.text;
                Debug.Log("Response: " + response);
            }
        }
    }
}
#

This is in unity :(

wary glade
#

Your payload doesn't contain the model parameter, this is needed

#

you're using an outdated endpoint

teal silo
#

ah

#

i changed it to this and the error became

#
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class GPT3API : MonoBehaviour
{
    private string openaiApiKey = "sk-MYKEY";
    private string model = "text-davinci-002";
    public string prompt = "hello";
    public int maxTokens = 2048;
    public void Start()
    {
        
    }

    public void callGPT3()
    {
        StartCoroutine(GetGPT3Response());
    }

    private IEnumerator GetGPT3Response()
    {
        string url = "api.openai.com/v1/completions";

        // Create a JSON payload with the desired API parameters
        string payload = "{\"prompt\":\"" + prompt + "\", \"max_tokens\": " + maxTokens + "\", \"model\": " + model + "\"}";
        Debug.Log(payload);
        using (UnityWebRequest www = UnityWebRequest.Post(url, payload))
        {
            // Set the API authorization header using your API key
            www.SetRequestHeader("Authorization", "Bearer " + openaiApiKey);

            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log("Error: " + www.error);
            }
            else
            {
                string response = www.downloadHandler.text;
                Debug.Log("Response: " + response);
            }
        }
    }
}
#

would you happen to know whats erorring with that now?

#

Been stuck on this for ages D:

wary glade
#

403 forbidden looks like it's using the wrong API key

#

are you sure that you have the correct API key entered?

teal silo
#

i think so

#

im using whatever popped up here

wary glade
#

It would be helpful to print the error message returned by the API

#

When it returns a 403 error, it also returns a JSON response that has a string of plaintext english that explains what's wrong

#

Can you try printing that out somehow?

teal silo
#

uhhh im not too sure how to do that, i can try in unity

wary glade
#

Yeah, it would be super helpful to figure out what's wrong

teal silo
#

yeah im not sue how to get that printed :(

mellow salmon
#

Try printing www.result

teal silo
#

i have a new error now :(