-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchat_moa.gd
More file actions
308 lines (251 loc) · 10.1 KB
/
chat_moa.gd
File metadata and controls
308 lines (251 loc) · 10.1 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
extends Control
#see https://ai.google.dev/tutorials/rest_quickstart
var api_key = ""
var http_request
var conversations = []
var last_user_prompt
var main_model = "v1beta/models/gemini-1.5-pro-latest"
# TODO save external
var REFERENCE_SYSTEM_PROMPT = """\
You have been provided with a set of responses from various open-source models to the latest user query.
Your task is to synthesize these responses into a single, high-quality response.
It is crucial to critically evaluate the information provided in these responses, recognizing that some of it may be biased or incorrect.
Your response should not simply replicate the given answers but should offer a refined, accurate, and comprehensive reply to the instruction.
Ensure your response is well-structured, coherent, and adheres to the highest standards of accuracy and reliability.
Responses from models:
{responses}
"""
class Agent:
var model = "v1beta/models/gemini-1.5-flash-latest"
var system_prompt = ""
var agents = []
var messages_option_button_dic ={}
func update_main_model():
var option = find_child("MainModelOptionButton")
var text = option.get_item_text(option.get_selected_id())
main_model = "v1beta/models/%s"%[text]
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_main_model()
#find_child("LineEdit1").text = "You are a writer exploring the concept of artificial intelligence and its potential to change our world."
#find_child("LineEdit2").text = "You are a software developer creating an AI assistant that can help people learn a new skill."
#find_child("LineEdit3").text = "You are a painter creating a surreal landscape that represents the inner workings of a human mind."
#find_child("LineEdit4").text = "You are a powerful sorcerer in a medieval fantasy realm, tasked with crafting a unique spell to protect your village from a looming threat."
#find_child("LineEdit5").text = "You are a celestial being observing the rise and fall of civilizations across countless galaxies, reflecting on the nature of life, death, and the universe"
#find_child("LineEdit6").text = "You are a time traveler who accidentally swapped bodies with a grumpy old cat. Describe your daily life and your attempts to get back to your own body."
#find_child("LineEdit7").text = "You are a scientist contemplating the possibility of life on other planets. What are the biggest challenges and potential solutions in the search for extraterrestrial life?"
#
#print("append %s"%agent.system_prompt)
#break
# for safety filter
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")
#var name = main_model.split("/")[-1]
#find_child("ModelName").text = name
#conversations.append({"user":"I am aki","model":"Hello aki"})
# Called every frame. 'delta' is the elapsed time since the previous frame.
func set_up_agent():
agents.clear()
var agent_container = find_child("AgentContainer")
for child in agent_container.get_children():
if child is LineEdit:
var text = child.text
#print(text)
if text == "": # end
break
var agent = Agent.new()
agent.system_prompt = text
agents.append(agent)
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 _add_messages_option_button(label,select=true):
var option = find_child("MessagesOptionButton")
option.add_item(label)
if select:
option.select(option.item_count-1)
func _on_send_button_pressed():
set_up_agent()
conversations = []
messages_option_button_dic.clear()
find_child("MessagesOptionButton").clear()
find_child("SendButton").disabled = true
var input = find_child("InputEdit").text
#_request_chat(input)
var cycle = find_child("CycleOptionButton").get_selected_id()
print(cycle)
var final_system_prompt = ""
for i in range(cycle):
print("cycle %s"%i)
var cycle_conversations = []
#var responses = []
for agent in agents:
print("[%s]"%agent.system_prompt)
var system = agent.system_prompt + "\n\n"+final_system_prompt
var error = _request_chat(agent.model,input,system)
if error != OK:
push_error("requested but error happen code = %s"%error)
return
# TODO support thread
var res = await http_request.request_completed
#var result, responseCode, headers, body = res
var response = _get_response_text(res[0],res[1],res[2],res[3])
find_child("TextEditA").text = response
find_child("TextEditB").text = system
var max_text = 80
var label = "Cycle %02d:%s"%[i,agent.system_prompt.substr(0,max_text)]
_add_messages_option_button(label)
var dic = {"user":input,"model":response,"system":system}
messages_option_button_dic[label] = dic
cycle_conversations.append(dic)
var response_text = ""
for j in range(cycle_conversations.size()):
var index = j+1
var current_response = cycle_conversations[j]["model"]
response_text += "<Assistant%s>\n%s\n</assistant%s>\n\n"%[index,current_response,index]
conversations.append(cycle_conversations[j])
final_system_prompt = REFERENCE_SYSTEM_PROMPT.replace("{responses}",response_text)
print("[Final]")
var error_main = _request_chat(main_model,input,final_system_prompt)
if error_main != OK:
push_error("requested but error happen code = %s"%error_main)
return
var res_main = await http_request.request_completed
var main_response = _get_response_text(res_main[0],res_main[1],res_main[2],res_main[3])
find_child("ResponseEdit").text = main_response
find_child("SendButton").disabled = false
var label = "Final"
_add_messages_option_button(label,false)
var dic = {"user":input,"model":main_response,"system":final_system_prompt}
messages_option_button_dic[label] = dic
#conversations.append(dic) #not add re submit yet
func _request_chat(model_name,prompt,system_prompt=""):
var url = "https://generativelanguage.googleapis.com/%s:generateContent?key=%s"%[model_name,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"]}]
})
var system_parts = []
if system_prompt!="":
pass
system_parts.append({"text":system_prompt})
contents_value.append({
"role":"user",
"parts":[{"text":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")
print(str(body))
var error = http_request.request(url, ["Content-Type: application/json"], HTTPClient.METHOD_POST, body)
return 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):
pass
func _get_response_text(result, responseCode, headers, body):
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
return newStr
#find_child("ResponseEdit").text = newStr
#conversations.append({"user":"%s"%last_user_prompt,"model":"%s"%newStr})
func _on_messages_option_button_item_selected(index):
var text = find_child("MessagesOptionButton").get_item_text(index)
var dict = messages_option_button_dic[text]
var response = dict["model"]
find_child("TextEditA").text = response
var system = dict["system"]
find_child("TextEditB").text = system
func _on_main_model_option_button_item_selected(index):
update_main_model()