A simple guide to creating a script that can generate text with the OpenAI API!
-
Sign up for an OpenAI account and obtain an API key by visiting this website
-
In the script, replace
YOUR_API_KEY
with your own API key. -
Customize the request body by setting the
model
andprompt
fields to the desired values. You can also adjust themax_tokens
field to control the maximum number of tokens (i.e. words) in the generated text. -
Run the script. It will send a request to the OpenAI API using the specified
model
andprompt
, and then print the generated text.
-- Set the API key and endpoint
local API_KEY = "YOUR_API_KEY"
local API_ENDPOINT = "https://api.openai.com/v1/text-davinci/generations"
-- Set the request body
local requestBody = {
model = "davinci",
prompt = "What is your name?",
max_tokens = 100
}
-- Encode the request body as JSON
local requestBodyJson = HttpService:JSONEncode(requestBody)
-- Create the request headers
local requestHeaders = {
["Content-Type"] = "application/json",
["Authorization"] = "Bearer "..API_KEY
}
-- Send the request and get the response
local response = HttpService:PostAsync(API_ENDPOINT, requestBodyJson, Enum.HttpContentType.ApplicationJson, false, requestHeaders)
-- Parse the response as JSON
local responseJson = HttpService:JSONDecode(response)
-- Print the generated text
print(responseJson.choices[1].text)