Trying to get UI working with vector stores in a
Browse files
app.ipynb
CHANGED
@@ -192,6 +192,135 @@
|
|
192 |
"chain.run({'question': query})"
|
193 |
]
|
194 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
{
|
196 |
"cell_type": "code",
|
197 |
"execution_count": 3,
|
@@ -241,6 +370,11 @@
|
|
241 |
"\n",
|
242 |
"iface.launch()"
|
243 |
]
|
|
|
|
|
|
|
|
|
|
|
244 |
}
|
245 |
],
|
246 |
"metadata": {
|
|
|
192 |
"chain.run({'question': query})"
|
193 |
]
|
194 |
},
|
195 |
+
{
|
196 |
+
"cell_type": "markdown",
|
197 |
+
"metadata": {},
|
198 |
+
"source": [
|
199 |
+
"# UI\n",
|
200 |
+
"https://medium.com/@gabriel_renno/how-to-build-a-gpt3-5-powered-chatbot-for-your-landing-page-with-langchain-and-gradio-1236ddfb0cf1"
|
201 |
+
]
|
202 |
+
},
|
203 |
+
{
|
204 |
+
"cell_type": "code",
|
205 |
+
"execution_count": 40,
|
206 |
+
"metadata": {},
|
207 |
+
"outputs": [
|
208 |
+
{
|
209 |
+
"name": "stdout",
|
210 |
+
"output_type": "stream",
|
211 |
+
"text": [
|
212 |
+
"Running on local URL: http://127.0.0.1:7862\n",
|
213 |
+
"\n",
|
214 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
215 |
+
]
|
216 |
+
},
|
217 |
+
{
|
218 |
+
"data": {
|
219 |
+
"text/html": [
|
220 |
+
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
221 |
+
],
|
222 |
+
"text/plain": [
|
223 |
+
"<IPython.core.display.HTML object>"
|
224 |
+
]
|
225 |
+
},
|
226 |
+
"metadata": {},
|
227 |
+
"output_type": "display_data"
|
228 |
+
},
|
229 |
+
{
|
230 |
+
"data": {
|
231 |
+
"text/plain": []
|
232 |
+
},
|
233 |
+
"execution_count": 40,
|
234 |
+
"metadata": {},
|
235 |
+
"output_type": "execute_result"
|
236 |
+
},
|
237 |
+
{
|
238 |
+
"name": "stderr",
|
239 |
+
"output_type": "stream",
|
240 |
+
"text": [
|
241 |
+
"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",
|
242 |
+
" warnings.warn(\n",
|
243 |
+
"Traceback (most recent call last):\n",
|
244 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\queueing.py\", line 406, in call_prediction\n",
|
245 |
+
" output = await route_utils.call_process_api(\n",
|
246 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\route_utils.py\", line 226, in call_process_api\n",
|
247 |
+
" output = await app.get_blocks().process_api(\n",
|
248 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\blocks.py\", line 1554, in process_api\n",
|
249 |
+
" result = await self.call_function(\n",
|
250 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\blocks.py\", line 1206, in call_function\n",
|
251 |
+
" prediction = await utils.async_iteration(iterator)\n",
|
252 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\utils.py\", line 517, in async_iteration\n",
|
253 |
+
" return await iterator.__anext__()\n",
|
254 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\utils.py\", line 621, in asyncgen_wrapper\n",
|
255 |
+
" async for response in f(*args, **kwargs):\n",
|
256 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\chat_interface.py\", line 424, in _stream_fn\n",
|
257 |
+
" first_response = await async_iteration(generator)\n",
|
258 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\utils.py\", line 517, in async_iteration\n",
|
259 |
+
" return await iterator.__anext__()\n",
|
260 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\utils.py\", line 510, in __anext__\n",
|
261 |
+
" return await anyio.to_thread.run_sync(\n",
|
262 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\anyio\\to_thread.py\", line 33, in run_sync\n",
|
263 |
+
" return await get_asynclib().run_sync_in_worker_thread(\n",
|
264 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 877, in run_sync_in_worker_thread\n",
|
265 |
+
" return await future\n",
|
266 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 807, in run\n",
|
267 |
+
" result = context.run(func, *args)\n",
|
268 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\gradio\\utils.py\", line 493, in run_sync_iterator_async\n",
|
269 |
+
" return next(iterator)\n",
|
270 |
+
" File \"C:\\Users\\Garth Raulstone\\AppData\\Local\\Temp\\ipykernel_23576\\3750532605.py\", line 13, in predict\n",
|
271 |
+
" response = openai.ChatCompletion.create(\n",
|
272 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\openai\\api_resources\\chat_completion.py\", line 25, in create\n",
|
273 |
+
" return super().create(*args, **kwargs)\n",
|
274 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\openai\\api_resources\\abstract\\engine_api_resource.py\", line 155, in create\n",
|
275 |
+
" response, _, api_key = requestor.request(\n",
|
276 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\openai\\api_requestor.py\", line 289, in request\n",
|
277 |
+
" result = self.request_raw(\n",
|
278 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\openai\\api_requestor.py\", line 591, in request_raw\n",
|
279 |
+
" abs_url, headers, data = self._prepare_request_raw(\n",
|
280 |
+
" File \"c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\openai\\api_requestor.py\", line 563, in _prepare_request_raw\n",
|
281 |
+
" data = json.dumps(params).encode()\n",
|
282 |
+
" File \"C:\\Users\\Garth Raulstone\\AppData\\Local\\Programs\\Python\\Python310\\lib\\json\\__init__.py\", line 231, in dumps\n",
|
283 |
+
" return _default_encoder.encode(obj)\n",
|
284 |
+
" File \"C:\\Users\\Garth Raulstone\\AppData\\Local\\Programs\\Python\\Python310\\lib\\json\\encoder.py\", line 199, in encode\n",
|
285 |
+
" chunks = self.iterencode(o, _one_shot=True)\n",
|
286 |
+
" File \"C:\\Users\\Garth Raulstone\\AppData\\Local\\Programs\\Python\\Python310\\lib\\json\\encoder.py\", line 257, in iterencode\n",
|
287 |
+
" return _iterencode(o, 0)\n",
|
288 |
+
" File \"C:\\Users\\Garth Raulstone\\AppData\\Local\\Programs\\Python\\Python310\\lib\\json\\encoder.py\", line 179, in default\n",
|
289 |
+
" raise TypeError(f'Object of type {o.__class__.__name__} '\n",
|
290 |
+
"TypeError: Object of type Pinecone is not JSON serializable\n"
|
291 |
+
]
|
292 |
+
}
|
293 |
+
],
|
294 |
+
"source": [
|
295 |
+
"import openai\n",
|
296 |
+
"import gradio as gr\n",
|
297 |
+
"\n",
|
298 |
+
"openai.api_key = os.environ['OPENAI_API_KEY']\n",
|
299 |
+
"\n",
|
300 |
+
"def predict(message, history):\n",
|
301 |
+
" history_openai_format = []\n",
|
302 |
+
" for human, assistant in history:\n",
|
303 |
+
" history_openai_format.append({\"role\": \"user\", \"content\": human })\n",
|
304 |
+
" history_openai_format.append({\"role\": \"assistant\", \"content\":assistant})\n",
|
305 |
+
" history_openai_format.append({\"role\": \"user\", \"content\": message})\n",
|
306 |
+
"\n",
|
307 |
+
" response = openai.ChatCompletion.create(\n",
|
308 |
+
" model='gpt-3.5-turbo',\n",
|
309 |
+
" messages= history_openai_format,\n",
|
310 |
+
" temperature=1.0,\n",
|
311 |
+
" stream=True,\n",
|
312 |
+
" vector_store=vector_store\n",
|
313 |
+
" )\n",
|
314 |
+
"\n",
|
315 |
+
" partial_message = \"\"\n",
|
316 |
+
" for chunk in response:\n",
|
317 |
+
" if len(chunk['choices'][0]['delta']) != 0:\n",
|
318 |
+
" partial_message = partial_message + chunk['choices'][0]['delta']['content']\n",
|
319 |
+
" yield partial_message\n",
|
320 |
+
"\n",
|
321 |
+
"gr.ChatInterface(predict).queue().launch()"
|
322 |
+
]
|
323 |
+
},
|
324 |
{
|
325 |
"cell_type": "code",
|
326 |
"execution_count": 3,
|
|
|
370 |
"\n",
|
371 |
"iface.launch()"
|
372 |
]
|
373 |
+
},
|
374 |
+
{
|
375 |
+
"cell_type": "markdown",
|
376 |
+
"metadata": {},
|
377 |
+
"source": []
|
378 |
}
|
379 |
],
|
380 |
"metadata": {
|