-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdiscussion.py
303 lines (241 loc) · 10.2 KB
/
discussion.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
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
# from https://docs.streamlit.io/develop/tutorials/llms/build-conversational-apps
import streamlit as st
from langchain_upstage import ChatUpstage as Chat
from pydantic import BaseModel
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_community.tools import DuckDuckGoSearchResults
from solar_util import initialize_solar_llm
st.set_page_config(page_title="Discuss", page_icon="🗣️")
st.title("Self-debating Solar Pro Preview")
llm = initialize_solar_llm()
ddg_search = DuckDuckGoSearchResults()
# Define your desired data structure.
class SearchKeyword(BaseModel):
list[str]
search_keyword_extraction = ChatPromptTemplate.from_messages(
[
(
"system",
"""You are Solar-Discussor, a smart discussion chatbot by Upstage, loved by many people.
You already comeup with a discussion draft.
Now you can use google search to find more information about the discussion point.
Please come up with 2~3 search keywords that you can use to find more information about the discussion point.
---
Topic: {topic}
""",
),
(
"human",
"""Please write search keywords in python list like ["keyword1", "keyword2", "keyword3"].
---
Discusion Point draft: {discussion_candidate}
""",
),
]
)
discussion_prompt_with_search = ChatPromptTemplate.from_messages(
[
(
"system",
"""You are Solar-Discussor, a smart discussion chatbot by Upstage, loved by many people.
You are taking about a topic and discussing with a user. Please participate in the discussion and provide engaging answers.
If necessasy, ask for more information or clarify the question or add follow-up questions.
If you find something wrong in others' discussion, correct them in a friendly manner in bold.
Do not talk beyond the topic and do not provide inappropriate language.
No need to agree on everything. You can have different opinions and discuss in a friendly manner.
Find contradictions and correct them in a harsh manner.It's OK to say I don't agree with you.
Speak shortly and clearly about 2~3 sentences.
Get to the point first and expand if necessary.
Count each turn and put [Turn n/10] at the only beginning of your discussion only once.
---
Topic: {topic}
""",
),
MessagesPlaceholder("chat_history"),
(
"human",
"""Based on your ciscussion draft, we did google search.
Please use the search result to enhance your original discussion draft if the information is relevant and useful.
If it is important, please add URL of the search result.
Using all these please focus on the discussion and provide engaging answers.
Don't thank the search result or mention the search result. Assume you already know these infomration.
Fully Focus on the discussion with human. Discuss based on the facts and information you have.
Please speak in a friendly and engaging manner. Speak shortly and clearly about 2~3 sentences.
Get to the point first and expand if necessary.
Count each turn and put [Turn n/10] at the only beginning of your discussion only once.
Please do only one turn discussion.
---
Discusion Draft: {discussion_candidate}
----
Search result: {external_information}
""",
),
]
)
discussion_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"""You are Solar-Discussor, a smart discussion chatbot by Upstage, loved by many people.
You are taking about a topic and discussing with a user. Please participate in the discussion and provide engaging answers.
If necessasy, ask for more information or clarify the question or add follow-up questions.
If you find something wrong in others' discussion, correct them in a friendly manner in bold.
Do not talk beyond the topic and do not provide inappropriate language.
Please speak in a friendly and engaging manner. Speak shortly and clearly about 2~3 sentences.
Get to the point first and expand if necessary.
Count each turn and put [Turn n/10] at the only beginning of your discussion only once.
Please do only one turn.
Do not repeat the same point already mentioned.
---
Topic: {topic}
""",
),
MessagesPlaceholder("chat_history"),
("human", "{discussion}"),
]
)
summary_prompt = ChatPromptTemplate.from_messages(
[
MessagesPlaceholder("chat_history"),
(
"human",
"""
You are Solar-Discussor, a smart discussion chatbot by Upstage, loved by many people.
By reading the discussion, provide comprehensive summarize of the discussion and provide a conclusion.
Only use previous discussion and do not add new information.
Highlight several sentences if necessary.
---
Topic: {topic}
---
Please summarize the discussion in history.""",
),
]
)
def make_human_last_in_history(chat_history):
# No need to change if the last message is from human
if not chat_history:
return []
if not isinstance(chat_history[-1], AIMessage):
return chat_history
return [
(
HumanMessage(content=chat.content)
if isinstance(chat, AIMessage)
else AIMessage(content=chat.content)
)
for chat in chat_history
]
def get_discussion_draft(topic, discussion, chat_history):
chain = discussion_prompt | llm | StrOutputParser()
discussion_candidate = chain.invoke(
{
"chat_history": chat_history,
"topic": topic,
"discussion": discussion,
}
)
st.write(discussion_candidate)
return discussion_candidate
def extract_search_keywords(topic, discussion_candidate):
parser = JsonOutputParser(pydantic_object=SearchKeyword)
keyword_chain = search_keyword_extraction | llm | parser
try:
search_keywords = keyword_chain.invoke(
{
"topic": topic,
"format_instructions": parser.get_format_instructions(),
"discussion_candidate": discussion_candidate,
}
)
st.write(search_keywords)
return search_keywords
except Exception as e:
st.error(f"Error extracting search keywords: {str(e)}")
return []
def perform_search(search_keywords):
if not search_keywords:
return []
or_merged_search_query = " OR ".join(search_keywords)
try:
search_results = ddg_search.invoke(or_merged_search_query, max_results=3)
st.write(search_results)
return search_results
except Exception as e:
st.error(f"Error performing search: {str(e)}")
return []
def get_discussion(topic, discussion, chat_history, use_search=True):
new_chat_history = make_human_last_in_history(chat_history)
if use_search:
with st.status("Writing discussion draft"):
discussion_candidate = get_discussion_draft(
topic, discussion, new_chat_history
)
with st.status("Extracting search keywords"):
search_keywords = extract_search_keywords(topic, discussion_candidate)
with st.status("Searching information"):
search_results = perform_search(search_keywords)
search_result_summary = str(search_results)[:3000]
chain = discussion_prompt_with_search | llm | StrOutputParser()
return chain.stream(
{
"chat_history": new_chat_history,
"topic": topic,
"discussion_candidate": discussion,
"external_information": search_result_summary,
}
)
chain = discussion_prompt | llm | StrOutputParser()
return chain.stream(
{
"chat_history": new_chat_history,
"topic": topic,
"discussion": discussion,
}
)
def get_summary(topic, chat_history):
chain = summary_prompt | llm | StrOutputParser()
return chain.stream(
{
"chat_history": chat_history,
"topic": topic,
}
)
if "messages" not in st.session_state:
st.session_state.messages = []
if False:
for message in st.session_state.messages:
role = "AI" if isinstance(message, AIMessage) else "Human"
with st.chat_message(role):
st.markdown(message.content)
topic = st.text_input("Discussion Topic", "How can I win LLM/AI hackathon?")
use_search = st.toggle("Use Search", False)
if st.button("Start Discussion"):
st.session_state.messages = []
previous_discussion = ""
for i in range(5):
with st.chat_message("user"):
discussion = st.write_stream(
get_discussion(
topic,
previous_discussion,
st.session_state.messages,
use_search,
)
)
st.session_state.messages.append(HumanMessage(content=discussion))
if discussion.startswith("[Turn 10/10]"):
break
with st.chat_message("assistant"):
previous_discussion = st.write_stream(
get_discussion(topic, discussion, st.session_state.messages, use_search)
)
st.session_state.messages.append(AIMessage(content=previous_discussion))
if previous_discussion.startswith("[Turn 10/10]"):
break
## summarize the discussion
with st.chat_message("user"):
st.write_stream(get_summary(topic, st.session_state.messages))