1

I am trying to use GPT3 in a game I am making but I can't seem to be able to call the OpenAI API correctly. I got most of this from the Unity docs. Here is the code I am using:

public class gpt3_complete : MonoBehaviour
{
    public string model;
    public string prompt;
    public int len;
    public string temp;
    public string api_key = "<key>";
    void Start()
    {
        StartCoroutine(Upload());
    }

    IEnumerator Upload()
    {
        WWWForm form = new WWWForm();
        form.AddField("model", model);
        form.AddField("prompt", prompt);
        form.AddField("max_tokens", len);
        form.AddField("temperature", temp);
        //form.headers.Add("Authorization", "Bearer "+api_key);



        using (UnityWebRequest www = UnityWebRequest.Post("https://api.openai.com/v1/completions", form))
        {
            www.SetRequestHeader("Authorization", "Bearer " + api_key);
            www.SetRequestHeader("Content-Type", "application/json");

            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log(www.result);
                Debug.Log("Form upload complete!");
            }
        }
    }
}

This always returns: 400 Bad Request. The GPT3 docs can be found here: https://beta.openai.com/docs/api-reference/completions/create

Any idea why this is? This is my first time making any web requests in unity so I'm probably missing something obvious. Thanks!

Dadu Khan
  • 369
  • 2
  • 16

1 Answers1

0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;

public class OpenAIRequest : MonoBehaviour
{
    public string apiKey = "YOUR_API_KEY_HERE";
    public string prompt = "Once upon a time, in a land far far away, there lived a brave knight";
    public string model = "text-davinci-002";
    public int maxTokens = 100;

    void Start()
    {
        StartCoroutine(GetOpenAIResponse());
    }

    IEnumerator GetOpenAIResponse()
    {
        string url = "https://api.openai.com/v1/engines/" + model + "/completions";
        string requestData = "{\"prompt\": \"" + prompt + "\", \"max_tokens\": " + maxTokens + "}";

        UnityWebRequest request = new UnityWebRequest(url, "POST");
        byte[] bodyRaw = Encoding.UTF8.GetBytes(requestData);
        request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
        request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");
        request.SetRequestHeader("Authorization", "Bearer " + apiKey);

        yield return request.SendWebRequest();

        if (request.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(request.error);
        }
        else
        {
            string response = request.downloadHandler.text;
            Debug.Log(response);
        }
    }
}

result

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '23 at 09:22