我尝试在我制作的游戏中使用GPT 3,但我似乎无法正确调用OpenAI API。我从Unity文档中获得了大部分信息。以下是我使用的代码:
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!");
}
}
}
}
这始终返回:400错误请求。GPT 3文档可以在这里找到:https://beta.openai.com/docs/api-reference/completions/create
你知道为什么吗?这是我第一次在unity上做任何网络请求,所以我可能错过了一些明显的东西。谢谢!
1条答案
按热度按时间aor9mmx11#
result