local HttpService = game:GetService("HttpService")
local API_KEY = "blanked out for privacy"
local API_URL = "https://api.openai.com/v1/engines/davinci/completions"

function generateText(prompt, length)
	local requestBody = HttpService:JSONEncode({
		prompt = prompt,
		length = length,
		temperature = 0.7,
	})

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

	local response = HttpService:PostAsync(API_URL, requestBody, headers)
	local responseData = HttpService:JSONDecode(response)

	return responseData.choices[1].text
end

local generatedText = generateText("Once upon a time,", 50)
print(generatedText)

5 comments

0

any error messages?

0

Yes. I got "unable to cast dictionary to token"

0

At what line?

0

nvm, it's because PostAsync is given the wrong data - You need to do HttpService:PostAsync(API_URL, requestBody, Enum.HttpContentType.ApplicationJson, false, headers)

0

I got this error: Header "Content-Type" is not allowed! - Server - Script:17