How do I add ChatGPT to Roblox by script?

I’ve searched everywhere, but I don’t find anything. Could you help me? (I already have my API key)

4 Likes

In this code I was using davinci-003 model, there are other models be sure to check them, are different and requires different price of tokens, like curie, babbage, ada etc

local HttpService = game:GetService("HttpService")
local endpoint = "https://api.openai.com/v1/engines/text-davinci-003/completions"
local yourKEY = "Bearer yourAPI_Key"

local function getResponse(prompt)
	local requestBody = {
		prompt = prompt,
		max_tokens = 100,
		n = 1,
		--stop = "\n",
		temperature = 1 -- play with these values
		top_p = 1 -- play with these values
	}
	local headers = {
		["Authorization"] = yourKEY,
	}
	local response = HttpService:PostAsync(endpoint, HttpService:JSONEncode(requestBody), Enum.HttpContentType.ApplicationJson, false, headers)
	local decodedResponse = HttpService:JSONDecode(response)
	warn("Stop reason:", decodedResponse.choices[1].finish_reason)
	warn("Total Tokens required:") --, decodedResponse.usage.total_tokens
	print(decodedResponse.usage)
	return decodedResponse.choices[1].text
end

Its free for only 3 months or tokens limit reached as I remember.
You could ask ChatGPT itself too, I suppose “it” knows more about “it” :face_with_hand_over_mouth:

5 Likes

Thanks!!! It worked perfectly.

1 Like

So this is Chat GPT but inside Roblox Studio ?

Yes, works exactly the same as ChatGPT from a browser, and by using davinci model it behaves exactly like the browser one. But its only free for a limit of “words/tokens”, which you can control with the requestBody parameters

Nice. Never heard of it it until now. Thanks for telling.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.