Spaces:
Runtime error
Runtime error
File size: 17,383 Bytes
5f3392e |
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
import re
import random
from langchain.prompts import PromptTemplate
from modules.gpt_modules import gpt_call
def erase_start_word_and_after(text, start_word):
pattern = re.compile(re.escape(start_word) + '.*')
return re.sub(pattern, '', text)
def one_to_one_debator(prompt, history, debate_subject, bot_role, history_num):
# Debate Rule 설명하기
if history_num == 0:
print("history_num", history_num)
user_role = ""
bot_response = ""
debate_role = [
"pro side",
"con side"
]
# user role random으로 정하기
user_debate_role = random.choice(debate_role)
# user role이 아닌 것이 bot의 role임
bot_debate_role_list = [role for role in debate_role if role != user_debate_role]
print("user_debate_role", user_debate_role)
print("bot_debate_role_list", bot_debate_role_list)
debate_preset = "\n".join([
"Debate Rules: ",
"1) This debate will be divided into two teams, pro and con, with two debates on each team.",
"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.",
"3) Answer logically with an introduction, body, and conclusion.\n", #add this one.
"User debate role: " + user_debate_role,
"Bot debate roles: " + ", ".join(bot_debate_role_list) + "\n",
"Debate subject: " + debate_subject
])
# User가 첫번째 차례라면, User에게 먼저 prompt를 받아야 함
if user_debate_role == debate_role[0]:
#print("user_debate_role", user_debate_role)
bot_preset = "\n".join([
debate_preset + "\n",
"It's your turn! Write your opinion!"
])
bot_response = bot_preset
print("bot_response", bot_response)
#return bot_response
# User가 두번째 차례라면, Bot이 1번째 차례에 대한 response를 만들고, 사용자의 답변을 받아야 함
elif user_debate_role == debate_role[1]:
bot_preset = "\n".join([
debate_preset,
])
first_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
bot_preset, #persona
"{prompt}",
"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.",
debate_role[0] + ": "
])
)
first_bot_prompt = first_prompt_template.format(
prompt=""
)
first_response = gpt_call(first_bot_prompt)
# preprocess
# if first_response contain the first debater for the con side's opinion, remove it.
first_response = erase_start_word_and_after(first_response, debate_role[1])
first_response = erase_start_word_and_after(first_response, debate_role[2])
first_response = erase_start_word_and_after(first_response, debate_role[3])
#first_response = re.sub(debate_role[1] + ":.*", "", first_response)
bot_response = "\n".join([
bot_preset + "\n",
"-----------------------------------------------------------------",
"[First debater for the pro side]: " + "\n" + first_response + "\n",
"-----------------------------------------------------------------",
"It's your turn! Write your opinion!"
])
# User가 세번째 차례라면, Bot이 1, 2번째 차례에 대한 response를 만들고, 사용자의 답변을 받아야 함
elif user_debate_role == debate_role[2]:
bot_preset = "\n".join([
debate_preset,
])
# first
first_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
bot_preset, #persona
"{prompt}",
debate_role[0] + ": ",
])
)
first_bot_prompt = first_prompt_template.format(
prompt=""
)
first_response = gpt_call(first_bot_prompt)
# second
second_prompt_template = PromptTemplate(
input_variables=["first_prompt"],
template="\n".join([
bot_preset, #persona
"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.",
debate_role[0] + ": " + "{first_prompt}",
debate_role[1] + ": "
])
)
second_bot_prompt = second_prompt_template.format(
first_prompt=first_response
)
second_response = gpt_call(second_bot_prompt)
# preprocess
# if first_response contain the first debater for the con side's opinion, remove it.
first_response = erase_start_word_and_after(first_response, debate_role[1])
first_response = erase_start_word_and_after(first_response, debate_role[2])
first_response = erase_start_word_and_after(first_response, debate_role[3])
# if second_response contain the first debater for the con side's opinion, remove it.
#second_response = re.sub(debate_role[2] + ":.*", "", second_response)
second_response = erase_start_word_and_after(second_response, debate_role[2])
second_response = erase_start_word_and_after(second_response, debate_role[3])
bot_response = "\n".join([
bot_preset + "\n",
"-----------------------------------------------------------------",
"[First debater for the pro side]: " + "\n" + first_response + "\n",
"-----------------------------------------------------------------",
"[First debater for the con side]: " + "\n" + second_response + "\n",
"-----------------------------------------------------------------",
"It's your turn! Write your opinion!"
])
elif user_debate_role == debate_role[3]:
bot_preset = "\n".join([
debate_preset,
])
# first
first_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
bot_preset, #persona
"{prompt}",
debate_role[0] + ": ",
])
)
first_bot_prompt = first_prompt_template.format(
prompt=""
)
first_response = gpt_call(first_bot_prompt)
# second
second_prompt_template = PromptTemplate(
input_variables=["first_prompt"],
template="\n".join([
bot_preset, #persona
"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.",
debate_role[0] + ": " + "{first_prompt}",
debate_role[1] + ": "
])
)
second_bot_prompt = second_prompt_template.format(
first_prompt=first_response
)
second_response = gpt_call(second_bot_prompt)
# third
third_prompt_template = PromptTemplate(
input_variables=["first_prompt", "second_prompt"],
template="\n".join([
bot_preset, #persona
"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.",
debate_role[0] + ": " + "{first_prompt}",
debate_role[1] + ": " + "{second_prompt}",
debate_role[2] + ": "
])
)
third_bot_prompt = third_prompt_template.format(
first_prompt=first_response,
second_prompt=second_response
)
third_response = gpt_call(third_bot_prompt)
# preprocess
# if first_response contain the first debater for the con side's opinion, remove it.
first_response = erase_start_word_and_after(first_response, debate_role[1])
first_response = erase_start_word_and_after(first_response, debate_role[2])
first_response = erase_start_word_and_after(first_response, debate_role[3])
# if second_response contain the first debater for the con side's opinion, remove it.
#second_response = re.sub(debate_role[2] + ":.*", "", second_response)
second_response = erase_start_word_and_after(second_response, debate_role[2])
second_response = erase_start_word_and_after(second_response, debate_role[3])
# if third_response contain the first debater for the con side's opinion, remove it.
thir_response = erase_start_word_and_after(thir_response, debate_role[3])
#third_response = re.sub(debate_role[3] + ":.*", "", third_response)
bot_response = "\n".join([
bot_preset + "\n",
"-----------------------------------------------------------------",
"[First debater for the pro side]: " + "\n" + first_response + "\n",
"-----------------------------------------------------------------",
"[First debater for the con side]: " + "\n" + second_response + "\n",
"-----------------------------------------------------------------",
"[Second debater for the pro side]: " + "\n" + third_response + "\n",
"-----------------------------------------------------------------",
"It's your turn! Write your opinion!"
])
else:
pass
# Answer and Ask Judgement.
if history_num == 1:
debate_role = [
"first debater for the pro side",
"first debater for the con side",
"second debater for the pro side",
"second debater for the con side"
]
print("history1: ", history)
# user가 가장 첫번째로 답변했다면, 봇이 2, 3, 4 답변을 하고, 평가할지를 물어보면 됨.
if "User debate role: first debater for the pro side" in history:
# second
second_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
history,
"User: {prompt}",
debate_role[2] + ": "
])
)
second_bot_prompt = second_prompt_template.format(
prompt=prompt
)
second_response = gpt_call(second_bot_prompt)
# third
third_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
history,
"User: {prompt}",
debate_role[2] + ": "
])
)
third_bot_prompt = third_prompt_template.format(
prompt=prompt
)
third_response = gpt_call(third_bot_prompt)
# fourth
fourth_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
history,
"User: {prompt}",
debate_role[3] + ": "
])
)
fourth_bot_prompt = fourth_prompt_template.format(
prompt=prompt
)
fourth_response = gpt_call(fourth_bot_prompt)
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
bot_response = "\n".join([
"[first debater for the con side]: " + "\n" + second_response + "\n",
"-----------------------------------------------------------------",
"[second debater for the pro sid]: " + "\n" + third_response + "\n",
"-----------------------------------------------------------------",
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
"-----------------------------------------------------------------",
ask_judgement
])
# user가 두번째로 답변했다면, 봇이 3, 4 번째 답변을 하고, 평가할지를 물어보면 됨.
elif "User debate role: first debater for the con side" in history:
# third
third_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
history,
"User: {prompt}",
debate_role[2] + ": "
])
)
third_bot_prompt = third_prompt_template.format(
prompt=prompt
)
third_response = gpt_call(third_bot_prompt)
# fourth
fourth_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
history,
"User: {prompt}",
debate_role[2] + ": " + third_response,
debate_role[3] + ": "
])
)
fourth_bot_prompt = fourth_prompt_template.format(
prompt=prompt
)
fourth_response = gpt_call(fourth_bot_prompt)
# ask_judgement
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
bot_response = "\n".join([
"[second debater for the pro sid]: " + "\n" + third_response + "\n",
"-----------------------------------------------------------------",
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
"-----------------------------------------------------------------",
ask_judgement
])
# user가 세번째로 답변했다면, 봇이 4 번째 답변을 하고, 평가할지를 물어보면 됨.
elif "User debate role: second debater for the pro side" in history:
fourth_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
history,
"User: {prompt}",
debate_role[3] + ": "
])
)
fourth_bot_prompt = fourth_prompt_template.format(
prompt=prompt
)
fourth_response = gpt_call(fourth_bot_prompt)
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
bot_response = "\n".join([
"[second debater for the con side]: " + "\n" + fourth_response + "\n",
"-----------------------------------------------------------------",
ask_judgement
])
# user가 네번째로 답변했다면, 바로 평가할지를 물어보면 됨.
elif "User debate role: second debater for the con side" in history:
ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
bot_response = ask_judgement
else:
pass
# Judgement.
if history_num == 2:
judgement_word_list = "\n".join([
"!!Instruction!",
"You are now the judge of this debate. Evaluate the debate according to the rules below.",
"Rule 1. Decide between the pro and con teams.",
"Rule 2. Summarize the debate as a whole and what each debater said.",
"Rule 3. For each debater, explain what was persuasive and what made the differnce between winning and losing.",
])
judgement_prompt_template = PromptTemplate(
input_variables=["prompt"],
template="\n".join([
history,
"{prompt}",
judgement_word_list,
"Judgement: "
])
)
judgement_bot_prompt = judgement_prompt_template.format(
prompt=""
)
judgement_response = gpt_call(judgement_bot_prompt)
bot_response = "\n".join([
"[Judgement]: " + "\n" + judgement_response + "\n",
])
return bot_response |