dcchill

chatgpt

Feb 26th, 2023 (edited)
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. -- Set up the prompt for ChatGPT to complete
  2. local prompt = "Hello, ChatGPT! What's your favorite color?"
  3.  
  4. -- Set up the headers for the API request
  5. local headers = {
  6. ["Content-Type"] = "application/json",
  7. ["Authorization"] = "Bearer " .. api_key
  8. }
  9.  
  10. -- Set up the request body for the API request
  11. local body = {
  12. prompt = prompt,
  13. max_tokens = 50,
  14. temperature = 0.5,
  15. n = 1
  16. }
  17.  
  18. -- Encode the request body as JSON
  19. local encoded_body = http.encode_json(body)
  20.  
  21. -- Send the API request and receive the response
  22. local response = http.post(endpoint, headers, encoded_body)
  23.  
  24. -- Decode the response body from JSON
  25. local decoded_body = http.decode_json(response.body)
  26.  
  27. -- Extract the completed text from the response
  28. local completed_text = decoded_body.choices[1].text
  29.  
  30. -- Print the completed text to the ComputerCraft terminal
  31. print(completed_text)
Advertisement
Add Comment
Please, Sign In to add comment