Spaces:
Sleeping
Sleeping
File size: 53,381 Bytes
464b72a | 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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 | {
"cells": [
{
"cell_type": "markdown",
"id": "fdfc1b2a",
"metadata": {},
"source": [
"## 1. Install Required Packages"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "e0f621d9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"π¦ Installing required packages...\n",
"β
All packages installed!\n"
]
}
],
"source": [
"import sys\n",
"import subprocess\n",
"\n",
"# Install packages (works in VS Code Jupyter)\n",
"packages = [\n",
" 'langchain-community',\n",
" 'sentence-transformers',\n",
" 'transformers',\n",
" 'faiss-cpu',\n",
" 'pypdf',\n",
" 'google-generativeai',\n",
" 'langchain-huggingface',\n",
" 'langchain-text-splitters',\n",
" 'fastapi',\n",
" 'uvicorn',\n",
" 'nest-asyncio'\n",
"]\n",
"\n",
"print(\"π¦ Installing required packages...\")\n",
"subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-q'] + packages)\n",
"print(\"β
All packages installed!\")"
]
},
{
"cell_type": "markdown",
"id": "6c5a12c2",
"metadata": {},
"source": [
"## 2. Setup Local Directories (Windows)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "fbe27891",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"β
Local directories created!\n",
"π RAG data will be stored at: /content/rag_data\n"
]
}
],
"source": [
"import os\n",
"\n",
"# Use local directories instead of Google Drive\n",
"RAG_DIR = os.path.join(os.getcwd(), 'rag_data')\n",
"FAISS_PATH = os.path.join(RAG_DIR, 'faiss_index')\n",
"PDFS_PATH = os.path.join(RAG_DIR, 'pdfs')\n",
"\n",
"os.makedirs(FAISS_PATH, exist_ok=True)\n",
"os.makedirs(PDFS_PATH, exist_ok=True)\n",
"\n",
"print(f\"β
Local directories created!\")\n",
"print(f\"π RAG data will be stored at: {RAG_DIR}\")"
]
},
{
"cell_type": "markdown",
"id": "b75dabae",
"metadata": {},
"source": [
"## 3. Configure Gemini API Key"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "330b1f65",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"β
Gemini API configured!\n"
]
}
],
"source": [
"import google.generativeai as genai\n",
"\n",
"# Replace with your API key\n",
"GOOGLE_API_KEY = \"AIzaSyC7tkb3uFgmh8YSuOVHYgIDywyL2lzICBA\" # Get from https://makersuite.google.com/app/apikey\n",
"\n",
"genai.configure(api_key=GOOGLE_API_KEY)\n",
"print(\"β
Gemini API configured!\")"
]
},
{
"cell_type": "markdown",
"id": "49f2b49c",
"metadata": {},
"source": [
"## 4. RAG Functions - Load, Process, Query"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "c296fc8b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"β
RAG functions defined!\n"
]
}
],
"source": [
"import unicodedata\n",
"import re\n",
"from typing import List, Dict\n",
"from langchain_community.document_loaders.pdf import PyPDFLoader\n",
"from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
"from langchain_huggingface import HuggingFaceEmbeddings\n",
"from langchain_community.vectorstores import FAISS\n",
"\n",
"# Global variables\n",
"vectordb = None\n",
"retriever = None\n",
"embeddings = None\n",
"rag_initialized = False\n",
"uploaded_documents = []\n",
"\n",
"\n",
"def initialize_embeddings():\n",
" \"\"\"Initialize multilingual embedding model\"\"\"\n",
" global embeddings\n",
" \n",
" if embeddings is not None:\n",
" return embeddings\n",
" \n",
" print(\"Loading multilingual embedding model...\")\n",
" embeddings = HuggingFaceEmbeddings(\n",
" model_name=\"sentence-transformers/paraphrase-multilingual-mpnet-base-v2\"\n",
" )\n",
" print(\"β
Embedding model loaded!\")\n",
" return embeddings\n",
"\n",
"\n",
"def clean_text(text: str) -> str:\n",
" \"\"\"Clean and normalize text\"\"\"\n",
" if not isinstance(text, str) or not text.strip():\n",
" return \"\"\n",
" \n",
" normalized_text = unicodedata.normalize('NFKC', text)\n",
" cleaned_chars = [\n",
" char for char in normalized_text\n",
" if unicodedata.category(char) not in ['So', 'Cn', 'Cc', 'Cf', 'Cs']\n",
" ]\n",
" cleaned_text = \"\".join(cleaned_chars)\n",
" cleaned_text = re.sub(r'\\s+', ' ', cleaned_text).strip()\n",
" return cleaned_text\n",
"\n",
"\n",
"def load_and_process_pdf(pdf_path: str) -> List:\n",
" \"\"\"Load PDF and split into chunks\"\"\"\n",
" print(f\"Loading PDF: {pdf_path}\")\n",
" \n",
" loader = PyPDFLoader(pdf_path)\n",
" docs = loader.load()\n",
" \n",
" splitter = RecursiveCharacterTextSplitter(\n",
" chunk_size=300,\n",
" chunk_overlap=80\n",
" )\n",
" chunks = splitter.split_documents(docs)\n",
" \n",
" print(f\"β
Loaded {len(docs)} pages, created {len(chunks)} chunks\")\n",
" return chunks\n",
"\n",
"\n",
"def create_vector_store(chunks: List) -> bool:\n",
" \"\"\"Create or update FAISS vector store\"\"\"\n",
" global vectordb, retriever, rag_initialized\n",
" \n",
" initialize_embeddings()\n",
" \n",
" texts = [doc.page_content for doc in chunks]\n",
" metadatas = [doc.metadata for doc in chunks]\n",
" \n",
" processed_texts = []\n",
" processed_metadatas = []\n",
" \n",
" for i, text in enumerate(texts):\n",
" cleaned_text = clean_text(text)\n",
" if cleaned_text:\n",
" processed_texts.append(cleaned_text)\n",
" processed_metadatas.append(metadatas[i])\n",
" \n",
" if not processed_texts:\n",
" print(\"β No valid texts after cleaning\")\n",
" return False\n",
" \n",
" print(f\"Creating embeddings for {len(processed_texts)} chunks...\")\n",
" \n",
" if vectordb is None:\n",
" vectordb = FAISS.from_texts(processed_texts, embeddings, metadatas=processed_metadatas)\n",
" else:\n",
" new_vectordb = FAISS.from_texts(processed_texts, embeddings, metadatas=processed_metadatas)\n",
" vectordb.merge_from(new_vectordb)\n",
" \n",
" retriever = vectordb.as_retriever(search_kwargs={\"k\": 4})\n",
" rag_initialized = True\n",
" \n",
" # Save to Google Drive\n",
" save_vector_store()\n",
" \n",
" print(\"β
Vector store created/updated!\")\n",
" return True\n",
"\n",
"\n",
"def save_vector_store():\n",
" \"\"\"Save FAISS index to Google Drive\"\"\"\n",
" if vectordb is None:\n",
" return\n",
" \n",
" vectordb.save_local(FAISS_PATH)\n",
" print(f\"β
Vector store saved to Google Drive: {FAISS_PATH}\")\n",
"\n",
"\n",
"def load_vector_store() -> bool:\n",
" \"\"\"Load FAISS index from Google Drive\"\"\"\n",
" global vectordb, retriever, rag_initialized\n",
" \n",
" if not os.path.exists(FAISS_PATH):\n",
" print(\"βΉ No existing vector store found\")\n",
" return False\n",
" \n",
" try:\n",
" initialize_embeddings()\n",
" vectordb = FAISS.load_local(\n",
" FAISS_PATH, \n",
" embeddings,\n",
" allow_dangerous_deserialization=True\n",
" )\n",
" retriever = vectordb.as_retriever(search_kwargs={\"k\": 4})\n",
" rag_initialized = True\n",
" print(\"β
Loaded existing vector store from Google Drive\")\n",
" return True\n",
" except Exception as e:\n",
" print(f\"β Failed to load vector store: {e}\")\n",
" return False\n",
"\n",
"\n",
"def rag_answer(question: str, relevance_threshold: float = 1.5) -> Dict:\n",
" \"\"\"Answer question using RAG - check database first, fallback to Gemini\"\"\"\n",
" global retriever, vectordb\n",
" \n",
" result = {\n",
" \"answer\": \"\",\n",
" \"source\": \"none\",\n",
" \"context_found\": False,\n",
" \"relevance_score\": 0.0\n",
" }\n",
" \n",
" if not rag_initialized or retriever is None:\n",
" result[\"source\"] = \"gemini\"\n",
" result[\"answer\"] = ask_gemini_directly(question)\n",
" return result\n",
" \n",
" # Search vector database\n",
" docs_with_scores = vectordb.similarity_search_with_score(question, k=4)\n",
" \n",
" if not docs_with_scores:\n",
" result[\"source\"] = \"gemini\"\n",
" result[\"answer\"] = ask_gemini_directly(question)\n",
" return result\n",
" \n",
" best_score = docs_with_scores[0][1]\n",
" result[\"relevance_score\"] = float(best_score)\n",
" \n",
" # Check relevance threshold\n",
" if best_score > relevance_threshold:\n",
" print(f\"β Low relevance (score: {best_score:.3f}), using Gemini\")\n",
" result[\"source\"] = \"gemini\"\n",
" result[\"answer\"] = ask_gemini_directly(question)\n",
" return result\n",
" \n",
" # Good relevance - use RAG\n",
" print(f\"β
Good relevance (score: {best_score:.3f}), answering from documents\")\n",
" docs = [doc for doc, score in docs_with_scores]\n",
" context = \"\\n\\n\".join([d.page_content for d in docs])\n",
" result[\"context_found\"] = True\n",
" \n",
" prompt = f\"\"\"Answer the question based ONLY on the following context from the PDF documents. If the context doesn't contain enough information, say \"I don't have enough information in the documents to answer this.\"\n",
"\n",
"Context from PDFs:\n",
"{context}\n",
"\n",
"Question: {question}\n",
"\n",
"Answer:\"\"\"\n",
" \n",
" try:\n",
" model = genai.GenerativeModel(\"models/gemini-1.5-flash\")\n",
" response = model.generate_content(prompt)\n",
" result[\"answer\"] = response.text\n",
" result[\"source\"] = \"rag\"\n",
" except Exception as e:\n",
" print(f\"β RAG generation error: {e}\")\n",
" result[\"answer\"] = f\"Error: {str(e)}\"\n",
" result[\"source\"] = \"error\"\n",
" \n",
" return result\n",
"\n",
"\n",
"def ask_gemini_directly(question: str) -> str:\n",
" \"\"\"Fallback: Ask Gemini directly\"\"\"\n",
" try:\n",
" model = genai.GenerativeModel(\"models/gemini-1.5-flash\")\n",
" response = model.generate_content(f\"Answer this question: {question}\")\n",
" return response.text\n",
" except Exception as e:\n",
" return f\"Error: {str(e)}\"\n",
"\n",
"\n",
"print(\"β
RAG functions defined!\")"
]
},
{
"cell_type": "markdown",
"id": "2b98c801",
"metadata": {},
"source": [
"## 5. Load PDFs from Local Directory"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "6aecdbe9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading multilingual embedding model...\n",
"β
Embedding model loaded!\n",
"β Failed to load vector store: Error in faiss::FileIOReader::FileIOReader(const char*) at /project/third-party/faiss/faiss/impl/io.cpp:69: Error: 'f' failed: could not open /content/rag_data/faiss_index/index.faiss for reading: No such file or directory\n",
"π Place your PDF files in: /content/rag_data/pdfs\n",
" Current directory: /content\n",
"\n",
"β οΈ No PDF files found!\n",
" Please add PDF files to: /content/rag_data/pdfs\n"
]
}
],
"source": [
"import glob\n",
"\n",
"# Try to load existing vector store first\n",
"load_vector_store()\n",
"\n",
"# Option 1: Manually place PDFs in the rag_data/pdfs folder, then run this\n",
"print(f\"π Place your PDF files in: {PDFS_PATH}\")\n",
"print(f\" Current directory: {os.getcwd()}\")\n",
"\n",
"# Find all PDFs in the pdfs folder\n",
"pdf_files = glob.glob(os.path.join(PDFS_PATH, \"*.pdf\"))\n",
"\n",
"if not pdf_files:\n",
" print(\"\\nβ οΈ No PDF files found!\")\n",
" print(f\" Please add PDF files to: {PDFS_PATH}\")\n",
"else:\n",
" print(f\"\\nπ Found {len(pdf_files)} PDF file(s):\")\n",
" \n",
" # Process each PDF\n",
" for pdf_path in pdf_files:\n",
" filename = os.path.basename(pdf_path)\n",
" print(f\"\\n Processing: {filename}\")\n",
" \n",
" # Skip if already processed\n",
" if filename in uploaded_documents:\n",
" print(f\" βοΈ Already processed, skipping...\")\n",
" continue\n",
" \n",
" # Process PDF\n",
" chunks = load_and_process_pdf(pdf_path)\n",
" create_vector_store(chunks)\n",
" uploaded_documents.append(filename)\n",
" \n",
" print(f\"\\nβ
Processed {len(uploaded_documents)} PDF(s) total\")\n",
" print(f\"π Documents in database: {uploaded_documents}\")"
]
},
{
"cell_type": "markdown",
"id": "ff67dfb7",
"metadata": {},
"source": [
"## 6. Test RAG Query (Simple)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "86dc46cd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"β Question: What is a wired network?\n",
"\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipython-input-1251978023.py\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"β Question: {test_question}\\n\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrag_answer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_question\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrelevance_threshold\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2.0\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Increased threshold\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"π Source: {result['source'].upper()}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/tmp/ipython-input-2893062687.py\u001b[0m in \u001b[0;36mrag_answer\u001b[0;34m(question, relevance_threshold)\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mrag_initialized\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mretriever\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"source\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"gemini\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 150\u001b[0;31m \u001b[0mresult\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"answer\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mask_gemini_directly\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mquestion\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 151\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/tmp/ipython-input-2893062687.py\u001b[0m in \u001b[0;36mask_gemini_directly\u001b[0;34m(question)\u001b[0m\n\u001b[1;32m 201\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgenai\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mGenerativeModel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"models/gemini-1.5-flash\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 203\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgenerate_content\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Answer this question: {question}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 204\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 205\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/generativeai/generative_models.py\u001b[0m in \u001b[0;36mgenerate_content\u001b[0;34m(self, contents, generation_config, safety_settings, stream, tools, tool_config, request_options)\u001b[0m\n\u001b[1;32m 329\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mgeneration_types\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mGenerateContentResponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_iterator\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 330\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 331\u001b[0;31m response = self._client.generate_content(\n\u001b[0m\u001b[1;32m 332\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 333\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mrequest_options\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py\u001b[0m in \u001b[0;36mgenerate_content\u001b[0;34m(self, request, model, contents, retry, timeout, metadata)\u001b[0m\n\u001b[1;32m 833\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 834\u001b[0m \u001b[0;31m# Send the request.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 835\u001b[0;31m response = rpc(\n\u001b[0m\u001b[1;32m 836\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 837\u001b[0m \u001b[0mretry\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mretry\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/api_core/gapic_v1/method.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, timeout, retry, compression, *args, **kwargs)\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"compression\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcompression\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 130\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 131\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mwrapped_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 132\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 133\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/api_core/retry/retry_unary.py\u001b[0m in \u001b[0;36mretry_wrapped_func\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 292\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_initial\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_maximum\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmultiplier\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_multiplier\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 293\u001b[0m )\n\u001b[0;32m--> 294\u001b[0;31m return retry_target(\n\u001b[0m\u001b[1;32m 295\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 296\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_predicate\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/api_core/retry/retry_unary.py\u001b[0m in \u001b[0;36mretry_target\u001b[0;34m(target, predicate, sleep_generator, timeout, on_error, exception_factory, **kwargs)\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 146\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 147\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 148\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0minspect\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misawaitable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 149\u001b[0m \u001b[0mwarnings\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_ASYNC_RETRY_WARNING\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/api_core/timeout.py\u001b[0m in \u001b[0;36mfunc_with_timeout\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"timeout\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mremaining_timeout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 130\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 131\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 132\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc_with_timeout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/api_core/grpc_helpers.py\u001b[0m in \u001b[0;36merror_remapped_callable\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 73\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0merror_remapped_callable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 74\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 75\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mcallable_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 76\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mgrpc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mRpcError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mexceptions\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_grpc_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexc\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/ai/generativelanguage_v1beta/services/generative_service/transports/rest.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, request, retry, timeout, metadata)\u001b[0m\n\u001b[1;32m 1146\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1147\u001b[0m \u001b[0;31m# Send the request\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1148\u001b[0;31m response = GenerativeServiceRestTransport._GenerateContent._get_response(\n\u001b[0m\u001b[1;32m 1149\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_host\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1150\u001b[0m \u001b[0mmetadata\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/ai/generativelanguage_v1beta/services/generative_service/transports/rest.py\u001b[0m in \u001b[0;36m_get_response\u001b[0;34m(host, metadata, query_params, session, timeout, transcoded_request, body)\u001b[0m\n\u001b[1;32m 1046\u001b[0m \u001b[0mheaders\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmetadata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1047\u001b[0m \u001b[0mheaders\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Content-Type\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"application/json\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1048\u001b[0;31m response = getattr(session, method)(\n\u001b[0m\u001b[1;32m 1049\u001b[0m \u001b[0;34m\"{host}{uri}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhost\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mhost\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0muri\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0muri\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1050\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/requests/sessions.py\u001b[0m in \u001b[0;36mpost\u001b[0;34m(self, url, data, json, **kwargs)\u001b[0m\n\u001b[1;32m 635\u001b[0m \"\"\"\n\u001b[1;32m 636\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 637\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"POST\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 638\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 639\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/google/auth/transport/requests.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(self, method, url, data, headers, max_allowed_time, timeout, **kwargs)\u001b[0m\n\u001b[1;32m 533\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mTimeoutGuard\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mremaining_time\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mguard\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 534\u001b[0m \u001b[0m_helpers\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequest_log\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_LOGGER\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 535\u001b[0;31m response = super(AuthorizedSession, self).request(\n\u001b[0m\u001b[1;32m 536\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 537\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/requests/sessions.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[1;32m 587\u001b[0m }\n\u001b[1;32m 588\u001b[0m \u001b[0msend_kwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msettings\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 589\u001b[0;31m \u001b[0mresp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprep\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0msend_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 590\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 591\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/requests/sessions.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, **kwargs)\u001b[0m\n\u001b[1;32m 701\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 702\u001b[0m \u001b[0;31m# Send the request\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 703\u001b[0;31m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0madapter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 704\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 705\u001b[0m \u001b[0;31m# Total elapsed time of the request (approximately)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/requests/adapters.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[1;32m 642\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 643\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 644\u001b[0;31m resp = conn.urlopen(\n\u001b[0m\u001b[1;32m 645\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 646\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/urllib3/connectionpool.py\u001b[0m in \u001b[0;36murlopen\u001b[0;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)\u001b[0m\n\u001b[1;32m 785\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 786\u001b[0m \u001b[0;31m# Make the request on the HTTPConnection object\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 787\u001b[0;31m response = self._make_request(\n\u001b[0m\u001b[1;32m 788\u001b[0m \u001b[0mconn\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 789\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/urllib3/connectionpool.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)\u001b[0m\n\u001b[1;32m 532\u001b[0m \u001b[0;31m# Receive the response from the server\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 533\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 534\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 535\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mBaseSSLError\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mOSError\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 536\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_raise_timeout\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0merr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout_value\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mread_timeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.12/dist-packages/urllib3/connection.py\u001b[0m in \u001b[0;36mgetresponse\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 564\u001b[0m \u001b[0;31m# Get the response from http.client.HTTPConnection\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 565\u001b[0;31m \u001b[0mhttplib_response\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 566\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 567\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.12/http/client.py\u001b[0m in \u001b[0;36mgetresponse\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1428\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1429\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1430\u001b[0;31m \u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbegin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1431\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mConnectionError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1432\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.12/http/client.py\u001b[0m in \u001b[0;36mbegin\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 329\u001b[0m \u001b[0;31m# read until we get a non-100 response\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 330\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 331\u001b[0;31m \u001b[0mversion\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstatus\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreason\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_read_status\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 332\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mstatus\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mCONTINUE\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 333\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.12/http/client.py\u001b[0m in \u001b[0;36m_read_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 290\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 291\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_read_status\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 292\u001b[0;31m \u001b[0mline\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_MAXLINE\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"iso-8859-1\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 293\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0m_MAXLINE\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 294\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mLineTooLong\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"status line\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.12/socket.py\u001b[0m in \u001b[0;36mreadinto\u001b[0;34m(self, b)\u001b[0m\n\u001b[1;32m 718\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 719\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 720\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_sock\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_into\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 721\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 722\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_timeout_occurred\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"# Test with a question\n",
"test_question = \"What is a wired network?\" # Change this to your question\n",
"\n",
"print(f\"β Question: {test_question}\\n\")\n",
"result = rag_answer(test_question, relevance_threshold=2.0) # Increased threshold\n",
"\n",
"print(f\"π Source: {result['source'].upper()}\")\n",
"print(f\"π Relevance Score: {result['relevance_score']:.3f}\")\n",
"print(f\"\\n㪠Answer:\\n{result['answer']}\")"
]
},
{
"cell_type": "markdown",
"id": "04937fbd",
"metadata": {},
"source": [
"## 7. Create FastAPI Server + ngrok (Public API)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "708b25ca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"β
FastAPI app created!\n"
]
}
],
"source": [
"from fastapi import FastAPI, HTTPException\n",
"from pydantic import BaseModel\n",
"import nest_asyncio\n",
"\n",
"# Allow nested event loops (for Jupyter)\n",
"nest_asyncio.apply()\n",
"\n",
"# Create FastAPI app\n",
"app = FastAPI(title=\"RAG API\", version=\"1.0\")\n",
"\n",
"class QuestionRequest(BaseModel):\n",
" question: str\n",
" threshold: float = 2.0 # Default threshold\n",
"\n",
"class AnswerResponse(BaseModel):\n",
" question: str\n",
" answer: str\n",
" source: str\n",
" relevance_score: float\n",
" context_found: bool\n",
"\n",
"@app.get(\"/\")\n",
"async def root():\n",
" return {\n",
" \"message\": \"RAG API is running!\",\n",
" \"endpoints\": {\n",
" \"/ask\": \"POST - Ask a question\",\n",
" \"/status\": \"GET - Check system status\"\n",
" }\n",
" }\n",
"\n",
"@app.post(\"/ask\", response_model=AnswerResponse)\n",
"async def ask_question(request: QuestionRequest):\n",
" \"\"\"Ask a question to RAG system\"\"\"\n",
" if not request.question:\n",
" raise HTTPException(status_code=400, detail=\"Question is required\")\n",
" \n",
" result = rag_answer(request.question, relevance_threshold=request.threshold)\n",
" \n",
" return AnswerResponse(\n",
" question=request.question,\n",
" answer=result[\"answer\"],\n",
" source=result[\"source\"],\n",
" relevance_score=result[\"relevance_score\"],\n",
" context_found=result[\"context_found\"]\n",
" )\n",
"\n",
"@app.get(\"/status\")\n",
"async def get_status():\n",
" \"\"\"Get RAG system status\"\"\"\n",
" return {\n",
" \"initialized\": rag_initialized,\n",
" \"documents_count\": len(uploaded_documents),\n",
" \"documents\": uploaded_documents,\n",
" \"has_vector_store\": vectordb is not None\n",
" }\n",
"\n",
"print(\"β
FastAPI app created!\")"
]
},
{
"cell_type": "markdown",
"id": "bd49f8a1",
"metadata": {},
"source": [
"## 8. Start Server Locally (Access at http://localhost:8000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e4c8558",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"============================================================\n",
"π LOCAL API SERVER STARTED!\n",
"============================================================\n",
"\n",
"π API Endpoints:\n",
" POST http://localhost:8000/ask - Ask a question\n",
" GET http://localhost:8000/status - Check status\n",
" GET http://localhost:8000/docs - API documentation\n",
"\n",
"π‘ Test in browser: http://localhost:8000/docs\n",
"\n",
"π‘ Example curl command:\n",
" curl -X POST \"http://localhost:8000/ask\" ^\n",
" -H \"Content-Type: application/json\" ^\n",
" -d \"{\\\"question\\\": \\\"What is a wired network?\\\", \\\"threshold\\\": 2.0}\"\n",
"\n",
"π Server is running in background...\n",
" (Server will stop when notebook kernel is restarted)\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.12/dist-packages/uvicorn/server.py:67: RuntimeWarning: coroutine 'Server.serve' was never awaited\n",
" return asyncio_run(self.serve(sockets=sockets), loop_factory=self.config.get_loop_factory())\n",
"RuntimeWarning: Enable tracemalloc to get the object allocation traceback\n",
"Exception in thread Thread-6 (run_server):\n",
"Traceback (most recent call last):\n",
" File \"/usr/lib/python3.12/threading.py\", line 1075, in _bootstrap_inner\n",
" self.run()\n",
" File \"/usr/lib/python3.12/threading.py\", line 1012, in run\n",
" self._target(*self._args, **self._kwargs)\n",
" File \"/tmp/ipython-input-2073060122.py\", line 6, in run_server\n",
" File \"/usr/local/lib/python3.12/dist-packages/uvicorn/main.py\", line 593, in run\n",
" server.run()\n",
" File \"/usr/local/lib/python3.12/dist-packages/uvicorn/server.py\", line 67, in run\n",
" return asyncio_run(self.serve(sockets=sockets), loop_factory=self.config.get_loop_factory())\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
"TypeError: _patch_asyncio.<locals>.run() got an unexpected keyword argument 'loop_factory'\n"
]
}
],
"source": [
"import uvicorn\n",
"import threading\n",
"\n",
"def run_server():\n",
" \"\"\"Run the FastAPI server in a thread\"\"\"\n",
" uvicorn.run(app, host=\"127.0.0.1\", port=8000, log_level=\"info\")\n",
"\n",
"# Start server in background thread\n",
"server_thread = threading.Thread(target=run_server, daemon=True)\n",
"server_thread.start()\n",
"\n",
"print(\"\\n\" + \"=\"*60)\n",
"print(\"π LOCAL API SERVER STARTED!\")\n",
"print(\"=\"*60)\n",
"print(\"\\nπ API Endpoints:\")\n",
"print(\" POST http://localhost:8000/ask - Ask a question\")\n",
"print(\" GET http://localhost:8000/status - Check status\")\n",
"print(\" GET http://localhost:8000/docs - API documentation\")\n",
"print(\"\\nπ‘ Test in browser: http://localhost:8000/docs\")\n",
"print(\"\\nπ‘ Example curl command:\")\n",
"print(' curl -X POST \"http://localhost:8000/ask\" ^')\n",
"print(' -H \"Content-Type: application/json\" ^')\n",
"print(' -d \"{\\\\\"question\\\\\": \\\\\"What is a wired network?\\\\\", \\\\\"threshold\\\\\": 2.0}\"')\n",
"print(\"\\nπ Server is running in background...\")\n",
"print(\" (Server will stop when notebook kernel is restarted)\\n\")"
]
},
{
"cell_type": "markdown",
"id": "a025b750",
"metadata": {},
"source": [
"## 9. Test API from Another Cell (While Server is Running)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b368a3ac",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"π‘ Testing API at http://localhost:8000/ask\n",
"\n",
"β Connection error: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /ask (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x79ffcd92bd40>: Failed to establish a new connection: [Errno 111] Connection refused'))\n",
" Make sure the server is running (cell 8)\n"
]
}
],
"source": [
"import requests\n",
"import json\n",
"import time\n",
"\n",
"# Give server a moment to start\n",
"time.sleep(2)\n",
"\n",
"# Local API URL\n",
"API_URL = \"http://localhost:8000\"\n",
"\n",
"# Test question\n",
"test_data = {\n",
" \"question\": \"What is a wireless network?\",\n",
" \"threshold\": 2.0\n",
"}\n",
"\n",
"print(f\"π‘ Testing API at {API_URL}/ask\\n\")\n",
"\n",
"try:\n",
" # Make API request\n",
" response = requests.post(\n",
" f\"{API_URL}/ask\",\n",
" json=test_data,\n",
" headers={\"Content-Type\": \"application/json\"}\n",
" )\n",
" \n",
" if response.status_code == 200:\n",
" result = response.json()\n",
" print(f\"β Question: {result['question']}\")\n",
" print(f\"π Source: {result['source'].upper()}\")\n",
" print(f\"π Score: {result['relevance_score']:.3f}\")\n",
" print(f\"\\n㪠Answer:\\n{result['answer']}\")\n",
" else:\n",
" print(f\"β Error: {response.status_code}\")\n",
" print(response.text)\n",
"except Exception as e:\n",
" print(f\"β Connection error: {e}\")\n",
" print(\" Make sure the server is running (cell 8)\")"
]
},
{
"cell_type": "markdown",
"id": "86a8d4bb",
"metadata": {},
"source": [
"---\n",
"\n",
"## β
Summary - Local Windows Setup\n",
"\n",
"Your RAG API is now configured for **local Windows** use:\n",
"\n",
"### How to Use:\n",
"1. β
**Run cells 1-4** to install packages and load functions\n",
"2. β
**Add PDFs** to the `rag_data/pdfs` folder in your project directory\n",
"3. β
**Run cell 5** to process PDFs and build the vector database\n",
"4. β
**Run cell 6** to test RAG queries directly\n",
"5. β
**Run cell 8** to start the local API server\n",
"6. β
**Access API docs** at http://localhost:8000/docs\n",
"\n",
"### Key Features:\n",
"- π Data stored locally in `rag_data/` folder\n",
"- π Answers from PDF documents first\n",
"- π€ Falls back to Gemini API when needed\n",
"- π Local API server at http://localhost:8000\n",
"- πΎ FAISS index persists between sessions\n",
"\n",
"### Quick Test:\n",
"```python\n",
"# Direct RAG query (no API)\n",
"result = rag_answer(\"Your question here\", relevance_threshold=2.0)\n",
"print(result['answer'])\n",
"```\n",
"\n",
"### Next Steps:\n",
"- Add more PDFs to `rag_data/pdfs/` folder\n",
"- Rerun cell 5 to add them to the database\n",
"- Adjust `relevance_threshold` (lower = stricter, higher = more lenient)\n",
"- Access interactive API docs at http://localhost:8000/docs"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|