-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsend_text.gd
More file actions
83 lines (66 loc) · 2.22 KB
/
send_text.gd
File metadata and controls
83 lines (66 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
extends Control
#see https://ai.google.dev/tutorials/rest_quickstart
var api_key = ""
var http_request
func _ready():
var settings = JSON.parse_string(FileAccess.get_file_as_string("res://settings.json"))
api_key = settings.api_key
http_request = HTTPRequest.new()
add_child(http_request)
http_request.connect("request_completed", _on_request_completed)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _on_send_button_pressed():
var input = find_child("InputEdit").text
_request_text(input)
func _request_text(prompt):
var url = "https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=%s"%api_key
var body = JSON.new().stringify({
"contents":[
{ "parts":[{
"text": prompt
}]
}
]
})
print("send-content"+str(body))
find_child("SendButton").disabled = true
var error = http_request.request(url, ["Content-Type: application/json"], HTTPClient.METHOD_POST, body)
if error != OK:
push_error("requested but error happen code = %s"%error)
func _on_request_completed(result, responseCode, headers, body):
find_child("SendButton").disabled = false
var json = JSON.new()
json.parse(body.get_string_from_utf8())
var response = json.get_data()
print("response")
print(response)
if response == null:
print("response is null")
find_child("FinishedLabel").text = "No Response"
find_child("FinishedLabel").visible = true
return
if response.has("error"):
find_child("FinishedLabel").text = "ERROR"
find_child("FinishedLabel").visible = true
find_child("ResponseEdit").text = str(response.error)
#maybe blocked
return
#No Answer
if !response.has("candidates"):
find_child("FinishedLabel").text = "Blocked"
find_child("FinishedLabel").visible = true
find_child("ResponseEdit").text = ""
#maybe blocked
return
#I can not talk about
if response.candidates[0].finishReason != "STOP":
find_child("FinishedLabel").text = "Safety"
find_child("FinishedLabel").visible = true
find_child("ResponseEdit").text = ""
else:
find_child("FinishedLabel").text = ""
find_child("FinishedLabel").visible = false
var newStr = response.candidates[0].content.parts[0].text
find_child("ResponseEdit").text = newStr