Gradio chat working with indexes drop down
Browse files
app.ipynb
CHANGED
@@ -79,51 +79,56 @@
|
|
79 |
},
|
80 |
{
|
81 |
"cell_type": "code",
|
82 |
-
"execution_count":
|
83 |
"metadata": {},
|
84 |
"outputs": [
|
85 |
{
|
86 |
"name": "stdout",
|
87 |
"output_type": "stream",
|
88 |
"text": [
|
89 |
-
"Using
|
90 |
-
"Using index: rag-demo-1-history-rome\n"
|
91 |
-
"Test questions: When was Ceasar born?\n",
|
92 |
-
"page_content='The situation in Rome. While Pompey was adding to his\\nmilitary reputation in the East he was regarded with jealous\\nand anxious eyes not only by the Senate but also by the other\\nchampions of the popular party, Crassus who found his wealth\\nno match for Pompeys military achievements, and Caius Julius\\nCaesar who was rapidly coming to be one of the leading figures in\\nRoman public life. Caesar was born in 100 B. C., of the patrician [162]\\ngens of the Julii, but since his aunt was the wife of Marius,\\nand he himself had married the daughter of Cinna, his lot was\\ncast with the Populares. As a young man he had distinguished\\nhimself by refusing to divorce his wife at Sullas behest, whereat\\nSulla was with difficulty induced to spare his life, saying that he\\nsaw in him many a Marius. For the time being Caesar judged it\\nprudent to withdraw from Rome to Rhodes. While in the East\\nhe was captured by pirates, and after being ransomed, fulfilled' metadata={'source': '..\\\\rag-demo-1-data\\\\history-roman\\\\3. A History of Rome to 565 A. D. author Arthur Edward Romilly Boak.pdf.txt'}\n"
|
93 |
]
|
94 |
}
|
95 |
],
|
96 |
"source": [
|
97 |
"embeddings = OpenAIEmbeddings()\n",
|
98 |
"\n",
|
99 |
-
"
|
100 |
-
"
|
101 |
-
"
|
102 |
-
" apiKeyKey = \"PINECONE_API_KEY_2\"\n",
|
103 |
-
" apiEnvKey = \"PINECONE_API_ENV_2\"\n",
|
104 |
-
" testQuestion = \"When was Ceasar born?\"\n",
|
105 |
-
"else:\n",
|
106 |
-
" print(f\"Using course: {course}\")\n",
|
107 |
-
" apiKeyKey = \"PINECONE_API_KEY\"\n",
|
108 |
-
" apiEnvKey = \"PINECONE_API_ENV\"\n",
|
109 |
-
" testQuestion = \"What is Moby Dick?\"\n",
|
110 |
" \n",
|
111 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
"\n",
|
113 |
-
"
|
114 |
-
"print(f
|
115 |
-
"
|
116 |
-
"
|
|
|
|
|
117 |
"\n",
|
118 |
-
"
|
119 |
-
"print(f'Test questions: {testQuestion}')\n",
|
120 |
-
"result = vector_store.similarity_search(query, k=3)\n",
|
121 |
-
"print(result[0])"
|
122 |
]
|
123 |
},
|
124 |
{
|
125 |
"cell_type": "code",
|
126 |
-
"execution_count":
|
127 |
"metadata": {},
|
128 |
"outputs": [],
|
129 |
"source": [
|
@@ -139,18 +144,9 @@
|
|
139 |
},
|
140 |
{
|
141 |
"cell_type": "code",
|
142 |
-
"execution_count":
|
143 |
"metadata": {},
|
144 |
-
"outputs": [
|
145 |
-
{
|
146 |
-
"name": "stderr",
|
147 |
-
"output_type": "stream",
|
148 |
-
"text": [
|
149 |
-
"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\components\\button.py:89: UserWarning: Using the update method is deprecated. Simply return a new object instead, e.g. `return gr.Button(...)` instead of `return gr.Button.update(...)`.\n",
|
150 |
-
" warnings.warn(\n"
|
151 |
-
]
|
152 |
-
}
|
153 |
-
],
|
154 |
"source": [
|
155 |
"from langchain.memory import ConversationBufferMemory\n",
|
156 |
"from langchain.chains import ConversationalRetrievalChain\n",
|
@@ -161,7 +157,7 @@
|
|
161 |
},
|
162 |
{
|
163 |
"cell_type": "code",
|
164 |
-
"execution_count":
|
165 |
"metadata": {},
|
166 |
"outputs": [
|
167 |
{
|
@@ -170,7 +166,7 @@
|
|
170 |
"' \\nCrassus was a Roman general who was killed in battle by the Parthians. He was killed while trying to extend the Roman Empire into the Middle East. Ceasar was avenging Crassus by trying to defeat the Parthians and expand the Roman Empire. He was also trying to avenge the death of his friend and mentor.'"
|
171 |
]
|
172 |
},
|
173 |
-
"execution_count":
|
174 |
"metadata": {},
|
175 |
"output_type": "execute_result"
|
176 |
}
|
@@ -201,7 +197,7 @@
|
|
201 |
},
|
202 |
{
|
203 |
"cell_type": "code",
|
204 |
-
"execution_count":
|
205 |
"metadata": {},
|
206 |
"outputs": [],
|
207 |
"source": [
|
@@ -213,8 +209,11 @@
|
|
213 |
"from langchain.memory import ConversationBufferMemory\n",
|
214 |
"import pinecone\n",
|
215 |
"\n",
|
216 |
-
"def create_conversation(query: str, chat_history: list) -> tuple
|
217 |
" try:\n",
|
|
|
|
|
|
|
218 |
" memory = ConversationBufferMemory(\n",
|
219 |
" memory_key='chat_history',\n",
|
220 |
" return_messages=False\n",
|
@@ -228,7 +227,7 @@
|
|
228 |
" )\n",
|
229 |
" result = cqa({'question': query, 'chat_history': chat_history})\n",
|
230 |
" chat_history.append((query, result['answer']))\n",
|
231 |
-
" return '', chat_history\n",
|
232 |
" except Exception as e:\n",
|
233 |
" chat_history.append((query, e))\n",
|
234 |
" return '', chat_history"
|
@@ -236,14 +235,30 @@
|
|
236 |
},
|
237 |
{
|
238 |
"cell_type": "code",
|
239 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
"metadata": {},
|
241 |
"outputs": [
|
242 |
{
|
243 |
"name": "stdout",
|
244 |
"output_type": "stream",
|
245 |
"text": [
|
246 |
-
"Running on local URL: http://127.0.0.1:
|
247 |
"\n",
|
248 |
"To create a public link, set `share=True` in `launch()`.\n"
|
249 |
]
|
@@ -251,7 +266,7 @@
|
|
251 |
{
|
252 |
"data": {
|
253 |
"text/html": [
|
254 |
-
"<div><iframe src=\"http://127.0.0.1:
|
255 |
],
|
256 |
"text/plain": [
|
257 |
"<IPython.core.display.HTML object>"
|
@@ -266,14 +281,15 @@
|
|
266 |
"\n",
|
267 |
"with gr.Blocks() as demo:\n",
|
268 |
" gr.Markdown(\"Chat to docs\")\n",
|
|
|
269 |
" chatbot = gr.Chatbot(label='Talk to the Doument')\n",
|
270 |
" msg = gr.Textbox()\n",
|
271 |
" \n",
|
272 |
" submitBtn = gr.Button(value=\"Submit\") \n",
|
273 |
" clear = gr.ClearButton([msg, chatbot])\n",
|
274 |
"\n",
|
275 |
-
" msg.submit(create_conversation, [msg, chatbot], [msg, chatbot])\n",
|
276 |
-
" submitBtn.click(create_conversation, [msg, chatbot], [msg, chatbot])\n",
|
277 |
"\n",
|
278 |
"if __name__ == '__main__':\n",
|
279 |
" demo.launch(show_error=True) #show_error=True, debug=True)"
|
|
|
79 |
},
|
80 |
{
|
81 |
"cell_type": "code",
|
82 |
+
"execution_count": 65,
|
83 |
"metadata": {},
|
84 |
"outputs": [
|
85 |
{
|
86 |
"name": "stdout",
|
87 |
"output_type": "stream",
|
88 |
"text": [
|
89 |
+
"Using vector store: Roman History\n",
|
90 |
+
"Using index: rag-demo-1-history-rome\n"
|
|
|
|
|
91 |
]
|
92 |
}
|
93 |
],
|
94 |
"source": [
|
95 |
"embeddings = OpenAIEmbeddings()\n",
|
96 |
"\n",
|
97 |
+
"vector_store = None\n",
|
98 |
+
"\n",
|
99 |
+
"def create_vector_store(indexToUse: str):\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
" \n",
|
101 |
+
" if indexToUse == \"Roman History\":\n",
|
102 |
+
" print(f\"Using vector store: {indexToUse}\")\n",
|
103 |
+
" apiKeyKey = \"PINECONE_API_KEY_2\"\n",
|
104 |
+
" apiEnvKey = \"PINECONE_API_ENV_2\"\n",
|
105 |
+
" testQuestion = \"When was Ceasar born?\"\n",
|
106 |
+
" else:\n",
|
107 |
+
" print(f\"Using vector store: {indexToUse}\")\n",
|
108 |
+
" apiKeyKey = \"PINECONE_API_KEY\"\n",
|
109 |
+
" apiEnvKey = \"PINECONE_API_ENV\"\n",
|
110 |
+
" testQuestion = \"What is Moby Dick?\"\n",
|
111 |
+
" \n",
|
112 |
+
" pinecone.init(api_key=os.environ[apiKeyKey], environment=os.environ[apiEnvKey])\n",
|
113 |
+
"\n",
|
114 |
+
" index_name = pinecone.list_indexes()[0]\n",
|
115 |
+
" print(f\"Using index: {index_name}\")\n",
|
116 |
+
" index = pinecone.Index(index_name)\n",
|
117 |
+
" vector_store = Pinecone(index, embeddings, \"text\") \n",
|
118 |
"\n",
|
119 |
+
" # query = testQuestion\n",
|
120 |
+
" # print(f'Test questions: {testQuestion}')\n",
|
121 |
+
" # result = vector_store.similarity_search(query, k=3)\n",
|
122 |
+
" # print(result[0])\n",
|
123 |
+
" \n",
|
124 |
+
" return vector_store\n",
|
125 |
"\n",
|
126 |
+
"vector_store = create_vector_store(\"Roman History\")"
|
|
|
|
|
|
|
127 |
]
|
128 |
},
|
129 |
{
|
130 |
"cell_type": "code",
|
131 |
+
"execution_count": 66,
|
132 |
"metadata": {},
|
133 |
"outputs": [],
|
134 |
"source": [
|
|
|
144 |
},
|
145 |
{
|
146 |
"cell_type": "code",
|
147 |
+
"execution_count": 67,
|
148 |
"metadata": {},
|
149 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
"source": [
|
151 |
"from langchain.memory import ConversationBufferMemory\n",
|
152 |
"from langchain.chains import ConversationalRetrievalChain\n",
|
|
|
157 |
},
|
158 |
{
|
159 |
"cell_type": "code",
|
160 |
+
"execution_count": 68,
|
161 |
"metadata": {},
|
162 |
"outputs": [
|
163 |
{
|
|
|
166 |
"' \\nCrassus was a Roman general who was killed in battle by the Parthians. He was killed while trying to extend the Roman Empire into the Middle East. Ceasar was avenging Crassus by trying to defeat the Parthians and expand the Roman Empire. He was also trying to avenge the death of his friend and mentor.'"
|
167 |
]
|
168 |
},
|
169 |
+
"execution_count": 68,
|
170 |
"metadata": {},
|
171 |
"output_type": "execute_result"
|
172 |
}
|
|
|
197 |
},
|
198 |
{
|
199 |
"cell_type": "code",
|
200 |
+
"execution_count": 109,
|
201 |
"metadata": {},
|
202 |
"outputs": [],
|
203 |
"source": [
|
|
|
209 |
"from langchain.memory import ConversationBufferMemory\n",
|
210 |
"import pinecone\n",
|
211 |
"\n",
|
212 |
+
"def create_conversation(query: str, chat_history: list, indexToUse: str) -> tuple: \n",
|
213 |
" try:\n",
|
214 |
+
" #vector_store = create_vector_store(\"Roman History\")\n",
|
215 |
+
" print(indexToUse)\n",
|
216 |
+
" vector_store = create_vector_store(\"Literature\")\n",
|
217 |
" memory = ConversationBufferMemory(\n",
|
218 |
" memory_key='chat_history',\n",
|
219 |
" return_messages=False\n",
|
|
|
227 |
" )\n",
|
228 |
" result = cqa({'question': query, 'chat_history': chat_history})\n",
|
229 |
" chat_history.append((query, result['answer']))\n",
|
230 |
+
" return '', chat_history, indexToUse\n",
|
231 |
" except Exception as e:\n",
|
232 |
" chat_history.append((query, e))\n",
|
233 |
" return '', chat_history"
|
|
|
235 |
},
|
236 |
{
|
237 |
"cell_type": "code",
|
238 |
+
"execution_count": 112,
|
239 |
+
"metadata": {},
|
240 |
+
"outputs": [],
|
241 |
+
"source": [
|
242 |
+
"\n",
|
243 |
+
"\n",
|
244 |
+
"# vector_store_index_to_use = None \n",
|
245 |
+
"# def set_vector_store_index(indexName:str):\n",
|
246 |
+
"# vector_store_index_to_use = indexName\n",
|
247 |
+
" \n",
|
248 |
+
"# with gr.Blocks() as demo:\n",
|
249 |
+
"# indexToUseDD = gr.Dropdown(choices=[\"Roman History\", \"Literature\"])"
|
250 |
+
]
|
251 |
+
},
|
252 |
+
{
|
253 |
+
"cell_type": "code",
|
254 |
+
"execution_count": 111,
|
255 |
"metadata": {},
|
256 |
"outputs": [
|
257 |
{
|
258 |
"name": "stdout",
|
259 |
"output_type": "stream",
|
260 |
"text": [
|
261 |
+
"Running on local URL: http://127.0.0.1:7917\n",
|
262 |
"\n",
|
263 |
"To create a public link, set `share=True` in `launch()`.\n"
|
264 |
]
|
|
|
266 |
{
|
267 |
"data": {
|
268 |
"text/html": [
|
269 |
+
"<div><iframe src=\"http://127.0.0.1:7917/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
270 |
],
|
271 |
"text/plain": [
|
272 |
"<IPython.core.display.HTML object>"
|
|
|
281 |
"\n",
|
282 |
"with gr.Blocks() as demo:\n",
|
283 |
" gr.Markdown(\"Chat to docs\")\n",
|
284 |
+
" indexToUseDD = gr.Dropdown(choices=[\"Roman History\", \"Literature\"])\n",
|
285 |
" chatbot = gr.Chatbot(label='Talk to the Doument')\n",
|
286 |
" msg = gr.Textbox()\n",
|
287 |
" \n",
|
288 |
" submitBtn = gr.Button(value=\"Submit\") \n",
|
289 |
" clear = gr.ClearButton([msg, chatbot])\n",
|
290 |
"\n",
|
291 |
+
" msg.submit(create_conversation, [msg, chatbot, indexToUseDD], [msg, chatbot, indexToUseDD])\n",
|
292 |
+
" submitBtn.click(create_conversation, [msg, chatbot, indexToUseDD], [msg, chatbot, indexToUseDD])\n",
|
293 |
"\n",
|
294 |
"if __name__ == '__main__':\n",
|
295 |
" demo.launch(show_error=True) #show_error=True, debug=True)"
|