-
Notifications
You must be signed in to change notification settings - Fork 99
/
backend_server.py
193 lines (171 loc) · 5.85 KB
/
backend_server.py
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
import argparse
import asyncio
import random
import string
import chainlit as cl
from chainlit_callback_handler import ChainlitCallbackHandler
from database import save_dialogue_to_db
from pipelines.chatbot import create_chain, run_one_turn
from pipelines.pipeline_arguments import (
add_pipeline_arguments,
check_pipeline_arguments,
)
from pipelines.utils import dict_to_command_line, get_logger
from tasks.defaults import CHATBOT_DEFAULT_CONFIG
logger = get_logger(__name__)
chat_profiles_dict = {
"Most Factual": {
"description": "WikiChat based on GPT-4o",
"overwritten_parameters": {},
"step_name_mapping": {
# "generate": "Generate",
"query": "Search",
"search": "",
"summarize": "",
"split_claims": "LLM Claims",
"verify": "",
"draft": "Draft",
"refine": "Refine",
},
},
"Balanced": {
"description": "WikiChat based on GPT-4o-mini",
"overwritten_parameters": {
"engine": "gpt-4o-mini",
},
"step_name_mapping": {
# "generate": "Generate",
"query": "Search",
"search": "",
"summarize": "",
"split_claims": "LLM Claims",
"verify": "",
"draft": "Draft",
"refine": "Refine",
},
},
# "Fastest": {
# "description": "WikiChat distilled",
# "overwritten_parameters": {
# "engine": "gpt-35-turbo-finetuned",
# "pipeline": "early_combine",
# "generation_prompt": "generate.prompt",
# "refinement_prompt": "refine_w_feedback.prompt",
# "retrieval_num": 3,
# "do_refine": False,
# "fuse_claim_splitting": True,
# "retrieval_reranking_method": "date",
# "retrieval_reranking_num": 9,
# },
# "step_name_mapping": {
# # "generate": "Generate",
# "query": "Search",
# "search": "",
# "summarize": "",
# "split_claims": "LLM Claims",
# "verify": "",
# "draft": "Draft",
# "refine": "Refine",
# },
# },
# "GPT-4o": {
# "description": "Vanilla GPT-4o",
# "overwritten_parameters": {
# "engine": "gpt-4o",
# "pipeline": "generate",
# "do_refine": False,
# },
# "step_name_mapping": {
# "generate": "Generate",
# # "refine": "Refine",
# },
# },
}
@cl.set_chat_profiles
async def chat_profile():
return [
cl.ChatProfile(
name=k, markdown_description=chat_profiles_dict[k]["description"]
)
for k in chat_profiles_dict.keys()
]
@cl.on_chat_start
async def start():
all_step_names = set()
for profile in chat_profiles_dict:
all_step_names.update(chat_profiles_dict[profile]["step_name_mapping"].values())
await asyncio.gather(
cl.Message(
content="""Hi! I'm WikiChat.\n
I'll keep our conversations for research purposes.\n
I can talk to you in any language, but I get my information from these Wikipedias: 🇺🇸 English, 🇨🇳 Chinese, 🇪🇸 Spanish, 🇵🇹 Portuguese, 🇷🇺 Russian, 🇩🇪 German, 🇮🇷 Farsi, 🇯🇵 Japanese, 🇫🇷 French, 🇮🇹 Italian.\n
By default, I prioritize factuality over speed. You can try other versions of WikiChat using the above menue.\n
Ask me about anything on Wikipedia!
"""
).send(),
cl.Avatar(
name="WikiChat",
path="public/logo_light.png",
).send(),
cl.Avatar(
name="You",
path="public/user_avatar.png",
).send(),
*[
cl.Avatar(
name=step_name,
path="public/empty.png",
).send()
for step_name in all_step_names
],
setup_agent(),
)
@cl.on_settings_update
async def setup_agent():
chat_profile = cl.user_session.get("chat_profile")
if chat_profile not in chat_profiles_dict:
return
logger.debug("Using chat profile %s", chat_profile)
parser = argparse.ArgumentParser()
add_pipeline_arguments(parser)
# set parameters
args = parser.parse_args(
dict_to_command_line(
CHATBOT_DEFAULT_CONFIG,
chat_profiles_dict[chat_profile]["overwritten_parameters"],
)
)
check_pipeline_arguments(args)
chatbot, dialogue_state = create_chain(args)
cl.user_session.set("chatbot", chatbot)
cl.user_session.set("dialogue_state", dialogue_state)
cl.user_session.set(
"dialogue_id",
"".join(random.choices(string.ascii_letters + string.digits, k=8)),
) # 8-character random string as the unique dialogue_id
@cl.on_message
async def chat(message: cl.Message):
chatbot = cl.user_session.get("chatbot")
chat_profile = cl.user_session.get("chat_profile")
dialogue_state = cl.user_session.get("dialogue_state")
dialogue_state["new_user_utterance"] = message.content
new_agent_utterance, dialogue_state = await run_one_turn(
chatbot,
dialogue_state,
new_user_utterance=message.content,
callbacks=[
ChainlitCallbackHandler(
step_name_mapping=chat_profiles_dict[chat_profile]["step_name_mapping"]
)
],
)
# print("dialogue_state = ", dialogue_state)
await cl.Message(content=new_agent_utterance).send()
cl.user_session.set("dialogue_state", dialogue_state)
@cl.on_chat_end
def on_chat_end():
dialogue_state = cl.user_session.get("dialogue_state")
dialogue_id = cl.user_session.get("dialogue_id")
chat_profile = cl.user_session.get("chat_profile")
if dialogue_state and dialogue_state["dialogue_history"]:
save_dialogue_to_db(dialogue_state, dialogue_id, chat_profile)