Trying to access ChatGPT API

  1. What do you want to achieve? I am trying to access the ChatGPT API just for experimental purposes of communicating with an AI from a Roblox game, ask it questions and stuff.

  2. What is the issue? I’m not sure how to really do that… This is my current code, but I’m getting an error:

local HTTPService = game:GetService("HttpService")

local API_Key = "not-sharing-my-key"
local URL = "https://api.openai.com/v1/completions/"..API_Key

local Data = {
	["model"] = "text-davinci-003",
	["prompt"] = "Say this is a test",
	["temperature"] = 0,
	["max_tokens"] = 7
}

HTTPService:GetAsync(URL) -- Line the error occurs on.

image

Edit: I also looked in their API docs but only Python code snippets were provided and Python has way different syntax and possibilities than Luau so I wasn’t able to make anything from it.

5 Likes

I don’t think that lua is the supported language

1 Like

Most capable doesn’t mean it’s not possible, where did you screenshot this?

2 Likes

I just typed “OpenAi api supported languages”

1 Like

And as well as in over a dozen languages including….

1 Like

You need to POST, not GET. Your setup is also wrong–you need to provide headers for your API key and not put at the end of the URL. Your data is also unused.

5 Likes

I ended up with this code:

local HTTPService = game:GetService("HttpService")

local API_Key = "no"
local URL = "https://api.openai.com/v1/completions/"..API_Key

local Data = {
	["model"] = "text-davinci-003",
	["prompt"] = "Say this is a test",
	["temperature"] = 0,
	["max_tokens"] = 7
}

local Header = {
	["Content-Type"] = "application/json",
	["Authorization"] = "Bearer "..API_Key
}

HTTPService:PostAsync(URL, Data, Enum.HttpContentType.ApplicationJson, false, Header)

But apparently, header Content-Type isn’t allowed. What do I do?
image

1 Like

You don’t need to set Content-Type as a header, that’s why you’re specifying HttpContentType. The error message on that could be better.

You also need to remove the .. API_KEY from the completions URL.

4 Likes

Another thing, I think you’ll need to remove the slash on the end of the URL as it returns invalid otherwise.

1 Like

This is correct although i do believe that the api call wont work at least I’ve tried…if u get it make sure to post code here :slight_smile:

2 Likes

I’ve tried and I can confirm it works. Can you elaborate on what do you mean by “won’t work”?

1 Like

You’ll most likely need a node.js wrapper for this instead. OpenAI’s requests take too long to process for roblox and you’ll get timedout, lol

1 Like

I have a chatgpt AI npc script I can send you once I’m home.

2 Likes

If anyone is still looking for this I’ve made code that should work. I just don’t have any credits on my account :pensive:

-- Types
type body = {
	model: string,
	prompt: string,
	temperature: number,
	max_tokens: number,
	top_p: number?,
	frequency_penalty: number?,
	presence_penalty: number?
}

type headers = {
	[string]: string
}

-- Services
local httpSerivce = game:GetService("HttpService")

-- Important
local authorization: string = "YOUR_OPEN_AI_API_KEY_HERE"

local headers: headers = {
	["Content-Type"] = "application/json",
	["Authorization"] = `Bearer {authorization}`
}

-- Change this to your liking.
local body: body = {
	["model"] = "text-davinci-003",
	["prompt"] = "Write a tagline for an ice cream shop",
	["temperature"] = 0.4,
	["max_tokens"] = 64,
	["top_p"] = 1, -- Optional
	["frequency_penalty"] = 0, -- Optional
	["presence_penalty"] = 0 -- Optional
}

-- Use the information from this request. Make sure to decode the JSON body.
local request = httpSerivce:RequestAsync({
	["Url"] = "https://api.openai.com/v1/completions",
	["Method"] = "POST",
	["Headers"] = headers,
	["Body"] = httpSerivce:JSONEncode(body)
})
4 Likes

you should totally post this on #resources:community-resources

1 Like

I’ll see. I think there’s already some posted so this is a nice place to post it where people with issues in their code probably come.

1 Like

I think the error is because there is no endpoint you want to access within api…

2 Likes

this is amazing. Do you perhaps have a tiny tutorial, or a step by step guide how to start? Where to use the code, how to run it and how to setup a everything from the start till the GUI, including prompting with chatGPT?

thank you sooo much, this is very very cool!

1 Like

I’m unable to really test that because I don’t have any OpenAI credit but all you really have to do is listen to a remote function from the client, add some sort of rate limiting and using their prompts that you received from the remote function to call the API.

1 Like