Im trying to add GPT-3 for a game Im making. This is the current code:
extends Control
var user_name = 'Player'
var GPT3 = 'GPT-3'
var text
var input = text
onready var chatLog = get_node("VBoxContainer/RichTextLabel")
onready var inputLabel = get_node("VBoxContainer/HBoxContainer/Label")
onready var inputField = get_node("VBoxContainer/HBoxContainer/LineEdit")
var api_key = "API-KEY HIDDEN"
var api_url = "https://api.openai.com/v1/engines/davinci/jobs"
var request = null
var response = null
func _ready():
inputField.connect("text_entered", self,'text_entered')
inputField.connect("text_changed", self, "_on_input_changed") #GPT3
func add_message(username, text, group = 0, color = ''):
chatLog.bbcode_text += '\n'
if username != '':
chatLog.bbcode_text += '[' + username + ']: '
chatLog.bbcode_text += text
func _make_post_request(url, data_to_send, use_ssl):
var endpoint = "https://api.openai.com/endpoint?input=" + input + "&api_key=" + api_key
var query = JSON.print(input)
var headers = ["Content-Type: application/json"]
$HTTPRequest.request(endpoint, headers, true, HTTPClient.METHOD_POST, query)
func text_entered(text):
var endpoint = "https://api.openai.com/endpoint?input=" + input + "&api_key=" + api_key
var request = HTTPRequest.new()
var query = JSON.print(input)
if text != '':
_make_post_request(endpoint,query,true)
add_message(user_name, text)
print(input)
request = HTTPRequest.new()
var headers = ["Content-Type: application/json"]
#request.set_request_headers(headers)
#request.set_body(JSON.print(input))
request.request(endpoint, headers, HTTPClient.METHOD_POST,0, query)
request.connect("request_completed", self, "_on_request_completed")
print(request)
inputField.text = ''
func _on_request_completed(result, response_code, headers, body):
if result == HTTPRequest.RESULT_SUCCESS:
print("Request successful! Response code: ", response_code)
response = JSON.parse(body)
print("Response: ", response)
else:
print("Request failed: ", response_code)
There is an input field for the user to input text and an output field. Everything besides the user being able to type text is broken (because I copied the code and deleted the useless stuff).
This is my logic/idea behind it:
1- User inputs text into field
2- Text gets saved as a variable
3- Text (which is now a variable) gets sent
4- Response gets received and printed into the output
The complete script is written in the ChatBox

Images:


The source for the chatbox: https://github.com/coppolaemilio/godot-chat-box