-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzh_cosearch_agent_app.py
214 lines (172 loc) · 9.17 KB
/
zh_cosearch_agent_app.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import time
import ast
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from agents.cosearch_agent import CoSearchAgent
from agents.search_engine import SearchEngine
from memory.cosearch_agent_memory import Memory
from memory.rag_results_memory import SearchMemory
from memory.click_memory import ClickMemory
from utils import (
get_conversation_history,
replace_utterance_ids,
get_user_info,
get_channel_info,
send_rag_answer,
send_clarify_question,
update_rag_answer,
SERPAPI_KEY,
OPENAI_KEY,
SQL_PASSWORD,
)
# Slack API tokens
SLACK_BOT_TOKEN = "bot_token"
SLACK_APP_TOKEN = "app_token"
BOT_ID = "bot_id"
app = App(token=SLACK_BOT_TOKEN)
search_engine = SearchEngine(api_key=SERPAPI_KEY)
agent = CoSearchAgent(search_engine=search_engine, api_key=OPENAI_KEY)
memory = Memory(sql_password=SQL_PASSWORD)
search_memory = SearchMemory(sql_password=SQL_PASSWORD)
click_memory = ClickMemory(sql_password=SQL_PASSWORD)
channel_id2names = get_channel_info(table_name="channel_info")
user_id2names = get_user_info(table_name="user_info")
user_id2names["bot_id"] = "CoSearchAgent"
@app.action("click")
def click_link(ack, body):
channel_name, user_name = body["channel"]["name"], body["user"]["username"]
event_ts = body["container"]["message_ts"]
search_info = search_memory.load_search_results_from_timestamp(table_name=f"{channel_name}_search",
timestamp=str(event_ts))
action = body["actions"][0]
link, ts = action["value"], action["action_ts"]
click_memory.create_table_if_not_exists(f"{channel_name}_click")
click_memory.write_into_memory(table_name=f"{channel_name}_click",
click_info={
"user_name": user_name,
"query": search_info["query"],
"link": link,
"timestamp": ts
})
ack()
@app.action("next")
def return_next_page(ack, body, client):
channel_id, user_id = body['channel']['id'], body['user']['id']
channel_name, user_name = channel_id2names[channel_id], user_id2names[user_id]
event_ts = body["container"]["message_ts"]
search_info = search_memory.load_search_results_from_timestamp(table_name=f"{channel_name}_search",
timestamp=str(event_ts))
search_results = ast.literal_eval(search_info["search_results"])
start, end = search_info["start"], search_info["end"]
if start + 2 < len(search_results):
start = start + 2
end = end + 2
ts = update_rag_answer(client=client, query=search_info["query"], channel_id=channel_id, user_id=user_id,
answer=search_info["answer"], references=search_results, start=start, end=end,
ts=event_ts)["ts"]
search_memory.write_into_memory(table_name=f"{channel_name}_search", search_info={
"user_name": user_name,
"query": search_info["query"],
"answer": search_info["answer"],
"search_results": str(search_results),
"start": start,
"end": end,
"click_time": time.time(),
"timestamp": ts
})
ack()
@app.action("previous")
def return_previous_page(ack, body, client):
channel_id, user_id = body['channel']['id'], body['user']['id']
channel_name, user_name = channel_id2names[channel_id], user_id2names[user_id]
event_ts = body["container"]["message_ts"]
search_info = search_memory.load_search_results_from_timestamp(table_name=f"{channel_name}_search",
timestamp=str(event_ts))
search_results = ast.literal_eval(search_info["search_results"])
start, end = search_info["start"], search_info["end"]
if start - 2 >= 0:
start = start - 2
end = end - 2
ts = update_rag_answer(client=client, query=search_info["query"], channel_id=channel_id, user_id=user_id,
answer=search_info["answer"], references=search_results, start=start, end=end,
ts=event_ts)["ts"]
search_memory.write_into_memory(table_name=f"{channel_name}_search", search_info={
"user_name": user_name,
"query": search_info["query"],
"answer": search_info["answer"],
"search_results": str(search_results),
"start": start,
"end": end,
"click_time": time.time(),
"timestamp": ts
})
ack()
@app.event("message")
def handle_message_event(ack, event, client):
channel_id, user_id, ts = event["channel"], event["user"], event["ts"]
channel_name, user_name = channel_id2names[channel_id], user_id2names[user_id]
if user_id == BOT_ID:
return
user_utterance = replace_utterance_ids(event["text"].strip("\n").strip(), id2names=user_id2names)
memory.create_table_if_not_exists(table_name=channel_name)
memory.write_into_memory(
table_name=channel_name,
utterance_info={"speaker": user_name, "utterance": user_utterance, "convs": "",
"query": "", "rewrite_query": "", "rewrite_thought": "",
"clarify": "", "clarify_thought": "", "clarify_cnt": 0, "search_results": "",
"infer_time": "", "reply_timestamp": "", "reply_user": "", "timestamp": ts}
)
pattern = "@CoSearchAgent"
if user_utterance.startswith(pattern):
convs = get_conversation_history(client=client, channel_id=channel_id, bot_id=BOT_ID,
user_id2names=user_id2names, ts=ts)
query = user_utterance.lstrip(pattern).strip()
rewrite_output, rewrite_time = agent.rewrite_query(user=user_name, query=query, convs=convs)
rewrite_output = rewrite_output.split("\n")
rewrite_thought = rewrite_output[0].lstrip("判断思路:").strip()
rewrite_query = rewrite_output[1].lstrip("重写查询:").strip()
clarify_cnt = memory.get_clarify_cnt_for_speaker(table_name=channel_name, reply_user=user_name)
if clarify_cnt > 0:
clarify_thought, clarify_question = "", ""
clarify_time = 0
else:
clarify_output, clarify_time = agent.ask_clarify_query(user=user_name, query=rewrite_query, convs=convs)
clarify_output = clarify_output.split("\n")
clarify_thought = clarify_output[0].lstrip("判断思路:").strip()
clarify_question = clarify_output[1].lstrip("澄清性问题:").strip()
if "不需要提出澄清性问题" not in clarify_question:
response_ts = send_clarify_question(client=client, channel_id=channel_id, user_id=user_id,
clarify_question=clarify_question)["ts"]
memory.write_into_memory(
table_name=channel_name,
utterance_info={"speaker": "CoSearchAgent", "utterance": clarify_question, "convs": convs,
"query": query, "rewrite_query": rewrite_query, "rewrite_thought": rewrite_thought,
"clarify": clarify_question, "clarify_thought": clarify_thought,
"clarify_cnt": clarify_cnt+1, "search_results": "",
"infer_time": str({"rewrite": rewrite_time, "clarify": clarify_time}),
"reply_user": user_name, "reply_timestamp": ts, "timestamp": response_ts}
)
return
answer, search_results, extract_time, answer_time = agent.generate_answer(rewrite_query)
response_ts = send_rag_answer(client=client, channel_id=channel_id, query=rewrite_query, user_id=user_id,
answer=answer, references=search_results)["ts"]
memory.write_into_memory(
table_name=channel_name,
utterance_info={"speaker": "CoSearchAgent", "utterance": answer, "convs": convs,
"query": query, "rewrite_query": rewrite_query, "rewrite_thought": rewrite_thought,
"clarify": clarify_question, "clarify_thought": clarify_thought, "clarify_cnt": 0,
"search_results": str(search_results),
"infer_time": str({"rewrite": rewrite_time, "clarify": clarify_time,
"extract": extract_time, "answer": answer_time}),
"reply_timestamp": ts, "reply_user": user_name, "timestamp": response_ts}
)
search_memory.create_table_if_not_exists(table_name=f"{channel_name}_search")
search_memory.write_into_memory(
table_name=f"{channel_name}_search",
search_info={"user_name": user_name, "query": rewrite_query, "answer": answer,
"search_results": str(search_results), "start": 0, "end": 2,
"click_time": time.time(), "timestamp": response_ts}
)
ack()
if __name__ == "__main__":
SocketModeHandler(app, SLACK_APP_TOKEN).start()