Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Lua
- http = require("socket.http")
- json = require("json")
- ltn12 = require("ltn12")
- http.TIMEOUT = 5
- local response_body = {}
- -- Set up your OpenAI API credentials
- local headers = {
- ["Content-Type"] = "application/json",
- ["Authorization"] = "Bearer YOUR_API_KEY"
- }
- -- The data to send in the POST request
- local data = {
- engine = "text-davinci-003", -- Specify GPT-3.5 engine
- prompt = "Once upon a time",
- max_tokens = 100 -- Set the maximum number of tokens in the response
- }
- -- Convert the data to JSON
- local json_data = json.encode(data)
- -- Send the POST request
- local response, status, headers_res = http.request {
- url = "https://api.openai.com/v1/completions",
- method = "POST",
- headers = headers,
- source = ltn12.source.string(json_data),
- sink = ltn12.sink.table(response_body)
- }
- -- Parse the response
- local response_json = json.decode(table.concat(response_body))
- -- Print the generated text
- print(response_json["choices"][1]["text"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement