Spaces:
Runtime error
Runtime error
add
Browse files- bots/debate_bot.py +101 -15
- modules/history_modules.py +4 -1
- modules/query_modules.py +3 -2
bots/debate_bot.py
CHANGED
@@ -1,26 +1,113 @@
|
|
1 |
import re
|
|
|
2 |
from langchain.prompts import PromptTemplate
|
3 |
from modules.gpt_modules import gpt_call
|
4 |
|
5 |
-
def debate_bot(prompt, history="", debate_subject="", bot_role=""):
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
bot_persona = "\n".join([
|
9 |
-
'
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
])
|
14 |
few_shot_prompt = "\n".join([
|
15 |
""
|
16 |
])
|
17 |
else:
|
18 |
-
|
19 |
-
few_shot_prompt = ""
|
20 |
|
21 |
-
debate_subject = debate_subject
|
22 |
|
23 |
-
# 요약한 내용을 친절하게 설명해주는
|
24 |
dialog_prompt_template = PromptTemplate(
|
25 |
input_variables=["prompt"],
|
26 |
template="\n".join([
|
@@ -29,14 +116,13 @@ def debate_bot(prompt, history="", debate_subject="", bot_role=""):
|
|
29 |
"Debate Subject: " + debate_subject,
|
30 |
history,
|
31 |
"User: {prompt}",
|
32 |
-
"
|
33 |
])
|
34 |
)
|
35 |
|
36 |
-
|
37 |
-
dduru_prompt = dialog_prompt_template.format(
|
38 |
prompt=prompt
|
39 |
)
|
40 |
-
|
41 |
|
42 |
-
return
|
|
|
1 |
import re
|
2 |
+
import random
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from modules.gpt_modules import gpt_call
|
5 |
|
|
|
6 |
|
7 |
+
def debate_bot(prompt, history="", debate_subject="", bot_role="", history_num=0):
|
8 |
+
|
9 |
+
##################################################
|
10 |
+
# Bot Role에 따라서 Bot Persona 변경
|
11 |
+
##################################################
|
12 |
+
if bot_role == "토론":
|
13 |
+
|
14 |
+
user_role = ""
|
15 |
+
|
16 |
+
# Debate Rule 설명하기
|
17 |
+
if history_num == 0:
|
18 |
+
|
19 |
+
debate_role = [
|
20 |
+
"(1) first debater for the pro side",
|
21 |
+
"(2) first debater for the con side",
|
22 |
+
"(3) second debater for the pro side",
|
23 |
+
"(4) second debater for the con side."
|
24 |
+
]
|
25 |
+
|
26 |
+
# user role random으로 정하기
|
27 |
+
user_debate_role = random.choice(debate_role)
|
28 |
+
# user role이 아닌 것이 bot의 role임
|
29 |
+
bot_debate_role_list = [role for role in debate_role if role != user_debate_role]
|
30 |
+
|
31 |
+
debate_preset = "\n".join([
|
32 |
+
"Debate Rules: ",
|
33 |
+
"1) This debate will be divided into two teams, pro and con, with two debates on each team.",
|
34 |
+
"2) The order of speaking is: (1) first debater for the pro side, (2) first debater for the con side, (3) second debater for the pro side, (4) second debater for the con side.",
|
35 |
+
"User debate role is " + user_debate_role,
|
36 |
+
"Bot debate roles are " + ", ".join(bot_debate_role_list),
|
37 |
+
"Debate subject: " + debate_subject
|
38 |
+
])
|
39 |
+
|
40 |
+
# User가 첫번째 차례라면, User에게 먼저 prompt를 받아야 함
|
41 |
+
if user_role == "first debater for the pro side":
|
42 |
+
bot_response = "\n".join([
|
43 |
+
debate_preset,
|
44 |
+
"It's your turn! Write your opinion!"
|
45 |
+
])
|
46 |
+
return bot_response
|
47 |
+
|
48 |
+
# User가 두번째 차례라면, Bot이 먼저 prompt를 제시해야 함
|
49 |
+
elif user_role == "first debater for the con side":
|
50 |
+
|
51 |
+
dialog_prompt_template = PromptTemplate(
|
52 |
+
input_variables=["prompt"],
|
53 |
+
template="\n".join([
|
54 |
+
debate_preset,
|
55 |
+
"User: {prompt}",
|
56 |
+
"Bot: "
|
57 |
+
])
|
58 |
+
)
|
59 |
+
few_shot_prompt = "\n".join([
|
60 |
+
""
|
61 |
+
])
|
62 |
+
elif user_role == "second debater for the pro side":
|
63 |
+
few_shot_prompt = "\n".join([
|
64 |
+
""
|
65 |
+
])
|
66 |
+
elif user_role == "second debater for the con side":
|
67 |
+
few_shot_prompt = "\n".join([
|
68 |
+
""
|
69 |
+
])
|
70 |
+
else:
|
71 |
+
pass
|
72 |
+
|
73 |
+
dialog_prompt_template = PromptTemplate(
|
74 |
+
input_variables=["prompt"],
|
75 |
+
template="\n".join([
|
76 |
+
bot_persona, #persona
|
77 |
+
few_shot_prompt,
|
78 |
+
"Debate Subject: " + debate_subject,
|
79 |
+
history,
|
80 |
+
"User: {prompt}",
|
81 |
+
"Bot: "
|
82 |
+
])
|
83 |
+
)
|
84 |
+
|
85 |
+
bot_prompt = dialog_prompt_template.format(
|
86 |
+
prompt=prompt
|
87 |
+
)
|
88 |
+
bot_response = gpt_call(bot_prompt)
|
89 |
+
|
90 |
+
return bot_response
|
91 |
+
# Assign user one of the following roles
|
92 |
+
elif history_num == 1:
|
93 |
+
pass
|
94 |
+
|
95 |
bot_persona = "\n".join([
|
96 |
+
'Debate Rules:',
|
97 |
+
"1) This debate will be divided into two teams, pro and con, with two debates on each team.",
|
98 |
+
"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.",
|
99 |
+
|
100 |
+
"Assign user one of the following roles: first debater for the pro side, first debater for the con side, second debater fo the pro side, seconde debater for the con side.",
|
101 |
+
"Debate the remaining roles you didn't give user. With given Debate Subject.",
|
102 |
+
|
103 |
])
|
104 |
few_shot_prompt = "\n".join([
|
105 |
""
|
106 |
])
|
107 |
else:
|
108 |
+
print("bot_role is needed")
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
dialog_prompt_template = PromptTemplate(
|
112 |
input_variables=["prompt"],
|
113 |
template="\n".join([
|
|
|
116 |
"Debate Subject: " + debate_subject,
|
117 |
history,
|
118 |
"User: {prompt}",
|
119 |
+
"Bot: "
|
120 |
])
|
121 |
)
|
122 |
|
123 |
+
bot_prompt = dialog_prompt_template.format(
|
|
|
124 |
prompt=prompt
|
125 |
)
|
126 |
+
bot_response = gpt_call(bot_prompt)
|
127 |
|
128 |
+
return bot_response
|
modules/history_modules.py
CHANGED
@@ -16,6 +16,7 @@ def get_history(
|
|
16 |
|
17 |
if history_list==[]:
|
18 |
history = ""
|
|
|
19 |
else:
|
20 |
history = ""
|
21 |
history_dummy_list = []
|
@@ -23,7 +24,9 @@ def get_history(
|
|
23 |
if dic['session_num'] == session_num:
|
24 |
history_dummy_list.append("User: " + dic['user_prompt'])
|
25 |
history_dummy_list.append("Bot: " + dic['bot_response'])
|
|
|
|
|
26 |
|
27 |
history = "\n".join(history_dummy_list)
|
28 |
|
29 |
-
return history
|
|
|
16 |
|
17 |
if history_list==[]:
|
18 |
history = ""
|
19 |
+
history_num = 0
|
20 |
else:
|
21 |
history = ""
|
22 |
history_dummy_list = []
|
|
|
24 |
if dic['session_num'] == session_num:
|
25 |
history_dummy_list.append("User: " + dic['user_prompt'])
|
26 |
history_dummy_list.append("Bot: " + dic['bot_response'])
|
27 |
+
|
28 |
+
history_num = int( len(history_dummy_list) / 2 ) # user와 bot이 한 세트이므로, 절반으로 나누기
|
29 |
|
30 |
history = "\n".join(history_dummy_list)
|
31 |
|
32 |
+
return history, history_num
|
modules/query_modules.py
CHANGED
@@ -21,7 +21,7 @@ def query(
|
|
21 |
|
22 |
print("query session", session_num)
|
23 |
|
24 |
-
history = get_history(
|
25 |
db_table,
|
26 |
name_of_partition_key="user_id",
|
27 |
value_of_partition_key=user_id,
|
@@ -33,7 +33,8 @@ def query(
|
|
33 |
prompt,
|
34 |
history,
|
35 |
debate_subject,
|
36 |
-
bot_role
|
|
|
37 |
)
|
38 |
|
39 |
time_stamp = str(datetime.fromtimestamp(time()))
|
|
|
21 |
|
22 |
print("query session", session_num)
|
23 |
|
24 |
+
history, history_num = get_history(
|
25 |
db_table,
|
26 |
name_of_partition_key="user_id",
|
27 |
value_of_partition_key=user_id,
|
|
|
33 |
prompt,
|
34 |
history,
|
35 |
debate_subject,
|
36 |
+
bot_role,
|
37 |
+
history_num
|
38 |
)
|
39 |
|
40 |
time_stamp = str(datetime.fromtimestamp(time()))
|