-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
213 lines (178 loc) · 8.69 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
"""
Multilingual Retrieval Augmented Generation demo built with Cohere, Weaviate and Streamlit.
It implements Semantic Search on Wikipedia Articles using 10 million vector embeddings.
This demo illustrates a three step approach (Pre-Search, Rank, Generation):
- Step 1: Pre-Search on Weaviate with Sparse Retrival (bm25), Dense Retrieval (neartext), or Hybrid Mode (bm25 + neartext).
- Step 2: Cohere Rank Model re-organizes the Pre-Search by assigning a relevance score to each Pre-Search result given the query.
- Step 3: Cohere Generation Model composes a response based on the ranked results.
Author:
@dcarpintero : https://github.com/dcarpintero
"""
import streamlit as st
import wikipedia
st.set_page_config(
page_title="Wikipedia Semantic Search",
page_icon="📚",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
"About": "Built by @dcarpintero with Cohere and Weaviate"},
)
@st.cache_resource(show_spinner=False)
def load_semantic_engine():
try:
return wikipedia.SearchEngine()
except (OSError, EnvironmentError) as e:
st.error(f'Semantic Engine Error {e}')
st.stop()
wikisearch = load_semantic_engine()
@st.cache_data
def query_bm25(query, lang='en', top_n=10):
try:
return wikisearch.with_bm25(query, lang=lang, top_n=top_n)
except (Exception) as e:
st.error(f'Querying Engine Error {e}')
@st.cache_data
def query_neartext(query, lang='en', top_n=10):
try:
return wikisearch.with_neartext(query, lang=lang, top_n=top_n)
except (Exception) as e:
st.error(f'Querying Engine Error {e}')
@st.cache_data
def query_hybrid(query, lang='en', top_n=10):
try:
return wikisearch.with_hybrid(query, lang=lang, top_n=top_n)
except (Exception) as e:
st.error(f'Querying Engine Error {e}')
def query_llm(context, query, temperature, model, lang="english"):
try:
response = wikisearch.with_llm(
context=context, query=query, temperature=temperature, model=model, lang=lang)
text = response.generations[0].text
return text
except (Exception) as e:
st.error(f'Querying Engine Error {e}')
def onclick_sample_query(query):
st.session_state.user_query_txt = query
languages = {
'Arabic': 'ar',
'Chinese': 'zh',
'English': 'en',
'French': 'fr',
'German': 'de',
'Hindi': 'hi',
'Italian': 'it',
'Japanese': 'ja',
'Korean': 'ko',
'Spanish': 'es'
}
samples = {
'q1': 'Who invented the printing press, what was the key development for this?',
'q2': 'What are the top 3 highest mountains on Earth?',
'q3': 'Who was the first person to win two Nobel prizes?',
'q4': 'Where and when were celebrated the first olimpic games?',
}
# -----------------------------------------------------------------------------
# Sidebar Section
# -----------------------------------------------------------------------------
with st.sidebar.expander("🤖 COHERE-SETTINGS", expanded=True):
lang = st.selectbox("Language", list(languages.keys()), index=2)
lang_code = languages.get(lang)
gen_model = st.selectbox("Generation Model", [
"command", "command-light", "command-nightly"], key="gen-model", index=0)
rank_model = st.selectbox("Rank Model", [
"rerank-multilingual-v2.0", "rerank-english-v2.0"], key="rank-model", index=0)
temperature = st.slider('Temperature', min_value=0.0,
max_value=1.0, value=0.30, step=0.05)
max_results = st.slider('Max Results', min_value=0,
max_value=15, value=10, step=1)
with st.sidebar.expander("🔧 WEAVIATE-SETTINGS", expanded=True):
options = ["Dense Retrieval", "Keyword Search", "Hybrid Mode"]
search_mode = st.radio("Select your preferred Search Mode:", options, key="search-mode", index=0)
st.info("ℹ️ Note that *Dense Retrieval* and *Hybrid* outperform *Keyword Search* on complex queries!")
with st.expander("ℹ️ ABOUT-THIS-APP", expanded=False):
st.write("""
- Multilingual RAG built with *Cohere* and the *Weaviate* demo database containing 10M Wikipedia embedding vectors (October 9th, 2021).
- Step 1: Pre-Search on *Weaviate* with Sparse Retrival (bm25), Dense Retrieval (neartext), or Hybrid Mode (bm25 + neartext).
- Step 2: *Cohere Rank Model* re-organizes the Pre-Search by assigning a relevance score to each Pre-Search result given the query.
- Step 3: *Cohere Generation Model* composes a response based on the ranked results.
- Ask in your preferred language, and experiment with the settings!
""")
with st.sidebar:
col_gh, col_co, col_we = st.columns([1, 1, 1])
with col_gh:
"[](https://github.com/dcarpintero/wikisearch)"
with col_co:
"[](https://cohere.com/?ref=https://github.com/dcarpintero)"
with col_we:
"[](https://weaviate.io/?ref=https://github.com/dcarpintero)"
# -----------------------------------------------------------------------------
# Ask Wikipedia Section
# -----------------------------------------------------------------------------
st.subheader("🪄 Wikipedia Semantic Search")
query = st.text_input(label="Ask 'Wikipedia'", placeholder='Ask your question here, or select one from the examples below',
key="user_query_txt", label_visibility="hidden")
btn_printing = st.session_state.get("btn_printing", False)
btn_nobel = st.session_state.get("btn_nobel", False)
btn_internet = st.session_state.get("btn_internet", False)
btn_ai = st.session_state.get("btn_ai", False)
col1, col2, col3, col4 = st.columns([1, 1, 1, 1])
with col1:
if st.button(label=samples["q1"], type="primary", disabled=btn_printing, on_click=onclick_sample_query, args=[samples["q1"]]):
st.session_state.btn_printing = True
with col2:
if st.button(label=samples["q2"], type="primary", disabled=btn_nobel, on_click=onclick_sample_query, args=[samples["q2"]]):
st.session_state.btn_nobel = True
with col3:
if st.button(label=samples["q3"], type="primary", disabled=btn_internet, on_click=onclick_sample_query, args=[samples["q3"]]):
st.session_state.btn_internet = True
with col4:
if st.button(label=samples["q4"], type="primary", disabled=btn_ai, on_click=onclick_sample_query, args=[samples["q4"]]):
st.session_state.btn_ai = True
if query:
if search_mode == "Dense Retrieval":
data = query_neartext(query, lang=lang_code, top_n=max_results)
elif search_mode == "Keyword Search":
data = query_bm25(query, lang=lang_code, top_n=max_results)
elif search_mode == "Hybrid Mode":
data = query_hybrid(query, lang=lang_code, top_n=max_results)
else:
st.info(
"ℹ️ Select your preferred Search Mode (Dense Retrieval, Keyword Search, or Hybrid)!")
st.stop()
st.divider()
col1, col2, col3 = st.columns([1, 1, 1])
with col1:
st.subheader("🔎 1. Pre-Search")
if data is None:
st.warning(
"⚠️ No results found! Note that this App uses a Wikipedia subset")
st.stop()
for idx, doc in enumerate(data):
with st.expander(f'**{doc["title"]} [Rank: {idx+1}**]', expanded=False):
st.markdown(
f'"*{doc["text"][:800]} [...]*" [Source]({doc["url"]})')
with col2:
st.subheader("🏆 2. Ranking")
with st.spinner("Reranking..."):
data_ranked = wikisearch.rerank(
query=query, documents=data, top_n=max_results, model=rank_model)
if data_ranked is None:
st.warning(
"⚠️ No results found! Note that this App uses a Wikipedia subset")
st.stop()
for idx, r in enumerate(data_ranked):
doc = r.document
expanded = False
if idx == 0:
expanded = True
with st.expander(f'**{doc["title"]} [Previous Rank: {r.index + 1} - Relevance: {r.relevance_score:.3f}**]', expanded=expanded):
st.markdown(
f'"*{doc["text"][:800]} [...]*" [Source]({doc["url"]})')
with col3:
st.subheader("📝 3. LLM Generation")
with st.spinner("Deep Diving..."):
r = query_llm(context=data_ranked[:5], query=query,
temperature=temperature, model=gen_model, lang=lang)
st.success(f"🪄 {r}")
st.info("ℹ️ Some references might appear to be duplicated while referring to different paragraphs of the same article.")