File size: 31,030 Bytes
a93809e |
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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"id": "c031ebee-371a-4ffd-bb4b-8ee273a5dfc2",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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\n"
]
}
],
"source": [
"import os\n",
"import openai\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"\n",
"print(os.getenv(\"OPENAI_API_KEY\"))\n",
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"\n",
"from langchain import PromptTemplate\n",
"from langchain.chains import RetrievalQA\n",
"\n",
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.text_splitter import CharacterTextSplitter\n",
"from langchain.text_splitter import MarkdownTextSplitter\n",
"from langchain.vectorstores import Chroma\n",
"from langchain.document_loaders import TextLoader\n",
"\n",
"from langchain.agents import Tool\n",
"from langchain.memory import ConversationBufferMemory\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.utilities import SerpAPIWrapper\n",
"from langchain.agents import initialize_agent\n",
"from langchain.agents import AgentType\n",
"from langchain.vectorstores import FAISS\n",
"\n",
"from langchain.chains.router import MultiRetrievalQAChain\n",
"from langchain.llms import OpenAI\n",
"\n",
"REST_PERSIST_DIRECTORY = \"chromadb_bul_details\"\n",
"FOOD_GUIDE_PERSIST_DIRECTORY = \"chromadb_food_guide\""
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "e5f08445-3da2-4f1d-8b6f-624200cc77c2",
"metadata": {},
"outputs": [],
"source": [
"embeddings = OpenAIEmbeddings()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0db17fe4-0fe8-47f3-9626-8f31039859cd",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"No embedding_function provided, using default embedding function: DefaultEmbeddingFunction https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2\n"
]
}
],
"source": [
"bulevar_restaurant_texts = [\n",
" \"Bulevar is open Sunday through Wednesday from 5-9pm, and Thursday through Saturday from 4-10pm. It is open for lunch on Friday from 11-3pm\",\n",
" \"Bulevar is located in the Arboretum at 360 and Mopac, next to Eddie V's\",\n",
" \"Bulevar is an excellent Mexican Cuisine restaurant with a laid back style to fine-dining.\",\n",
" \"Bulevar is another restaurant created by Guy and Larry. With the success of their ATX Cocina, Bulevar has created another unique dining experience with high quality dishes.\"\n",
"]\n",
"bulevar_details_retriever = Chroma.from_texts(bulevar_restaurant_texts, persist_directory=REST_PERSIST_DIRECTORY, embedding_function=embeddings)\n",
"bulevar_details_retriever.persist()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "ebf95554-2bf8-4665-a93a-9e033af54a64",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"loader = TextLoader('raw_text/food_guide.md')\n",
"documents = loader.load()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "55add9c5-cef1-4106-a9f8-560228d2c5ea",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"text_splitter = MarkdownTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"docs = text_splitter.split_documents(documents)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "fffe2d0b-d1c9-453f-99b1-bad54b6c5614",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"Document(page_content='## BONE-IN RIBEYE\\n\\n**<span style=\"text-decoration:underline;\">Chef Description:</span>**\\n\\nThe ribeye is carved from the primal section called the beef rib. It falls between the chuck (shoulder) and the loin, and spans from ribs six through twelve. This section of the animal naturally collects more intramuscular fat, creating the beautiful white lines of fat. Our ribeye is coming from Linz Meats out of Chicago, this cut is wet aged and hand cut, butter basted and rested. Served over a mixed animal roasted jus and with cebollitas, blistered shishitos and chipotle garlic butter. Cook time is roughly 45 minutes.\\n\\n**<span style=\"text-decoration:underline;\">Table Talk:</span>**\\n\\nOur prime wet aged ribeye is cooked over post oak wood and butter basted, served with cebollitas, shishitos & chipotle garlic butter\\n\\n**<span style=\"text-decoration:underline;\">Allergies:</span>**\\n\\nDairy,Garlic, Onion, Chilies', metadata={'source': 'raw_text/food_guide.md'})"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"docs[23]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "c7d314fa-d1ac-462c-ac29-1a2a2b8a53af",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"No embedding_function provided, using default embedding function: DefaultEmbeddingFunction https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2\n"
]
}
],
"source": [
"docs_retriever = Chroma.from_documents(docs, persist_directory=FOOD_GUIDE_PERSIST_DIRECTORY, embedding_function=embeddings)\n",
"docs_retriever.persist()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "a9e35123-7cfb-4890-bbe5-e1648aaf1cca",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# docs_retriever = None\n",
"# bulevar_details_retriever = None"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "b632b749-9e76-461a-87a8-036159d075cc",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# docs_retriever = Chroma(persist_directory=FOOD_GUIDE_PERSIST_DIRECTORY, embedding_function=embeddings)\n",
"# bulevar_details_retriever = Chroma(persist_directory=REST_PERSIST_DIRECTORY, embedding_function=OpenAIEmbeddings())"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "a22fe6e4-434f-47aa-92bf-02d088db2d1f",
"metadata": {
"tags": []
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "module 'chromadb.errors' has no attribute 'NotEnoughElementsException'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mInvalidDimensionException\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/langchain/vectorstores/chroma.py:121\u001b[0m, in \u001b[0;36mChroma.__query_collection\u001b[0;34m(self, query_texts, query_embeddings, n_results, where, **kwargs)\u001b[0m\n\u001b[1;32m 120\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 121\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_collection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mquery\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 122\u001b[0m \u001b[43m \u001b[49m\u001b[43mquery_texts\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mquery_texts\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 123\u001b[0m \u001b[43m \u001b[49m\u001b[43mquery_embeddings\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mquery_embeddings\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 124\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_results\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mi\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 125\u001b[0m \u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 126\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 127\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 128\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m chromadb\u001b[38;5;241m.\u001b[39merrors\u001b[38;5;241m.\u001b[39mNotEnoughElementsException:\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/chromadb/api/models/Collection.py:230\u001b[0m, in \u001b[0;36mCollection.query\u001b[0;34m(self, query_embeddings, query_texts, n_results, where, where_document, include)\u001b[0m\n\u001b[1;32m 228\u001b[0m where_document \u001b[38;5;241m=\u001b[39m {}\n\u001b[0;32m--> 230\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_query\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 231\u001b[0m \u001b[43m \u001b[49m\u001b[43mcollection_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mid\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 232\u001b[0m \u001b[43m \u001b[49m\u001b[43mquery_embeddings\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mquery_embeddings\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 233\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_results\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_results\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 234\u001b[0m \u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 235\u001b[0m \u001b[43m \u001b[49m\u001b[43mwhere_document\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere_document\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 236\u001b[0m \u001b[43m \u001b[49m\u001b[43minclude\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minclude\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 237\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/chromadb/api/local.py:439\u001b[0m, in \u001b[0;36mLocalAPI._query\u001b[0;34m(self, collection_id, query_embeddings, n_results, where, where_document, include)\u001b[0m\n\u001b[1;32m 430\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_query\u001b[39m(\n\u001b[1;32m 431\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 432\u001b[0m collection_id: UUID,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 437\u001b[0m include: Include \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdocuments\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmetadatas\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdistances\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[1;32m 438\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m QueryResult:\n\u001b[0;32m--> 439\u001b[0m uuids, distances \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_db\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_nearest_neighbors\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 440\u001b[0m \u001b[43m \u001b[49m\u001b[43mcollection_uuid\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcollection_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 441\u001b[0m \u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 442\u001b[0m \u001b[43m \u001b[49m\u001b[43mwhere_document\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere_document\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 443\u001b[0m \u001b[43m \u001b[49m\u001b[43membeddings\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mquery_embeddings\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 444\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_results\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_results\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 445\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 447\u001b[0m include_embeddings \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124membeddings\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m include\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/chromadb/db/clickhouse.py:591\u001b[0m, in \u001b[0;36mClickhouse.get_nearest_neighbors\u001b[0;34m(self, collection_uuid, where, where_document, embeddings, n_results)\u001b[0m\n\u001b[1;32m 590\u001b[0m index \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_index(collection_uuid)\n\u001b[0;32m--> 591\u001b[0m uuids, distances \u001b[38;5;241m=\u001b[39m \u001b[43mindex\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_nearest_neighbors\u001b[49m\u001b[43m(\u001b[49m\u001b[43membeddings\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn_results\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mids\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 593\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m uuids, distances\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/chromadb/db/index/hnswlib.py:272\u001b[0m, in \u001b[0;36mHnswlib.get_nearest_neighbors\u001b[0;34m(self, query, k, ids)\u001b[0m\n\u001b[1;32m 271\u001b[0m \u001b[38;5;66;03m# Check dimensionality\u001b[39;00m\n\u001b[0;32m--> 272\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_check_dimensionality\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 274\u001b[0m \u001b[38;5;66;03m# Check Number of requested results\u001b[39;00m\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/chromadb/db/index/hnswlib.py:130\u001b[0m, in \u001b[0;36mHnswlib._check_dimensionality\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dim \u001b[38;5;241m!=\u001b[39m idx_dim:\n\u001b[0;32m--> 130\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m InvalidDimensionException(\n\u001b[1;32m 131\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDimensionality of (\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdim\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m) does not match index dimensionality (\u001b[39m\u001b[38;5;132;01m{\u001b[39;00midx_dim\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 132\u001b[0m )\n",
"\u001b[0;31mInvalidDimensionException\u001b[0m: Dimensionality of (1536) does not match index dimensionality (384)",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[29], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m query \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWhat is the zucchini?\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 2\u001b[0m docs \u001b[38;5;241m=\u001b[39m \u001b[43mdocs_retriever\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msimilarity_search\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(docs)\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/langchain/vectorstores/chroma.py:182\u001b[0m, in \u001b[0;36mChroma.similarity_search\u001b[0;34m(self, query, k, filter, **kwargs)\u001b[0m\n\u001b[1;32m 165\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msimilarity_search\u001b[39m(\n\u001b[1;32m 166\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 167\u001b[0m query: \u001b[38;5;28mstr\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 170\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Any,\n\u001b[1;32m 171\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m List[Document]:\n\u001b[1;32m 172\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Run similarity search with Chroma.\u001b[39;00m\n\u001b[1;32m 173\u001b[0m \n\u001b[1;32m 174\u001b[0m \u001b[38;5;124;03m Args:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 180\u001b[0m \u001b[38;5;124;03m List[Document]: List of documents most similar to the query text.\u001b[39;00m\n\u001b[1;32m 181\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 182\u001b[0m docs_and_scores \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msimilarity_search_with_score\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mfilter\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mfilter\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 183\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m [doc \u001b[38;5;28;01mfor\u001b[39;00m doc, _ \u001b[38;5;129;01min\u001b[39;00m docs_and_scores]\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/langchain/vectorstores/chroma.py:229\u001b[0m, in \u001b[0;36mChroma.similarity_search_with_score\u001b[0;34m(self, query, k, filter, **kwargs)\u001b[0m\n\u001b[1;32m 227\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 228\u001b[0m query_embedding \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_embedding_function\u001b[38;5;241m.\u001b[39membed_query(query)\n\u001b[0;32m--> 229\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__query_collection\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 230\u001b[0m \u001b[43m \u001b[49m\u001b[43mquery_embeddings\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mquery_embedding\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn_results\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mfilter\u001b[39;49m\n\u001b[1;32m 231\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 233\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _results_to_docs_and_scores(results)\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/langchain/utils.py:52\u001b[0m, in \u001b[0;36mxor_args.<locals>.decorator.<locals>.wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 46\u001b[0m invalid_group_names \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(arg_groups[i]) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m invalid_groups]\n\u001b[1;32m 47\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 48\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExactly one argument in each of the following\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 49\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m groups must be defined:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 50\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(invalid_group_names)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 51\u001b[0m )\n\u001b[0;32m---> 52\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda/envs/mlops/lib/python3.10/site-packages/langchain/vectorstores/chroma.py:128\u001b[0m, in \u001b[0;36mChroma.__query_collection\u001b[0;34m(self, query_texts, query_embeddings, n_results, where, **kwargs)\u001b[0m\n\u001b[1;32m 120\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 121\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_collection\u001b[38;5;241m.\u001b[39mquery(\n\u001b[1;32m 122\u001b[0m query_texts\u001b[38;5;241m=\u001b[39mquery_texts,\n\u001b[1;32m 123\u001b[0m query_embeddings\u001b[38;5;241m=\u001b[39mquery_embeddings,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 126\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[1;32m 127\u001b[0m )\n\u001b[0;32m--> 128\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[43mchromadb\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43merrors\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mNotEnoughElementsException\u001b[49m:\n\u001b[1;32m 129\u001b[0m logger\u001b[38;5;241m.\u001b[39merror(\n\u001b[1;32m 130\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mChroma collection \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_collection\u001b[38;5;241m.\u001b[39mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 131\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontains fewer than \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mi\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m elements.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 132\u001b[0m )\n\u001b[1;32m 133\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m chromadb\u001b[38;5;241m.\u001b[39merrors\u001b[38;5;241m.\u001b[39mNotEnoughElementsException(\n\u001b[1;32m 134\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo documents found for Chroma collection \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_collection\u001b[38;5;241m.\u001b[39mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 135\u001b[0m )\n",
"\u001b[0;31mAttributeError\u001b[0m: module 'chromadb.errors' has no attribute 'NotEnoughElementsException'"
]
}
],
"source": [
"query = \"What is the zucchini?\"\n",
"docs = docs_retriever.similarity_search(query)\n",
"\n",
"print(docs)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "96cd0d8a-d0c5-4828-81a3-5e908d3d7c9d",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"prompt = \"\"\"Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
"\n",
"Context: {context}\n",
"\n",
"Question: {question}\n",
"Helpful Answer:\"\"\"\n",
"\n",
"prompt_template = PromptTemplate(\n",
" template=prompt, input_variables=[\"context\", \"question\"]\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "e3ee9598-8ea6-45cc-8a27-6b3bb8f22c11",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"retriever_infos = [\n",
" {\n",
" \"name\": \"Food Guide\", \n",
" \"description\": \"Good for answering questions about the menu\", \n",
" \"retriever\": docs_retriever.as_retriever()\n",
" },\n",
" {\n",
" \"name\": \"Bulevar Restaurant Details\", \n",
" \"description\": \"Good for answering questions about Bulevar's hours, and restaurant details such as its mission, history, and owners.\", \n",
" \"retriever\": bulevar_details_retriever.as_retriever()\n",
" }\n",
"]\n",
"\n",
"chain = MultiRetrievalQAChain.from_retrievers(OpenAI(), retriever_infos, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "3d7393ea-3464-4ef3-bfe4-3ca93546791e",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiRetrievalQAChain chain...\u001b[0m\n",
"Bulevar Restaurant Details: {'query': 'What is Bulevar?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" Bulevar is an excellent Mexican Cuisine restaurant with a laid back style to fine-dining created by Guy and Larry.\n"
]
}
],
"source": [
"print(chain.run(\"What is Bulevar?\"))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "774e945a-db65-4635-bb98-3d921fee912e",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiRetrievalQAChain chain...\u001b[0m\n",
"Food Guide: {'query': \"What's special about Niman Ranch steaks?\"}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" Niman Ranch steaks are raised humanely and sustainably, and are dry aged for a minimum of 50 days, which concentrates the beef flavor.\n"
]
}
],
"source": [
"print(chain.run(\"What's special about Niman Ranch steaks?\"))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "4a934553-2904-4fdd-a632-96a9cad34a58",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiRetrievalQAChain chain...\u001b[0m\n",
"Food Guide: {'query': 'Do you recommend dishes from Bulevar Restaurant?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" We cannot recommend dishes from Bulevar Restaurant, as we are not familiar with their menu.\n"
]
}
],
"source": [
"print(chain.run(\"Do you recommend it?\"))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "b1563ba0-d049-40b8-a917-f4b8c535be7e",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiRetrievalQAChain chain...\u001b[0m\n",
"Food Guide: {'query': \"What's on the crab tostada?\"}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" The crab tostada is fried Masienda tortilla and seasoned with POW POW, topped with sliced avocado and a remoulade. It is then topped with jumbo lump crab meat lightly dressed and topped with radishes, freshly chopped chives, cilantro and Tajin powder, and a light drizzle of olive oil.\n"
]
}
],
"source": [
"print(chain.run(\"What's on the crab tostada?\"))"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "d7c1e7c2-e534-4bbe-a6ce-53716c337864",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiRetrievalQAChain chain...\u001b[0m\n",
"Bulevar Restaurant Details: {'query': 'When does the restaurant open?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" Bulevar is open Sunday through Wednesday from 5-9pm, and Thursday through Saturday from 4-10pm. It is open for lunch on Friday from 11-3pm.\n"
]
}
],
"source": [
"print(chain.run(\"When does the restaurant open?\"))"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "10a9a109-3c63-4824-bb5f-c5aafe92e7dd",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiRetrievalQAChain chain...\u001b[0m\n",
"Food Guide: {'query': 'What allergies are in the Churros?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" Nuts(flour), Dairy, Cinnamon\n"
]
}
],
"source": [
"print(chain.run(\"What allergies are in the Churros?\"))"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "5b7d843b-a95f-4794-ae90-b83073b30fae",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiRetrievalQAChain chain...\u001b[0m\n",
"Food Guide: {'query': 'What are the Churros?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" The Churros are traditional mini churros deep fried and tossed with a cinnamon/sugar blend, served with house made dulce de leche and chocolate ganache dipping sauces, and topped with powdered sugar. They contain nuts (flour), dairy, and cinnamon.\n"
]
}
],
"source": [
"print(chain.run(\"What are the Churros?\"))"
]
},
{
"cell_type": "code",
"execution_count": 101,
"id": "49ff4757-463f-4616-ad1e-0693a16e72bd",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new MultiRetrievalQAChain chain...\u001b[0m\n",
"Food Guide: {'query': 'What allergies are in the tarta de limon?'}\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
" Dairy, Egg, Honey, Citrus\n"
]
}
],
"source": [
"print(chain.run(\"What allergies are on the tarta de limon?\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "59d54fdb-11da-437b-b513-a93d056ee713",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10 (mlops)",
"language": "python",
"name": "mlops"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|