-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchat_system.gd
More file actions
195 lines (158 loc) · 5.56 KB
/
chat_system.gd
File metadata and controls
195 lines (158 loc) · 5.56 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
extends Control
#see https://ai.google.dev/tutorials/rest_quickstart
var api_key = ""
var http_request
var conversations = []
var last_user_prompt
var target_model = "v1beta/models/gemini-1.5-flash-latest"
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)
_update_conversation_label()
var option_keys = ["SexuallyExplicit","HateSpeech","Harassment","DangerousContent"]
for key in option_keys:
var option = find_child(key+"OptionButton")
option.add_item("BLOCK_NONE")
option.add_item("HARM_BLOCK_THRESHOLD_UNSPECIFIED")
option.add_item("BLOCK_LOW_AND_ABOVE")
option.add_item("BLOCK_MEDIUM_AND_ABOVE")
option.add_item("BLOCK_ONLY_HIGH")
#conversations.append({"user":"I am aki","model":"Hello aki"})
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _get_option_selected_text(key):
var option = find_child(key+"OptionButton")
var text = option.get_item_text(option.get_selected_id())
return text
func _on_send_button_pressed():
find_child("SendButton").disabled = true
var input = find_child("InputEdit").text
_request_chat(input)
func _request_chat(prompt):
var url = "https://generativelanguage.googleapis.com/%s:generateContent?key=%s"%[target_model,api_key]
print(url)
var contents_value = []
for conversation in conversations:
contents_value.append({
"role":"user",
"parts":[{"text":conversation["user"]}]
})
contents_value.append({
"role":"model",
"parts":[{"text":conversation["model"]}]
})
contents_value.append({
"role":"user",
"parts":[{"text":prompt}]
})
## System Part [] or [{"text":"you are assistant"}]
var system_prompt = find_child("SystemInputEdit").text
var system_parts = []
if system_prompt!="":
system_parts.append({"text":system_prompt})
var body = JSON.new().stringify({
"contents":contents_value
,"system_instruction":{
"parts":system_parts
}
,# basically useless,just they say 'I cant talk about that.'
"safety_settings":[
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": _get_option_selected_text("SexuallyExplicit"),
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": _get_option_selected_text("HateSpeech"),
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": _get_option_selected_text("Harassment"),
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": _get_option_selected_text("DangerousContent"),
},
]
})
last_user_prompt = prompt
print("send-content"+str(body))
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 _set_label_text(key,text):
var label = find_child(key)
if text == "HIGH":
label.get_label_settings().set_font_color(Color(1,0,0,1))
else:
label.get_label_settings().set_font_color(Color(1,1,1,1))
label.text = text
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("promptFeedback"):
var ratings = response.promptFeedback.safetyRatings
for rate in ratings:
match rate.category:
"HARM_CATEGORY_SEXUALLY_EXPLICIT":
_set_label_text("SexuallyExplicitStatus",rate.probability)
"HARM_CATEGORY_HATE_SPEECH":
_set_label_text("HateSpeechStatus",rate.probability)
"HARM_CATEGORY_HARASSMENT":
_set_label_text("HarassmentStatus",rate.probability)
"HARM_CATEGORY_DANGEROUS_CONTENT":
_set_label_text("DangerousContentStatus",rate.probability)
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
conversations.append({"user":"%s"%last_user_prompt,"model":"%s"%newStr})
_update_conversation_label()
func _update_conversation_label():
var label = "%3d conversations"%[conversations.size()]
find_child("ConversationLabel").text = label
var send_label = "Start New Conversation"
if conversations.size()>0:
send_label= "Continue Conversation"
find_child("SendButton").text = send_label
func _on_clear_conversation_button_pressed():
conversations.clear()
_update_conversation_label()
func _update_main_model():
var option = find_child("MainModelOptionButton")
var text = option.get_item_text(option.get_selected_id())
target_model = "v1beta/models/%s"%[text]
func _on_main_model_option_button_item_selected(index):
_update_main_model()