Hello all, I’m currently making a script using ChatGPT’s API to return an answer to a simple prompt for a game in Unity with C#. I have implemented error handling and everytime it returns “Status code: NotFound”. Am I using the wrong endpoint?
Here is my script, thanks everybody.
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class ChatGPT : MonoBehaviour
{
private const string apiKey = "insert_my_key";
private const string apiUrl = "https://api.openai.com/v1/engines/davinci/completions";
public string prompt = "What is the capital of bulgaria?";
private void Start()
{
SendPrompt();
}
public async void SendPrompt()
{
try
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
var content = new StringContent($"{{\"prompt\": \"{prompt}\", \"max_tokens\": 100}}", System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(apiUrl, content);
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
print(result);
}
else
{
Debug.LogError($"Failed to make request. Status code: {response.StatusCode}");
}
}
}
catch (Exception e)
{
Debug.LogError($"Error making request: {e.Message}");
}
}
}