Advertisement
A_GUES

Lua ChatGPT

Jul 4th, 2023 (edited)
954
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 1 0
  1. -- Lua
  2. http = require("socket.http")
  3. json = require("json")
  4. ltn12 = require("ltn12")
  5. http.TIMEOUT = 5
  6.  
  7. local response_body = {}
  8.  
  9. -- Set up your OpenAI API credentials
  10. local headers = {
  11.   ["Content-Type"] = "application/json",
  12.   ["Authorization"] = "Bearer YOUR_API_KEY"
  13. }
  14.  
  15. -- The data to send in the POST request
  16. local data = {
  17.   engine = "text-davinci-003",  -- Specify GPT-3.5 engine
  18.   prompt = "Once upon a time",
  19.   max_tokens = 100  -- Set the maximum number of tokens in the response
  20. }
  21.  
  22. -- Convert the data to JSON
  23. local json_data = json.encode(data)
  24.  
  25. -- Send the POST request
  26. local response, status, headers_res = http.request {
  27.   url = "https://api.openai.com/v1/completions",
  28.   method = "POST",
  29.   headers = headers,
  30.   source = ltn12.source.string(json_data),
  31.   sink = ltn12.sink.table(response_body)
  32. }
  33.  
  34. -- Parse the response
  35. local response_json = json.decode(table.concat(response_body))
  36.  
  37. -- Print the generated text
  38. print(response_json["choices"][1]["text"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement