Spaces:
Runtime error
Runtime error
module
Browse files- bots/debate_bot.py +15 -399
- bots/normal_debate.py +397 -0
bots/debate_bot.py
CHANGED
@@ -3,407 +3,23 @@ import random
|
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from modules.gpt_modules import gpt_call
|
5 |
|
6 |
-
|
7 |
-
def erase_start_word_and_after(text, start_word):
|
8 |
-
pattern = re.compile(re.escape(start_word) + '.*')
|
9 |
-
return re.sub(pattern, '', text)
|
10 |
|
11 |
|
|
|
|
|
|
|
12 |
def debate_bot(prompt, history="", debate_subject="", bot_role="", history_num=0):
|
13 |
|
14 |
-
print("prompt", prompt)
|
15 |
-
print("history", history)
|
16 |
-
|
17 |
-
##################################################
|
18 |
-
# Bot Role에 따라서 Bot Persona 변경
|
19 |
-
##################################################
|
20 |
if bot_role == "토론":
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
"second debater for the pro side",
|
33 |
-
"second debater for the con side"
|
34 |
-
]
|
35 |
-
|
36 |
-
# user role random으로 정하기
|
37 |
-
user_debate_role = random.choice(debate_role)
|
38 |
-
# user role이 아닌 것이 bot의 role임
|
39 |
-
bot_debate_role_list = [role for role in debate_role if role != user_debate_role]
|
40 |
-
|
41 |
-
print("user_debate_role", user_debate_role)
|
42 |
-
print("bot_debate_role_list", bot_debate_role_list)
|
43 |
-
|
44 |
-
debate_preset = "\n".join([
|
45 |
-
"Debate Rules: ",
|
46 |
-
"1) This debate will be divided into two teams, pro and con, with two debates on each team.",
|
47 |
-
"2) The order of speaking is: first debater for the pro side, first debater for the con side, second debater for the pro side, second debater for the con side.",
|
48 |
-
"3) Answer logically with an introduction, body, and conclusion.\n", #add this one.
|
49 |
-
"User debate role: " + user_debate_role,
|
50 |
-
"Bot debate roles: " + ", ".join(bot_debate_role_list) + "\n",
|
51 |
-
"Debate subject: " + debate_subject
|
52 |
-
])
|
53 |
-
|
54 |
-
# User가 첫번째 차례라면, User에게 먼저 prompt를 받아야 함
|
55 |
-
if user_debate_role == debate_role[0]:
|
56 |
-
#print("user_debate_role", user_debate_role)
|
57 |
-
bot_preset = "\n".join([
|
58 |
-
debate_preset + "\n",
|
59 |
-
"It's your turn! Write your opinion!"
|
60 |
-
])
|
61 |
-
bot_response = bot_preset
|
62 |
-
print("bot_response", bot_response)
|
63 |
-
#return bot_response
|
64 |
-
|
65 |
-
# User가 두번째 차례라면, Bot이 1번째 차례에 대한 response를 만들고, 사용자의 답변을 받아야 함
|
66 |
-
elif user_debate_role == debate_role[1]:
|
67 |
-
|
68 |
-
bot_preset = "\n".join([
|
69 |
-
debate_preset,
|
70 |
-
])
|
71 |
-
|
72 |
-
first_prompt_template = PromptTemplate(
|
73 |
-
input_variables=["prompt"],
|
74 |
-
template="\n".join([
|
75 |
-
bot_preset, #persona
|
76 |
-
"{prompt}",
|
77 |
-
"Only say " + debate_role[0] + "\'s opinion after \':\'. Do not write " + debate_role[1] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
|
78 |
-
debate_role[0] + ": "
|
79 |
-
])
|
80 |
-
)
|
81 |
-
first_bot_prompt = first_prompt_template.format(
|
82 |
-
prompt=""
|
83 |
-
)
|
84 |
-
first_response = gpt_call(first_bot_prompt)
|
85 |
-
|
86 |
-
# preprocess
|
87 |
-
# if first_response contain the first debater for the con side's opinion, remove it.
|
88 |
-
first_response = erase_start_word_and_after(first_response, debate_role[1])
|
89 |
-
first_response = erase_start_word_and_after(first_response, debate_role[2])
|
90 |
-
first_response = erase_start_word_and_after(first_response, debate_role[3])
|
91 |
-
|
92 |
-
#first_response = re.sub(debate_role[1] + ":.*", "", first_response)
|
93 |
-
|
94 |
-
bot_response = "\n".join([
|
95 |
-
bot_preset + "\n",
|
96 |
-
"-----------------------------------------------------------------",
|
97 |
-
"[First debater for the pro side]: " + "\n" + first_response + "\n",
|
98 |
-
"-----------------------------------------------------------------",
|
99 |
-
"It's your turn! Write your opinion!"
|
100 |
-
])
|
101 |
-
|
102 |
-
# User가 세번째 차례라면, Bot이 1, 2번째 차례에 대한 response를 만들고, 사용자의 답변을 받아야 함
|
103 |
-
elif user_debate_role == debate_role[2]:
|
104 |
-
|
105 |
-
bot_preset = "\n".join([
|
106 |
-
debate_preset,
|
107 |
-
])
|
108 |
-
# first
|
109 |
-
first_prompt_template = PromptTemplate(
|
110 |
-
input_variables=["prompt"],
|
111 |
-
template="\n".join([
|
112 |
-
bot_preset, #persona
|
113 |
-
"{prompt}",
|
114 |
-
debate_role[0] + ": ",
|
115 |
-
])
|
116 |
-
)
|
117 |
-
first_bot_prompt = first_prompt_template.format(
|
118 |
-
prompt=""
|
119 |
-
)
|
120 |
-
first_response = gpt_call(first_bot_prompt)
|
121 |
-
|
122 |
-
# second
|
123 |
-
second_prompt_template = PromptTemplate(
|
124 |
-
input_variables=["first_prompt"],
|
125 |
-
template="\n".join([
|
126 |
-
bot_preset, #persona
|
127 |
-
"Only say " + debate_role[1] + "\'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
|
128 |
-
debate_role[0] + ": " + "{first_prompt}",
|
129 |
-
debate_role[1] + ": "
|
130 |
-
])
|
131 |
-
)
|
132 |
-
second_bot_prompt = second_prompt_template.format(
|
133 |
-
first_prompt=first_response
|
134 |
-
)
|
135 |
-
second_response = gpt_call(second_bot_prompt)
|
136 |
-
|
137 |
-
# preprocess
|
138 |
-
# if first_response contain the first debater for the con side's opinion, remove it.
|
139 |
-
first_response = erase_start_word_and_after(first_response, debate_role[1])
|
140 |
-
first_response = erase_start_word_and_after(first_response, debate_role[2])
|
141 |
-
first_response = erase_start_word_and_after(first_response, debate_role[3])
|
142 |
-
# if second_response contain the first debater for the con side's opinion, remove it.
|
143 |
-
#second_response = re.sub(debate_role[2] + ":.*", "", second_response)
|
144 |
-
second_response = erase_start_word_and_after(second_response, debate_role[2])
|
145 |
-
second_response = erase_start_word_and_after(second_response, debate_role[3])
|
146 |
-
|
147 |
-
bot_response = "\n".join([
|
148 |
-
bot_preset + "\n",
|
149 |
-
"-----------------------------------------------------------------",
|
150 |
-
"[First debater for the pro side]: " + "\n" + first_response + "\n",
|
151 |
-
"-----------------------------------------------------------------",
|
152 |
-
"[First debater for the con side]: " + "\n" + second_response + "\n",
|
153 |
-
"-----------------------------------------------------------------",
|
154 |
-
"It's your turn! Write your opinion!"
|
155 |
-
])
|
156 |
-
|
157 |
-
|
158 |
-
elif user_debate_role == debate_role[3]:
|
159 |
-
|
160 |
-
bot_preset = "\n".join([
|
161 |
-
debate_preset,
|
162 |
-
])
|
163 |
-
|
164 |
-
# first
|
165 |
-
first_prompt_template = PromptTemplate(
|
166 |
-
input_variables=["prompt"],
|
167 |
-
template="\n".join([
|
168 |
-
bot_preset, #persona
|
169 |
-
"{prompt}",
|
170 |
-
debate_role[0] + ": ",
|
171 |
-
])
|
172 |
-
)
|
173 |
-
first_bot_prompt = first_prompt_template.format(
|
174 |
-
prompt=""
|
175 |
-
)
|
176 |
-
first_response = gpt_call(first_bot_prompt)
|
177 |
-
|
178 |
-
# second
|
179 |
-
second_prompt_template = PromptTemplate(
|
180 |
-
input_variables=["first_prompt"],
|
181 |
-
template="\n".join([
|
182 |
-
bot_preset, #persona
|
183 |
-
"Only say " + debate_role[1] + "'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
|
184 |
-
debate_role[0] + ": " + "{first_prompt}",
|
185 |
-
debate_role[1] + ": "
|
186 |
-
])
|
187 |
-
)
|
188 |
-
second_bot_prompt = second_prompt_template.format(
|
189 |
-
first_prompt=first_response
|
190 |
-
)
|
191 |
-
second_response = gpt_call(second_bot_prompt)
|
192 |
-
|
193 |
-
# third
|
194 |
-
third_prompt_template = PromptTemplate(
|
195 |
-
input_variables=["first_prompt", "second_prompt"],
|
196 |
-
template="\n".join([
|
197 |
-
bot_preset, #persona
|
198 |
-
"Only say " + debate_role[2] + "\'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[1] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
|
199 |
-
debate_role[0] + ": " + "{first_prompt}",
|
200 |
-
debate_role[1] + ": " + "{second_prompt}",
|
201 |
-
debate_role[2] + ": "
|
202 |
-
])
|
203 |
-
)
|
204 |
-
third_bot_prompt = third_prompt_template.format(
|
205 |
-
first_prompt=first_response,
|
206 |
-
second_prompt=second_response
|
207 |
-
)
|
208 |
-
third_response = gpt_call(third_bot_prompt)
|
209 |
-
|
210 |
-
# preprocess
|
211 |
-
# if first_response contain the first debater for the con side's opinion, remove it.
|
212 |
-
first_response = erase_start_word_and_after(first_response, debate_role[1])
|
213 |
-
first_response = erase_start_word_and_after(first_response, debate_role[2])
|
214 |
-
first_response = erase_start_word_and_after(first_response, debate_role[3])
|
215 |
-
# if second_response contain the first debater for the con side's opinion, remove it.
|
216 |
-
#second_response = re.sub(debate_role[2] + ":.*", "", second_response)
|
217 |
-
second_response = erase_start_word_and_after(second_response, debate_role[2])
|
218 |
-
second_response = erase_start_word_and_after(second_response, debate_role[3])
|
219 |
-
# if third_response contain the first debater for the con side's opinion, remove it.
|
220 |
-
thir_response = erase_start_word_and_after(thir_response, debate_role[3])
|
221 |
-
#third_response = re.sub(debate_role[3] + ":.*", "", third_response)
|
222 |
-
|
223 |
-
bot_response = "\n".join([
|
224 |
-
bot_preset + "\n",
|
225 |
-
"-----------------------------------------------------------------",
|
226 |
-
"[First debater for the pro side]: " + "\n" + first_response + "\n",
|
227 |
-
"-----------------------------------------------------------------",
|
228 |
-
"[First debater for the con side]: " + "\n" + second_response + "\n",
|
229 |
-
"-----------------------------------------------------------------",
|
230 |
-
"[Second debater for the pro side]: " + "\n" + third_response + "\n",
|
231 |
-
"-----------------------------------------------------------------",
|
232 |
-
"It's your turn! Write your opinion!"
|
233 |
-
])
|
234 |
-
else:
|
235 |
-
pass
|
236 |
-
|
237 |
-
# Answer and Ask Judgement.
|
238 |
-
if history_num == 1:
|
239 |
-
|
240 |
-
debate_role = [
|
241 |
-
"first debater for the pro side",
|
242 |
-
"first debater for the con side",
|
243 |
-
"second debater for the pro side",
|
244 |
-
"second debater for the con side"
|
245 |
-
]
|
246 |
-
|
247 |
-
print("history1: ", history)
|
248 |
-
|
249 |
-
# user가 가장 첫번째로 답변했다면, 봇이 2, 3, 4 답변을 하고, 평가할지를 물어보면 됨.
|
250 |
-
if "User debate role: first debater for the pro side" in history:
|
251 |
-
|
252 |
-
# second
|
253 |
-
second_prompt_template = PromptTemplate(
|
254 |
-
input_variables=["prompt"],
|
255 |
-
template="\n".join([
|
256 |
-
history,
|
257 |
-
"User: {prompt}",
|
258 |
-
debate_role[2] + ": "
|
259 |
-
])
|
260 |
-
)
|
261 |
-
second_bot_prompt = second_prompt_template.format(
|
262 |
-
prompt=prompt
|
263 |
-
)
|
264 |
-
second_response = gpt_call(second_bot_prompt)
|
265 |
-
|
266 |
-
|
267 |
-
# third
|
268 |
-
third_prompt_template = PromptTemplate(
|
269 |
-
input_variables=["prompt"],
|
270 |
-
template="\n".join([
|
271 |
-
history,
|
272 |
-
"User: {prompt}",
|
273 |
-
debate_role[2] + ": "
|
274 |
-
])
|
275 |
-
)
|
276 |
-
third_bot_prompt = third_prompt_template.format(
|
277 |
-
prompt=prompt
|
278 |
-
)
|
279 |
-
third_response = gpt_call(third_bot_prompt)
|
280 |
-
|
281 |
-
# fourth
|
282 |
-
fourth_prompt_template = PromptTemplate(
|
283 |
-
input_variables=["prompt"],
|
284 |
-
template="\n".join([
|
285 |
-
history,
|
286 |
-
"User: {prompt}",
|
287 |
-
debate_role[3] + ": "
|
288 |
-
])
|
289 |
-
)
|
290 |
-
fourth_bot_prompt = fourth_prompt_template.format(
|
291 |
-
prompt=prompt
|
292 |
-
)
|
293 |
-
fourth_response = gpt_call(fourth_bot_prompt)
|
294 |
-
|
295 |
-
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
|
296 |
-
bot_response = "\n".join([
|
297 |
-
"[first debater for the con side]: " + "\n" + second_response + "\n",
|
298 |
-
"-----------------------------------------------------------------",
|
299 |
-
"[second debater for the pro sid]: " + "\n" + third_response + "\n",
|
300 |
-
"-----------------------------------------------------------------",
|
301 |
-
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
|
302 |
-
"-----------------------------------------------------------------",
|
303 |
-
ask_judgement
|
304 |
-
])
|
305 |
-
|
306 |
-
# user가 두번째로 답변했다면, 봇이 3, 4 번째 답변을 하고, 평가할지를 물어보면 됨.
|
307 |
-
elif "User debate role: first debater for the con side" in history:
|
308 |
-
|
309 |
-
# third
|
310 |
-
third_prompt_template = PromptTemplate(
|
311 |
-
input_variables=["prompt"],
|
312 |
-
template="\n".join([
|
313 |
-
history,
|
314 |
-
"User: {prompt}",
|
315 |
-
debate_role[2] + ": "
|
316 |
-
])
|
317 |
-
)
|
318 |
-
third_bot_prompt = third_prompt_template.format(
|
319 |
-
prompt=prompt
|
320 |
-
)
|
321 |
-
third_response = gpt_call(third_bot_prompt)
|
322 |
-
|
323 |
-
# fourth
|
324 |
-
fourth_prompt_template = PromptTemplate(
|
325 |
-
input_variables=["prompt"],
|
326 |
-
template="\n".join([
|
327 |
-
history,
|
328 |
-
"User: {prompt}",
|
329 |
-
debate_role[2] + ": " + third_response,
|
330 |
-
debate_role[3] + ": "
|
331 |
-
])
|
332 |
-
)
|
333 |
-
fourth_bot_prompt = fourth_prompt_template.format(
|
334 |
-
prompt=prompt
|
335 |
-
)
|
336 |
-
fourth_response = gpt_call(fourth_bot_prompt)
|
337 |
-
|
338 |
-
# ask_judgement
|
339 |
-
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
|
340 |
-
bot_response = "\n".join([
|
341 |
-
"[second debater for the pro sid]: " + "\n" + third_response + "\n",
|
342 |
-
"-----------------------------------------------------------------",
|
343 |
-
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
|
344 |
-
"-----------------------------------------------------------------",
|
345 |
-
ask_judgement
|
346 |
-
])
|
347 |
-
|
348 |
-
# user가 세번째로 답변했다면, 봇이 4 번째 답변을 하고, 평가할지를 물어보면 됨.
|
349 |
-
elif "User debate role: second debater for the pro side" in history:
|
350 |
-
|
351 |
-
fourth_prompt_template = PromptTemplate(
|
352 |
-
input_variables=["prompt"],
|
353 |
-
template="\n".join([
|
354 |
-
history,
|
355 |
-
"User: {prompt}",
|
356 |
-
debate_role[3] + ": "
|
357 |
-
])
|
358 |
-
)
|
359 |
-
fourth_bot_prompt = fourth_prompt_template.format(
|
360 |
-
prompt=prompt
|
361 |
-
)
|
362 |
-
fourth_response = gpt_call(fourth_bot_prompt)
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
|
367 |
-
bot_response = "\n".join([
|
368 |
-
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
|
369 |
-
"-----------------------------------------------------------------",
|
370 |
-
ask_judgement
|
371 |
-
])
|
372 |
-
|
373 |
-
# user가 네번째로 답변했다면, 바로 평가할지를 물어보면 됨.
|
374 |
-
elif "User debate role: second debater for the con side" in history:
|
375 |
-
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
|
376 |
-
bot_response = ask_judgement
|
377 |
-
else:
|
378 |
-
pass
|
379 |
-
|
380 |
-
# Judgement.
|
381 |
-
if history_num >= 2:
|
382 |
-
judgement_word_list = "\n".join([
|
383 |
-
"!!Instruction!",
|
384 |
-
"You are now the judge of this debate. Evaluate the debate according to the rules below.",
|
385 |
-
"Rule 1. Decide between the pro and con teams.",
|
386 |
-
"Rule 2. Summarize the debate as a whole and what each debater said.",
|
387 |
-
"Rule 3. For each debater, explain what was persuasive and what made the differnce between winning and losing.",
|
388 |
-
])
|
389 |
-
|
390 |
-
judgement_prompt_template = PromptTemplate(
|
391 |
-
input_variables=["prompt"],
|
392 |
-
template="\n".join([
|
393 |
-
history,
|
394 |
-
"{prompt}",
|
395 |
-
judgement_word_list,
|
396 |
-
"Judgement: "
|
397 |
-
])
|
398 |
-
)
|
399 |
-
judgement_bot_prompt = judgement_prompt_template.format(
|
400 |
-
prompt=""
|
401 |
-
)
|
402 |
-
judgement_response = gpt_call(judgement_bot_prompt)
|
403 |
-
|
404 |
-
bot_response = "\n".join([
|
405 |
-
"[Judgement]: " + "\n" + judgement_response + "\n",
|
406 |
-
])
|
407 |
-
|
408 |
-
|
409 |
-
return bot_response
|
|
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from modules.gpt_modules import gpt_call
|
5 |
|
6 |
+
from .normal_debate import nomal_debator
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
+
#############################################
|
10 |
+
# Debate bot setting
|
11 |
+
#############################################
|
12 |
def debate_bot(prompt, history="", debate_subject="", bot_role="", history_num=0):
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
if bot_role == "토론":
|
15 |
+
bot_response = nomal_debator(prompt, history, debate_subject, bot_role, history_num)
|
16 |
+
elif bot_role == "주제 정의":
|
17 |
+
pass
|
18 |
+
elif bot_role == "POI 연습":
|
19 |
+
pass
|
20 |
+
elif bot_role == "역할 추천":
|
21 |
+
pass
|
22 |
+
elif bot_role == "주장 비판":
|
23 |
+
pass
|
24 |
+
else:
|
25 |
+
print("bot_role error")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import random
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from modules.gpt_modules import gpt_call
|
5 |
+
|
6 |
+
def erase_start_word_and_after(text, start_word):
|
7 |
+
pattern = re.compile(re.escape(start_word) + '.*')
|
8 |
+
return re.sub(pattern, '', text)
|
9 |
+
|
10 |
+
def nomal_debator(prompt, history, debate_subject, bot_role, history_num):
|
11 |
+
# Debate Rule 설명하기
|
12 |
+
if history_num == 0:
|
13 |
+
print("history_num", history_num)
|
14 |
+
|
15 |
+
user_role = ""
|
16 |
+
bot_response = ""
|
17 |
+
|
18 |
+
debate_role = [
|
19 |
+
"first debater for the pro side",
|
20 |
+
"first debater for the con side",
|
21 |
+
"second debater for the pro side",
|
22 |
+
"second debater for the con side"
|
23 |
+
]
|
24 |
+
|
25 |
+
# user role random으로 정하기
|
26 |
+
user_debate_role = random.choice(debate_role)
|
27 |
+
# user role이 아닌 것이 bot의 role임
|
28 |
+
bot_debate_role_list = [role for role in debate_role if role != user_debate_role]
|
29 |
+
|
30 |
+
print("user_debate_role", user_debate_role)
|
31 |
+
print("bot_debate_role_list", bot_debate_role_list)
|
32 |
+
|
33 |
+
debate_preset = "\n".join([
|
34 |
+
"Debate Rules: ",
|
35 |
+
"1) This debate will be divided into two teams, pro and con, with two debates on each team.",
|
36 |
+
"2) The order of speaking is: first debater for the pro side, first debater for the con side, second debater for the pro side, second debater for the con side.",
|
37 |
+
"3) Answer logically with an introduction, body, and conclusion.\n", #add this one.
|
38 |
+
"User debate role: " + user_debate_role,
|
39 |
+
"Bot debate roles: " + ", ".join(bot_debate_role_list) + "\n",
|
40 |
+
"Debate subject: " + debate_subject
|
41 |
+
])
|
42 |
+
|
43 |
+
# User가 첫번째 차례라면, User에게 먼저 prompt를 받아야 함
|
44 |
+
if user_debate_role == debate_role[0]:
|
45 |
+
#print("user_debate_role", user_debate_role)
|
46 |
+
bot_preset = "\n".join([
|
47 |
+
debate_preset + "\n",
|
48 |
+
"It's your turn! Write your opinion!"
|
49 |
+
])
|
50 |
+
bot_response = bot_preset
|
51 |
+
print("bot_response", bot_response)
|
52 |
+
#return bot_response
|
53 |
+
|
54 |
+
# User가 두번째 차례라면, Bot이 1번째 차례에 대한 response를 만들고, 사용자의 답변을 받아야 함
|
55 |
+
elif user_debate_role == debate_role[1]:
|
56 |
+
|
57 |
+
bot_preset = "\n".join([
|
58 |
+
debate_preset,
|
59 |
+
])
|
60 |
+
|
61 |
+
first_prompt_template = PromptTemplate(
|
62 |
+
input_variables=["prompt"],
|
63 |
+
template="\n".join([
|
64 |
+
bot_preset, #persona
|
65 |
+
"{prompt}",
|
66 |
+
"Only say " + debate_role[0] + "\'s opinion after \':\'. Do not write " + debate_role[1] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
|
67 |
+
debate_role[0] + ": "
|
68 |
+
])
|
69 |
+
)
|
70 |
+
first_bot_prompt = first_prompt_template.format(
|
71 |
+
prompt=""
|
72 |
+
)
|
73 |
+
first_response = gpt_call(first_bot_prompt)
|
74 |
+
|
75 |
+
# preprocess
|
76 |
+
# if first_response contain the first debater for the con side's opinion, remove it.
|
77 |
+
first_response = erase_start_word_and_after(first_response, debate_role[1])
|
78 |
+
first_response = erase_start_word_and_after(first_response, debate_role[2])
|
79 |
+
first_response = erase_start_word_and_after(first_response, debate_role[3])
|
80 |
+
|
81 |
+
#first_response = re.sub(debate_role[1] + ":.*", "", first_response)
|
82 |
+
|
83 |
+
bot_response = "\n".join([
|
84 |
+
bot_preset + "\n",
|
85 |
+
"-----------------------------------------------------------------",
|
86 |
+
"[First debater for the pro side]: " + "\n" + first_response + "\n",
|
87 |
+
"-----------------------------------------------------------------",
|
88 |
+
"It's your turn! Write your opinion!"
|
89 |
+
])
|
90 |
+
|
91 |
+
# User가 세번째 차례라면, Bot이 1, 2번째 차례에 대한 response를 만들고, 사용자의 답변을 받아야 함
|
92 |
+
elif user_debate_role == debate_role[2]:
|
93 |
+
|
94 |
+
bot_preset = "\n".join([
|
95 |
+
debate_preset,
|
96 |
+
])
|
97 |
+
# first
|
98 |
+
first_prompt_template = PromptTemplate(
|
99 |
+
input_variables=["prompt"],
|
100 |
+
template="\n".join([
|
101 |
+
bot_preset, #persona
|
102 |
+
"{prompt}",
|
103 |
+
debate_role[0] + ": ",
|
104 |
+
])
|
105 |
+
)
|
106 |
+
first_bot_prompt = first_prompt_template.format(
|
107 |
+
prompt=""
|
108 |
+
)
|
109 |
+
first_response = gpt_call(first_bot_prompt)
|
110 |
+
|
111 |
+
# second
|
112 |
+
second_prompt_template = PromptTemplate(
|
113 |
+
input_variables=["first_prompt"],
|
114 |
+
template="\n".join([
|
115 |
+
bot_preset, #persona
|
116 |
+
"Only say " + debate_role[1] + "\'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
|
117 |
+
debate_role[0] + ": " + "{first_prompt}",
|
118 |
+
debate_role[1] + ": "
|
119 |
+
])
|
120 |
+
)
|
121 |
+
second_bot_prompt = second_prompt_template.format(
|
122 |
+
first_prompt=first_response
|
123 |
+
)
|
124 |
+
second_response = gpt_call(second_bot_prompt)
|
125 |
+
|
126 |
+
# preprocess
|
127 |
+
# if first_response contain the first debater for the con side's opinion, remove it.
|
128 |
+
first_response = erase_start_word_and_after(first_response, debate_role[1])
|
129 |
+
first_response = erase_start_word_and_after(first_response, debate_role[2])
|
130 |
+
first_response = erase_start_word_and_after(first_response, debate_role[3])
|
131 |
+
# if second_response contain the first debater for the con side's opinion, remove it.
|
132 |
+
#second_response = re.sub(debate_role[2] + ":.*", "", second_response)
|
133 |
+
second_response = erase_start_word_and_after(second_response, debate_role[2])
|
134 |
+
second_response = erase_start_word_and_after(second_response, debate_role[3])
|
135 |
+
|
136 |
+
bot_response = "\n".join([
|
137 |
+
bot_preset + "\n",
|
138 |
+
"-----------------------------------------------------------------",
|
139 |
+
"[First debater for the pro side]: " + "\n" + first_response + "\n",
|
140 |
+
"-----------------------------------------------------------------",
|
141 |
+
"[First debater for the con side]: " + "\n" + second_response + "\n",
|
142 |
+
"-----------------------------------------------------------------",
|
143 |
+
"It's your turn! Write your opinion!"
|
144 |
+
])
|
145 |
+
|
146 |
+
|
147 |
+
elif user_debate_role == debate_role[3]:
|
148 |
+
|
149 |
+
bot_preset = "\n".join([
|
150 |
+
debate_preset,
|
151 |
+
])
|
152 |
+
|
153 |
+
# first
|
154 |
+
first_prompt_template = PromptTemplate(
|
155 |
+
input_variables=["prompt"],
|
156 |
+
template="\n".join([
|
157 |
+
bot_preset, #persona
|
158 |
+
"{prompt}",
|
159 |
+
debate_role[0] + ": ",
|
160 |
+
])
|
161 |
+
)
|
162 |
+
first_bot_prompt = first_prompt_template.format(
|
163 |
+
prompt=""
|
164 |
+
)
|
165 |
+
first_response = gpt_call(first_bot_prompt)
|
166 |
+
|
167 |
+
# second
|
168 |
+
second_prompt_template = PromptTemplate(
|
169 |
+
input_variables=["first_prompt"],
|
170 |
+
template="\n".join([
|
171 |
+
bot_preset, #persona
|
172 |
+
"Only say " + debate_role[1] + "'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
|
173 |
+
debate_role[0] + ": " + "{first_prompt}",
|
174 |
+
debate_role[1] + ": "
|
175 |
+
])
|
176 |
+
)
|
177 |
+
second_bot_prompt = second_prompt_template.format(
|
178 |
+
first_prompt=first_response
|
179 |
+
)
|
180 |
+
second_response = gpt_call(second_bot_prompt)
|
181 |
+
|
182 |
+
# third
|
183 |
+
third_prompt_template = PromptTemplate(
|
184 |
+
input_variables=["first_prompt", "second_prompt"],
|
185 |
+
template="\n".join([
|
186 |
+
bot_preset, #persona
|
187 |
+
"Only say " + debate_role[2] + "\'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[1] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
|
188 |
+
debate_role[0] + ": " + "{first_prompt}",
|
189 |
+
debate_role[1] + ": " + "{second_prompt}",
|
190 |
+
debate_role[2] + ": "
|
191 |
+
])
|
192 |
+
)
|
193 |
+
third_bot_prompt = third_prompt_template.format(
|
194 |
+
first_prompt=first_response,
|
195 |
+
second_prompt=second_response
|
196 |
+
)
|
197 |
+
third_response = gpt_call(third_bot_prompt)
|
198 |
+
|
199 |
+
# preprocess
|
200 |
+
# if first_response contain the first debater for the con side's opinion, remove it.
|
201 |
+
first_response = erase_start_word_and_after(first_response, debate_role[1])
|
202 |
+
first_response = erase_start_word_and_after(first_response, debate_role[2])
|
203 |
+
first_response = erase_start_word_and_after(first_response, debate_role[3])
|
204 |
+
# if second_response contain the first debater for the con side's opinion, remove it.
|
205 |
+
#second_response = re.sub(debate_role[2] + ":.*", "", second_response)
|
206 |
+
second_response = erase_start_word_and_after(second_response, debate_role[2])
|
207 |
+
second_response = erase_start_word_and_after(second_response, debate_role[3])
|
208 |
+
# if third_response contain the first debater for the con side's opinion, remove it.
|
209 |
+
thir_response = erase_start_word_and_after(thir_response, debate_role[3])
|
210 |
+
#third_response = re.sub(debate_role[3] + ":.*", "", third_response)
|
211 |
+
|
212 |
+
bot_response = "\n".join([
|
213 |
+
bot_preset + "\n",
|
214 |
+
"-----------------------------------------------------------------",
|
215 |
+
"[First debater for the pro side]: " + "\n" + first_response + "\n",
|
216 |
+
"-----------------------------------------------------------------",
|
217 |
+
"[First debater for the con side]: " + "\n" + second_response + "\n",
|
218 |
+
"-----------------------------------------------------------------",
|
219 |
+
"[Second debater for the pro side]: " + "\n" + third_response + "\n",
|
220 |
+
"-----------------------------------------------------------------",
|
221 |
+
"It's your turn! Write your opinion!"
|
222 |
+
])
|
223 |
+
else:
|
224 |
+
pass
|
225 |
+
|
226 |
+
# Answer and Ask Judgement.
|
227 |
+
if history_num == 1:
|
228 |
+
|
229 |
+
debate_role = [
|
230 |
+
"first debater for the pro side",
|
231 |
+
"first debater for the con side",
|
232 |
+
"second debater for the pro side",
|
233 |
+
"second debater for the con side"
|
234 |
+
]
|
235 |
+
|
236 |
+
print("history1: ", history)
|
237 |
+
|
238 |
+
# user가 가장 첫번째로 답변했다면, 봇이 2, 3, 4 답변을 하고, 평가할지를 물어보면 됨.
|
239 |
+
if "User debate role: first debater for the pro side" in history:
|
240 |
+
|
241 |
+
# second
|
242 |
+
second_prompt_template = PromptTemplate(
|
243 |
+
input_variables=["prompt"],
|
244 |
+
template="\n".join([
|
245 |
+
history,
|
246 |
+
"User: {prompt}",
|
247 |
+
debate_role[2] + ": "
|
248 |
+
])
|
249 |
+
)
|
250 |
+
second_bot_prompt = second_prompt_template.format(
|
251 |
+
prompt=prompt
|
252 |
+
)
|
253 |
+
second_response = gpt_call(second_bot_prompt)
|
254 |
+
|
255 |
+
|
256 |
+
# third
|
257 |
+
third_prompt_template = PromptTemplate(
|
258 |
+
input_variables=["prompt"],
|
259 |
+
template="\n".join([
|
260 |
+
history,
|
261 |
+
"User: {prompt}",
|
262 |
+
debate_role[2] + ": "
|
263 |
+
])
|
264 |
+
)
|
265 |
+
third_bot_prompt = third_prompt_template.format(
|
266 |
+
prompt=prompt
|
267 |
+
)
|
268 |
+
third_response = gpt_call(third_bot_prompt)
|
269 |
+
|
270 |
+
# fourth
|
271 |
+
fourth_prompt_template = PromptTemplate(
|
272 |
+
input_variables=["prompt"],
|
273 |
+
template="\n".join([
|
274 |
+
history,
|
275 |
+
"User: {prompt}",
|
276 |
+
debate_role[3] + ": "
|
277 |
+
])
|
278 |
+
)
|
279 |
+
fourth_bot_prompt = fourth_prompt_template.format(
|
280 |
+
prompt=prompt
|
281 |
+
)
|
282 |
+
fourth_response = gpt_call(fourth_bot_prompt)
|
283 |
+
|
284 |
+
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
|
285 |
+
bot_response = "\n".join([
|
286 |
+
"[first debater for the con side]: " + "\n" + second_response + "\n",
|
287 |
+
"-----------------------------------------------------------------",
|
288 |
+
"[second debater for the pro sid]: " + "\n" + third_response + "\n",
|
289 |
+
"-----------------------------------------------------------------",
|
290 |
+
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
|
291 |
+
"-----------------------------------------------------------------",
|
292 |
+
ask_judgement
|
293 |
+
])
|
294 |
+
|
295 |
+
# user가 두번째로 답변했다면, 봇이 3, 4 번째 답변을 하고, 평가할지를 물어보면 됨.
|
296 |
+
elif "User debate role: first debater for the con side" in history:
|
297 |
+
|
298 |
+
# third
|
299 |
+
third_prompt_template = PromptTemplate(
|
300 |
+
input_variables=["prompt"],
|
301 |
+
template="\n".join([
|
302 |
+
history,
|
303 |
+
"User: {prompt}",
|
304 |
+
debate_role[2] + ": "
|
305 |
+
])
|
306 |
+
)
|
307 |
+
third_bot_prompt = third_prompt_template.format(
|
308 |
+
prompt=prompt
|
309 |
+
)
|
310 |
+
third_response = gpt_call(third_bot_prompt)
|
311 |
+
|
312 |
+
# fourth
|
313 |
+
fourth_prompt_template = PromptTemplate(
|
314 |
+
input_variables=["prompt"],
|
315 |
+
template="\n".join([
|
316 |
+
history,
|
317 |
+
"User: {prompt}",
|
318 |
+
debate_role[2] + ": " + third_response,
|
319 |
+
debate_role[3] + ": "
|
320 |
+
])
|
321 |
+
)
|
322 |
+
fourth_bot_prompt = fourth_prompt_template.format(
|
323 |
+
prompt=prompt
|
324 |
+
)
|
325 |
+
fourth_response = gpt_call(fourth_bot_prompt)
|
326 |
+
|
327 |
+
# ask_judgement
|
328 |
+
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
|
329 |
+
bot_response = "\n".join([
|
330 |
+
"[second debater for the pro sid]: " + "\n" + third_response + "\n",
|
331 |
+
"-----------------------------------------------------------------",
|
332 |
+
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
|
333 |
+
"-----------------------------------------------------------------",
|
334 |
+
ask_judgement
|
335 |
+
])
|
336 |
+
|
337 |
+
# user가 세번째로 답변했다면, 봇이 4 번째 답변을 하고, 평가할지를 물어보면 됨.
|
338 |
+
elif "User debate role: second debater for the pro side" in history:
|
339 |
+
|
340 |
+
fourth_prompt_template = PromptTemplate(
|
341 |
+
input_variables=["prompt"],
|
342 |
+
template="\n".join([
|
343 |
+
history,
|
344 |
+
"User: {prompt}",
|
345 |
+
debate_role[3] + ": "
|
346 |
+
])
|
347 |
+
)
|
348 |
+
fourth_bot_prompt = fourth_prompt_template.format(
|
349 |
+
prompt=prompt
|
350 |
+
)
|
351 |
+
fourth_response = gpt_call(fourth_bot_prompt)
|
352 |
+
|
353 |
+
|
354 |
+
|
355 |
+
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
|
356 |
+
bot_response = "\n".join([
|
357 |
+
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
|
358 |
+
"-----------------------------------------------------------------",
|
359 |
+
ask_judgement
|
360 |
+
])
|
361 |
+
|
362 |
+
# user가 네번째로 답변했다면, 바로 평가할지를 물어보면 됨.
|
363 |
+
elif "User debate role: second debater for the con side" in history:
|
364 |
+
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
|
365 |
+
bot_response = ask_judgement
|
366 |
+
else:
|
367 |
+
pass
|
368 |
+
|
369 |
+
# Judgement.
|
370 |
+
if history_num == 2:
|
371 |
+
judgement_word_list = "\n".join([
|
372 |
+
"!!Instruction!",
|
373 |
+
"You are now the judge of this debate. Evaluate the debate according to the rules below.",
|
374 |
+
"Rule 1. Decide between the pro and con teams.",
|
375 |
+
"Rule 2. Summarize the debate as a whole and what each debater said.",
|
376 |
+
"Rule 3. For each debater, explain what was persuasive and what made the differnce between winning and losing.",
|
377 |
+
])
|
378 |
+
|
379 |
+
judgement_prompt_template = PromptTemplate(
|
380 |
+
input_variables=["prompt"],
|
381 |
+
template="\n".join([
|
382 |
+
history,
|
383 |
+
"{prompt}",
|
384 |
+
judgement_word_list,
|
385 |
+
"Judgement: "
|
386 |
+
])
|
387 |
+
)
|
388 |
+
judgement_bot_prompt = judgement_prompt_template.format(
|
389 |
+
prompt=""
|
390 |
+
)
|
391 |
+
judgement_response = gpt_call(judgement_bot_prompt)
|
392 |
+
|
393 |
+
bot_response = "\n".join([
|
394 |
+
"[Judgement]: " + "\n" + judgement_response + "\n",
|
395 |
+
])
|
396 |
+
|
397 |
+
return bot_response
|