-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
392 lines (310 loc) · 11.2 KB
/
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
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
from langchain.callbacks.base import BaseCallbackHandler
from langchain.chat_models import ChatOpenAI
from langchain.schema import ChatMessage
from langchain.prompts.chat import (
SystemMessagePromptTemplate,
)
import streamlit as st
import random
import time
from typing import Dict, TypedDict
import re
import os
# Custom select component
from avatarselect import avatarselect
"""
# 👨👩👧👦 GroupChatGPT
"""
HOST = "https://groupchatgpt.streamlit.app"
class Character(TypedDict):
value: str
display_name: str
avatar: str
CHARACTERS: Dict[str, Character] = {
"rick": {
"value": "rick",
"display_name": "Rick Sanchez",
"avatar": "https://i.imgur.com/LwYm9Jk.png",
},
"barbie": {
"value": "barbie",
"display_name": "Barbie",
"avatar": "https://i.imgur.com/tyJajYs.png",
},
"yoda": {
"value": "yoda",
"display_name": "Yoda",
"avatar": "https://i.imgur.com/z29bSou.png",
},
"cartman": {
"value": "cartman",
"display_name": "Eric Cartman",
"avatar": "https://i.imgur.com/MSEbuEU.png",
},
"stewie": {
"value": "stewie",
"display_name": "Stewie Griffin",
"avatar": "https://i.imgur.com/zIwZH3q.png",
},
"batman": {
"value": "batman",
"display_name": "Batman",
"avatar": "https://i.imgur.com/kTtbJLJ.png",
},
"morty": {
"value": "morty",
"display_name": "Morty",
"avatar": "https://i.imgur.com/QMqm5rC.png",
},
"spongebob": {
"value": "spongebob",
"display_name": "Spongebob Squarepants",
"avatar": "https://i.imgur.com/i6O9v2H.png",
},
"ken": {
"value": "ken",
"display_name": "Ken Doll",
"avatar": "https://i.imgur.com/ENnWII9.png",
},
"pooh": {
"value": "pooh",
"display_name": "Winnie the Pooh",
"avatar": "https://i.imgur.com/WL5hhce.png",
},
"oppenheimer": {
"value": "oppenheimer",
"display_name": "Robert Oppenheimer",
"avatar": "https://i.imgur.com/qVYBzk4.png",
},
}
from supabase import create_client
# Initialize DB connection on app load
@st.cache_resource
def init_connection():
url = st.secrets["supabase_url"]
key = st.secrets["supabase_key"]
return create_client(url, key)
supabase = init_connection()
st.markdown(
"""
<style>
div[data-testid="toastContainer"] {
z-index: 999992;
}
div[data-testid="stToast"] {
border: 2px solid rgb(0, 66, 128);
width: 100%;
}
</style>
""",
unsafe_allow_html=True,
)
# Example: https://groupchatgpt.streamlit.io/?chat=123
qp = st.experimental_get_query_params()
url_chat_id = qp.get("chat", [None])[0]
RENDER_READONLY = bool(url_chat_id)
if url_chat_id:
data, _ = (
supabase.table("messages")
.select("*")
.eq("chat_id", url_chat_id)
.order("order")
.execute()
)
st.session_state["messages"] = [
ChatMessage(
role="user",
content=msg["content"],
additional_kwargs={
"character": CHARACTERS[msg["character"]]
if msg["character"] != "user"
else {}
},
)
for msg in data[1]
]
st.session_state["chat_id"] = url_chat_id
if not st.session_state.get("has_started_conversation"):
st.session_state["chosen_characters"] = {}
if not RENDER_READONLY:
if not st.session_state.get("has_started_conversation"):
st.info(
"Choose your friends! Then start by asking a question or just saying hi!"
)
avatarselect_placeholder = st.empty()
with avatarselect_placeholder:
chosen_character_values = avatarselect(CHARACTERS)
if chosen_character_values:
for value in chosen_character_values:
st.session_state.chosen_characters[value] = CHARACTERS[value]
else:
avatarselect_placeholder = st.empty()
def get_response_prefix(text):
"""
Rick Sanchez: lorem ipsum -> Rick Sanchez
Winnie the Pooh: lorem ipsum -> Winnie the Pooh
lorem ipsum -> None
"""
pattern = r"^\s*([\w\s]+(?=:))"
match = re.match(pattern, text)
if match:
return match.group(1).strip()
else:
return None
def replace_prefix(text, character):
_text = text
prefix = get_response_prefix(_text)
if not prefix:
return _text
if (
prefix == character.get("display_name")
or prefix in character.get("display_name", "").split(" ")
or prefix == "Friend"
):
_text = _text.replace(prefix + ":", "")
return _text
class StreamHandler(BaseCallbackHandler):
def __init__(self, container, character, initial_text=""):
self.character = character
self.container = container
self.text = initial_text
def on_llm_new_token(self, token: str, **kwargs) -> None:
self.text += token
self.text = replace_prefix(self.text, self.character)
# This is to avoid showing the prefix in the partial stream
if len(self.text) < len(self.character["display_name"]) + 2:
return
self.container.markdown(self.text)
def share_conversation():
chat_id = st.session_state.get("chat_id")
if chat_id:
# Replace existing chat
supabase.table("chats").delete().eq("id", chat_id).execute()
data, _ = supabase.table("chats").insert({"id": chat_id}).execute()
else:
# Create new chat
data, _ = supabase.table("chats").insert({}).execute()
chat_id = str(data[1][0]["id"])
st.session_state["chat_id"] = chat_id
for count, msg in enumerate(st.session_state.messages):
character = msg.additional_kwargs.get("character", {})
data, _ = (
supabase.table("messages")
.insert(
{
"chat_id": chat_id,
"content": msg.content,
"character": character.get("value", "user"),
"order": count,
}
)
.execute()
)
st.toast(f"Share link: {HOST}?chat=" + str(chat_id), icon="🔗")
if not RENDER_READONLY:
with st.sidebar:
if os.getenv("STREAMLIT_LOCAL"):
openai_api_key = os.getenv("OPEN_AI_KEY")
st.text_input("Using Env OpenAI API Key", disabled=True)
else:
openai_api_key = st.text_input("OpenAI API Key", type="password")
st.button("Share conversation", on_click=share_conversation)
else:
with st.sidebar:
def new_conversation():
st.session_state["chat_id"] = None
st.session_state["messages"] = []
st.experimental_set_query_params()
def fork_conversation():
st.session_state["chat_id"] = None
st.experimental_set_query_params()
st.button("Start new chat", on_click=new_conversation, use_container_width=True)
st.button("Fork chat", on_click=fork_conversation, use_container_width=True)
if "messages" not in st.session_state:
st.session_state["messages"] = []
for msg in st.session_state.messages:
character = msg.additional_kwargs.get("character", {})
value = character.get("value", "user")
avatar = character.get("avatar", None)
content = replace_prefix(msg.content, character)
st.chat_message(
value,
avatar=avatar,
).write(content)
CHARACTER_PROMPT_TEMPLATE = """You are {display_name}. Do not go off character.
Do not start the message with your name. Always respond as if you are {display_name}.
You are in a group setting, so please make sure to be conversational. Lean into your personality and perspective as much as possible.
Keep your responses under 60 words please."""
RETORT_PROMPT = "Make a critical comment about the last thing the chat said and follow up with an interesting question that spices up the conversation."
def get_number_of_bot_responses(*, prompt=""):
# If there is only one character, they can't talk with themselves!
if len(st.session_state.chosen_characters.keys()) < 2:
return 1
return (len(prompt) % 2) * 2 + 3
def get_character_system_message(*, character: Character):
character_prompt_template = SystemMessagePromptTemplate.from_template(
template=CHARACTER_PROMPT_TEMPLATE
)
return character_prompt_template.format(display_name=character["display_name"])
def pick_character(*, exclude=[]) -> Character:
character_list = [
x for x in list(st.session_state.chosen_characters.keys()) if x not in exclude
]
key = random.choice(character_list)
return st.session_state.chosen_characters[key]
def reduce_messages_below_token_limit(llm, messages):
buffer = messages.copy()
curr_buffer_length = llm.get_num_tokens_from_messages(messages)
max_token_limit = 3000
while curr_buffer_length > max_token_limit and len(buffer) > 0:
buffer = buffer[1:]
curr_buffer_length = llm.get_num_tokens_from_messages(buffer)
return buffer
if not RENDER_READONLY:
if prompt := st.chat_input():
if not openai_api_key:
st.warning("Please add your OpenAI API key to continue.")
st.stop()
if len(st.session_state.chosen_characters.keys()) == 0:
st.warning("Please select at least one character to continue.")
st.stop()
st.session_state["has_started_conversation"] = True
avatarselect_placeholder.empty()
st.session_state.messages.append(
ChatMessage(role="user", content=f"Friend: {prompt}")
)
st.chat_message("user").write(prompt)
number_of_bot_responses = get_number_of_bot_responses(prompt=prompt)
for i in range(number_of_bot_responses):
previous_character = (
st.session_state.messages[-1]
.additional_kwargs.get("character", {})
.get("value")
)
character = pick_character(exclude=[previous_character])
system_message = get_character_system_message(character=character)
if i % 2 == 1:
system_message.content = system_message.content + " " + RETORT_PROMPT
messages = [system_message] + reduce_messages_below_token_limit(
ChatOpenAI(openai_api_key=openai_api_key), st.session_state.messages
)
with st.chat_message(
character["value"], avatar=character.get("avatar", None)
):
with st.spinner(""):
time.sleep(2)
stream_handler = StreamHandler(st.empty(), character)
llm = ChatOpenAI(
openai_api_key=openai_api_key,
streaming=True,
callbacks=[stream_handler],
)
response = llm(messages)
answer = replace_prefix(response.content, character)
st.session_state.messages.append(
ChatMessage(
role="user",
content=f"{character['display_name']}: {answer}",
additional_kwargs={"character": character},
)
)