![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | TrippleDLee |
I’ve been trying to use chatgpt to create a AI that responds to text and etc. I’ve gotten this far but whenever I run the game I keep getting an 401 error. What am I doing wrong?
var http_request: HTTPRequest
var api_key: String = "MY API KEY"
func _ready():
http_request = HTTPRequest.new()
http_request.request
http_request.request_completed.connect(self._on_request_completed)
add_child(http_request)
var body = JSON.new().stringify({"prompt": "Hello, world!", "max_tokens": 50})
var header = {"Authorization": "Bearer " + api_key,"Content-Type":"application/json"}
http_request.request("https://api.openai.com/v1/chat/completions", [header], HTTPClient.METHOD_POST, body)
func _on_request_completed(result, response_code, headers, body):
if response_code == 200:
var response = JSON.new()
var json_error = response.open_and_parse(body)
if json_error != OK:
print("JSON parsing error:", json_error)
else:
var generated_text = response.get_var("choices/0/text")
print("Generated text:", generated_text)
else:
print("Request failed with code:", response_code)