-
Notifications
You must be signed in to change notification settings - Fork 38
/
streamlit_app.py
463 lines (418 loc) · 17.7 KB
/
streamlit_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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
from st_weaviate_connection import WeaviateConnection
import streamlit as st
import time
import sys
import os
from dotenv import load_dotenv
load_dotenv()
# Constants
ENV_VARS = ["WEAVIATE_URL", "WEAVIATE_API_KEY", "OPENAI_KEY"]
NUM_IMAGES_PER_ROW = 3
# Functions
def get_env_vars(env_vars: list) -> dict:
"""Retrieve environment variables
@parameter env_vars : list - List containing keys of environment variables
@returns dict - A dictionary of environment variables
"""
env_vars = {}
for var in ENV_VARS:
value = os.environ.get(var, "")
if value == "":
st.error(f"{var} not set", icon="🚨")
sys.exit(f"{var} not set")
env_vars[var] = value
return env_vars
def display_chat_messages() -> None:
"""Print message history
@returns None
"""
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if "images" in message:
for i in range(0, len(message["images"]), NUM_IMAGES_PER_ROW):
cols = st.columns(NUM_IMAGES_PER_ROW)
for j in range(NUM_IMAGES_PER_ROW):
if i + j < len(message["images"]):
cols[j].image(message["images"][i + j], width=200)
# Environment variables
env_vars = get_env_vars(ENV_VARS)
url = env_vars["WEAVIATE_URL"]
api_key = env_vars["WEAVIATE_API_KEY"]
openai_key = env_vars["OPENAI_KEY"]
# Check keys
if url == "" or api_key == "" or openai_key == "":
st.error(f"Environment variables not set", icon="🚨")
sys.exit("Environment variables not set")
# Title
st.title("🔮 Magic Chat")
# Connection to Weaviate thorugh Connector
conn = st.connection(
"weaviate",
type=WeaviateConnection,
url=os.getenv("WEAVIATE_URL"),
api_key=os.getenv("WEAVIATE_API_KEY"),
additional_headers={"X-OpenAI-Api-Key": openai_key},
)
with st.sidebar:
st.title("🔮 Magic Chat")
st.subheader("The Generative Gathering")
st.markdown(
"""Magic Chat is a chatbot built with [Streamlit](https://streamlit.io/) and [Weaviate](https://weaviate.io/) to search for [Magic the Gathering](https://magic.wizards.com/en) cards.
It offers multiple search options such as traditional BM25, Semantic, Hybrid, and Generative Search to find cards for your dream deck.
Whether you're looking for blue cards to nullify spells, black cards to create an undead army, or just want to find new cool cards!
Our Weaviate database contains 27k cards from the [Scryfall API](https://scryfall.com/) ready for you to be discovered."""
)
st.header("Settings")
st.success("Connected to Weaviate client", icon="💚")
# Search Mode descriptions
bm25_gql = """
{{
Get {{
MagicChat_Card(limit: {limit_card}, bm25: {{ query: "{input}" }})
{{
name
card_id
img
mana_cost
type
mana_produced
power
toughness
color
keyword
set
rarity
description
_additional {{
id
distance
vector
}}
}}
}}
}}"""
vector_gql = """
{{
Get {{
MagicChat_Card(limit: {limit_card}, nearText: {{ concepts: ["{input}"] }})
{{
name
card_id
img
mana_cost
type
mana_produced
power
toughness
color
keyword
set
rarity
description
_additional {{
id
distance
vector
}}
}}
}}
}}"""
hybrid_gql = """
{{
Get {{
MagicChat_Card(limit: {limit_card}, hybrid: {{ query: "{input}" alpha:0.5 }})
{{
name
card_id
img
mana_cost
type
mana_produced
power
toughness
color
keyword
set
rarity
description
_additional {{
id
distance
vector
}}
}}
}}
}}"""
generative_gql = """
{{
Get {{
MagicChat_Card(limit: {limit_card}, nearText: {{ concepts: ["{input}"] }})
{{
name
card_id
img
mana_cost
type
mana_produced
power
toughness
color
keyword
set
rarity
description
_additional {{
generate(
groupedResult: {{
task: "Based on the Magic The Gathering Cards, which one would you recommend and why. Use the context of the user query: {input}"
}}
) {{
groupedResult
error
}}
id
distance
vector
}}
}}
}}
}}"""
mode_descriptions = {
"BM25": [
"BM25 is a method used by search engines to rank documents based on their relevance to a given query, factoring in both the frequency of keywords and the length of the document.",
bm25_gql,
30,
],
"Vector": [
"Vector search is a method used by search engines to find and rank results based on their similarity to your search query. Instead of just matching keywords, it understands the context and meaning behind your search, offering more relevant and nuanced results.",
vector_gql,
15,
],
"Hybrid": [
"Hybrid search combines vector and BM25 methods to offer better search results. It leverages the precision of BM25's keyword-based ranking with vector search's ability to understand context and semantic meaning. Providing results that are both directly relevant to the query and contextually related.",
hybrid_gql,
15,
],
"Generative": [
"Generative search is an advanced method that combines information retrieval with AI language models. After finding relevant documents using search techniques like vector and BM25, the found information is used as an input to a language model, which generates further contextually related information.",
generative_gql,
9,
],
}
# Information
with st.expander("Built with Weaviate for the Streamlit Hackathon 2023"):
st.subheader("Streamlit Hackathon 2023")
st.markdown(
"""
This project is a submission for the [Streamlit Connections Hackathon 2023](https://discuss.streamlit.io/t/connections-hackathon/47574).
It delivers a Streamlit connector for the open-source vector database, [Weaviate](https://weaviate.io/).
Magic Chat uses the Weaviate connector to search through [Magic The Gathering](https://magic.wizards.com/en) cards with various search options, such as BM25, Semantic Search, Hybrid Search and Generative Search.
You can find the submission in this [GitHub repo](https://github.com/weaviate/st-weaviate-connection/tree/main)
"""
)
st.subheader("Data")
st.markdown(
"""The database contains around 27k cards from the [Scryfall API](https://scryfall.com/). We used the following attributes to index the cards:
- Name, Type, Keywords
- Mana cost, Mana produced, Color
- Power, Toughness, Rarity
- Set name and Card description """
)
st.subheader("How the demo works")
st.markdown(
"""The demo offers four search options with defined GraphQL queries, which you can use to search for various Magic cards.
You can search for card types such as "Vampires", "Humans", "Wizards", or you can search for abilities, mana color, descriptions, and more.
If you're interested you can read more about the anatomy of a Magic card in this [documentation from the Magic Academy](https://magic.wizards.com/en/news/feature/anatomy-magic-card-2006-10-21).
"""
)
st.markdown(
"""The first is the **BM25 search**,
it's a method used by search engines to rank documents based on their relevance to a given query,
factoring in both the frequency of keywords and the length of the document. In simple terms, we're conducting keyword matching.
We can simply pass a query to the `query` parameter ([see docs](https://weaviate.io/developers/weaviate/search/bm25))"""
)
st.code(
"""
{
Get {
MagicChat_Card(limit: {card_limit}, bm25: { query: "Vampires with flying ability" })
{
...
}
}
}""",
language="graphql",
)
st.markdown(
"""The second option is **Vector search**, a method used to find and rank results based on their similarity to a given search query.
Instead of matching keywords, it understands the context and meaning behind the query, offering relevant and more semantic related results. For example, when we search for "Vampires" we might also get cards like a "Bat" or "Undead" because they are semantically related.
We use the `nearText` function in which we pass our query to the `concepts` parameter ([see docs](https://weaviate.io/developers/weaviate/api/graphql/search-operators#neartext))"""
)
st.code(
"""
{
Get {
MagicChat_Card(limit: {card_limit}, nearText: { concepts: ["Vampires with flying ability"] })
{
...
}
}
}""",
language="graphql",
)
st.markdown(
"""With **Hybrid search** we combine both methods and use a ranking alogrithm to combine their results.
It leverages the precision of BM25's keyword-based ranking with vector search's ability to understand context and semantic meaning.
We can pass our query to the `query` parameter and set the `alpha` that determines the weighting for each search ([see docs](https://weaviate.io/developers/weaviate/api/graphql/search-operators#hybrid))"""
)
st.code(
"""
{
Get {
MagicChat_Card(limit: {card_limit}, hybrid: { query: "Vampires with flying ability" alpha:0.5 })
{
...
}
}
}""",
language="graphql",
)
st.markdown(
"""The last option is **Generative search** which is an advanced method that combines information retrieval with AI language models.
In our configuration, it retrieves results with a **Vector search** and passes them to a `gpt-3.5-turbo model` to determine the best Magic card based on the user query. For this, we rely on its knowledge about Magic The Gathering, but this model could be easily exchanged.
We use the `generate` module and `groupedResult` task which uses the data of the result as context for the given prompt and query. ([see docs](https://weaviate.io/developers/weaviate/modules/reader-generator-modules/generative-openai))"""
)
st.code(
"""
{
Get {
MagicChat_Card(limit: {card_limit}, nearText: { concepts: ["Vampires with flying ability"] })
{
_additional {
generate(
groupedResult: {
task: "Based on the Magic The Gathering Cards, which one would you recommend and why. Use the context of the user query: {input}"
}
) {
groupedResult
error
}
}
}
}
}""",
language="graphql",
)
st.subheader("Future ideas")
st.markdown(
"""Magic The Gathering is a very complex and exciting game, and there were many ideas we got when building this demo.
We thought about an actual deck builder interface, where you could search for similar cards or predict the next card based on your already established deck.
Or even a meta-finder that simulates battles between two sets of cards, trying out different combinations and more. With Magic, the possibilities for an exciting demo are endless, and we hope to enhance the demo further!
"""
)
col1, col2, col3 = st.columns([0.2, 0.5, 0.2])
col2.image("./img/anim.gif")
# User Configuration Sidebar
with st.sidebar:
mode = st.radio(
"Search Mode", options=["BM25", "Vector", "Hybrid", "Generative"], index=3
)
limit = st.slider(
label="Number of cards",
min_value=1,
max_value=mode_descriptions[mode][2],
value=6,
)
st.info(mode_descriptions[mode][0])
st.divider()
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.greetings = False
# Display chat messages from history on app rerun
display_chat_messages()
# Greet user
if not st.session_state.greetings:
with st.chat_message("assistant"):
intro = "Hey! I am Magic Chat, your assistant for finding the best Magic The Gathering cards to build your dream deck. Let's get started!"
st.markdown(intro)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": intro})
st.session_state.greetings = True
# Example prompts
example_prompts = [
"You gain life and enemy loses life",
"Vampires cards with flying ability",
"Blue and green colored sorcery cards",
"White card with protection from black",
"The famous 'Black Lotus' card",
"Wizard card with Vigiliance ability",
]
example_prompts_help = [
"Look for a specific card effect",
"Search for card type: 'Vampires', card color: 'black', and ability: 'flying'",
"Color cards and card type",
"Specifc card effect to another mana color",
"Search for card names",
"Search for card types with specific abilities",
]
button_cols = st.columns(3)
button_cols_2 = st.columns(3)
button_pressed = ""
if button_cols[0].button(example_prompts[0], help=example_prompts_help[0]):
button_pressed = example_prompts[0]
elif button_cols[1].button(example_prompts[1], help=example_prompts_help[1]):
button_pressed = example_prompts[1]
elif button_cols[2].button(example_prompts[2], help=example_prompts_help[2]):
button_pressed = example_prompts[2]
elif button_cols_2[0].button(example_prompts[3], help=example_prompts_help[3]):
button_pressed = example_prompts[3]
elif button_cols_2[1].button(example_prompts[4], help=example_prompts_help[4]):
button_pressed = example_prompts[4]
elif button_cols_2[2].button(example_prompts[5], help=example_prompts_help[5]):
button_pressed = example_prompts[5]
if prompt := (st.chat_input("What cards are you looking for?") or button_pressed):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
prompt = prompt.replace('"', "").replace("'", "")
images = []
if prompt != "":
query = prompt.strip().lower()
gql = mode_descriptions[mode][1].format(input=query, limit_card=limit)
df = conn.query(gql, ttl=None)
response = ""
with st.chat_message("assistant"):
for index, row in df.iterrows():
if index == 0:
if "_additional.generate.groupedResult" in row:
first_response = row["_additional.generate.groupedResult"]
else:
first_response = f"Here are the results from the {mode} search:"
message_placeholder = st.empty()
full_response = ""
for chunk in first_response.split():
full_response += chunk + " "
time.sleep(0.02)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
response += full_response + " "
# Create a new row of columns for every NUM_IMAGES_PER_ROW images
if index % NUM_IMAGES_PER_ROW == 0:
cols = st.columns(NUM_IMAGES_PER_ROW)
if row["img"]:
# Display image in the column
cols[index % NUM_IMAGES_PER_ROW].image(row["img"], width=200)
images.append(row["img"])
else:
cols[index % NUM_IMAGES_PER_ROW].write(
f"No Image Available for: {row['type']}"
)
st.session_state.messages.append(
{"role": "assistant", "content": response, "images": images}
)
st.experimental_rerun()