Spaces:
Runtime error
Runtime error
File size: 24,779 Bytes
5135ab4 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3a800e93-1e4a-40de-a211-4244e8d1a161",
"metadata": {},
"outputs": [],
"source": [
"#!pip install -qU langchain-google-genai"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "764db45e-0ed0-480b-b338-2d7747d7746d",
"metadata": {},
"outputs": [],
"source": [
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain.chains import LLMChain\n",
"import os\n",
"#from google.colab import userdata "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d5b8529b-7206-4f51-804c-0a49b3242310",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"MY_SECRET_KEY\"] = \"AIzaSyDRj3wAgqOCjc_D45W_u-G3y9dk5YDgxEo\"\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f484e386-e7a8-44a9-9ad5-0ec977a8b618",
"metadata": {},
"outputs": [],
"source": [
"#pip install fastapi uvicorn google-generativeai"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "bcb32fd9-805c-409c-aa27-2745867daf41",
"metadata": {},
"outputs": [],
"source": [
"from fastapi import FastAPI\n",
"import google.generativeai as genai\n",
"from fastapi.middleware.cors import CORSMiddleware\n",
"\n",
"# Configure Google Gemini API\n",
"from langchain_google_genai import ChatGoogleGenerativeAI"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "34b28dd0-5675-48ac-b07a-5ee27b5a04dd",
"metadata": {},
"outputs": [],
"source": [
"google_api_key = os.environ[\"MY_SECRET_KEY\"]\n",
"\n",
"# Check if the API key was found\n",
"if google_api_key:\n",
" # Set the environment variable if the API key was found\n",
" os.environ[\"GOOGLE_API_KEY\"] = google_api_key\n",
"\n",
" llm = ChatGoogleGenerativeAI(\n",
" model=\"gemini-pro\", # Specify the model name\n",
" google_api_key=os.environ[\"GOOGLE_API_KEY\"]\n",
" )\n",
"else:\n",
" print(\"Error: GOOGLE_API_KEY not found in Colab secrets. Please store your API key.\")\n",
"\n",
"\n",
"\n",
"genai.configure(api_key=google_api_key)\n",
"model = genai.GenerativeModel(\"gemini-pro\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c452755f-fad6-455f-9822-7c66b36d724f",
"metadata": {},
"outputs": [],
"source": [
"# Initialize FastAPI\n",
"app = FastAPI()\n",
"\n",
"# Enable CORS for frontend access\n",
"app.add_middleware(\n",
" CORSMiddleware,\n",
" allow_origins=[\"*\"],\n",
" allow_credentials=True,\n",
" allow_methods=[\"*\"],\n",
" allow_headers=[\"*\"],\n",
")\n",
"\n",
"@app.get(\"/chat\")\n",
"def chat(query: str):\n",
" response = model.generate_content(query)\n",
" return {\"response\": response.text}\n",
"\n",
"# Run the server: uvicorn backend:app --reload"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "cde9c8d0-80a4-4943-a78a-a75ca4825e34",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/y9/krs1m7td1p33yj75p9f1s5740000gn/T/ipykernel_11301/2071011701.py:7: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n",
" chain = LLMChain(llm=llm, prompt=prompt)\n",
"/var/folders/y9/krs1m7td1p33yj75p9f1s5740000gn/T/ipykernel_11301/2071011701.py:11: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
" response = chain.run(query=query)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chatbot Response: The capital of France is Paris.\n"
]
}
],
"source": [
"prompt = PromptTemplate.from_template(\"Answer the following query: {query}\")\n",
"\n",
"# Initialize LLM\n",
"llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash\", temperature=0)\n",
"\n",
"# Create an LLM Chain\n",
"chain = LLMChain(llm=llm, prompt=prompt)\n",
"\n",
"# Run chatbot\n",
"query = \"What is the capital of France?\"\n",
"response = chain.run(query=query)\n",
"print(\"Chatbot Response:\", response)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "da1d9899-328b-47c2-87a5-4175519bacdc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chatbot Response: The capital of India is **New Delhi**.\n"
]
}
],
"source": [
"# Run chatbot\n",
"query = \"What is the capital of India?\"\n",
"response = chain.run(query=query)\n",
"print(\"Chatbot Response:\", response)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "d42c4cf2-8b96-4310-8692-350d0d9c85b4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/Users/saurabhverma/GENAI'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"os.getcwd()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "161cd070-2ac4-422a-8199-2a7cc42ac335",
"metadata": {},
"outputs": [],
"source": [
"# UI with Gradio\n",
"def chat_interface(question):\n",
" return rag_pipeline(question)\n",
"\n",
"ui = gr.Interface(fn=chat_interface, inputs=\"text\", outputs=\"text\", title=\"RAG Chat with Gemini\")\n",
"ui.launch()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "cc81ff71-c152-4780-bb90-31f1df623f7e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting gradio\n",
" Downloading gradio-5.20.1-py3-none-any.whl.metadata (16 kB)\n",
"Collecting aiofiles<24.0,>=22.0 (from gradio)\n",
" Downloading aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)\n",
"Requirement already satisfied: anyio<5.0,>=3.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (4.2.0)\n",
"Requirement already satisfied: fastapi<1.0,>=0.115.2 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (0.115.11)\n",
"Collecting ffmpy (from gradio)\n",
" Downloading ffmpy-0.5.0-py3-none-any.whl.metadata (3.0 kB)\n",
"Collecting gradio-client==1.7.2 (from gradio)\n",
" Downloading gradio_client-1.7.2-py3-none-any.whl.metadata (7.1 kB)\n",
"Collecting groovy~=0.1 (from gradio)\n",
" Downloading groovy-0.1.2-py3-none-any.whl.metadata (6.1 kB)\n",
"Requirement already satisfied: httpx>=0.24.1 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (0.27.0)\n",
"Collecting huggingface-hub>=0.28.1 (from gradio)\n",
" Downloading huggingface_hub-0.29.2-py3-none-any.whl.metadata (13 kB)\n",
"Requirement already satisfied: jinja2<4.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (3.1.4)\n",
"Requirement already satisfied: markupsafe~=2.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (2.1.3)\n",
"Requirement already satisfied: numpy<3.0,>=1.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (1.26.4)\n",
"Requirement already satisfied: orjson~=3.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (3.10.15)\n",
"Requirement already satisfied: packaging in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (23.2)\n",
"Requirement already satisfied: pandas<3.0,>=1.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (2.2.2)\n",
"Requirement already satisfied: pillow<12.0,>=8.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (10.3.0)\n",
"Requirement already satisfied: pydantic>=2.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (2.10.3)\n",
"Collecting pydub (from gradio)\n",
" Downloading pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
"Collecting python-multipart>=0.0.18 (from gradio)\n",
" Downloading python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)\n",
"Requirement already satisfied: pyyaml<7.0,>=5.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (6.0.1)\n",
"Collecting ruff>=0.9.3 (from gradio)\n",
" Downloading ruff-0.9.10-py3-none-macosx_11_0_arm64.whl.metadata (25 kB)\n",
"Collecting safehttpx<0.2.0,>=0.1.6 (from gradio)\n",
" Downloading safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)\n",
"Collecting semantic-version~=2.0 (from gradio)\n",
" Downloading semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n",
"Requirement already satisfied: starlette<1.0,>=0.40.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (0.46.1)\n",
"Collecting tomlkit<0.14.0,>=0.12.0 (from gradio)\n",
" Downloading tomlkit-0.13.2-py3-none-any.whl.metadata (2.7 kB)\n",
"Collecting typer<1.0,>=0.12 (from gradio)\n",
" Downloading typer-0.15.2-py3-none-any.whl.metadata (15 kB)\n",
"Requirement already satisfied: typing-extensions~=4.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (4.12.2)\n",
"Requirement already satisfied: uvicorn>=0.14.0 in /opt/anaconda3/lib/python3.12/site-packages (from gradio) (0.34.0)\n",
"Requirement already satisfied: fsspec in /opt/anaconda3/lib/python3.12/site-packages (from gradio-client==1.7.2->gradio) (2024.3.1)\n",
"Collecting websockets<16.0,>=10.0 (from gradio-client==1.7.2->gradio)\n",
" Downloading websockets-15.0.1-cp312-cp312-macosx_11_0_arm64.whl.metadata (6.8 kB)\n",
"Requirement already satisfied: idna>=2.8 in /opt/anaconda3/lib/python3.12/site-packages (from anyio<5.0,>=3.0->gradio) (3.7)\n",
"Requirement already satisfied: sniffio>=1.1 in /opt/anaconda3/lib/python3.12/site-packages (from anyio<5.0,>=3.0->gradio) (1.3.0)\n",
"Requirement already satisfied: certifi in /opt/anaconda3/lib/python3.12/site-packages (from httpx>=0.24.1->gradio) (2024.12.14)\n",
"Requirement already satisfied: httpcore==1.* in /opt/anaconda3/lib/python3.12/site-packages (from httpx>=0.24.1->gradio) (1.0.2)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in /opt/anaconda3/lib/python3.12/site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n",
"Requirement already satisfied: filelock in /opt/anaconda3/lib/python3.12/site-packages (from huggingface-hub>=0.28.1->gradio) (3.13.1)\n",
"Requirement already satisfied: requests in /opt/anaconda3/lib/python3.12/site-packages (from huggingface-hub>=0.28.1->gradio) (2.32.2)\n",
"Requirement already satisfied: tqdm>=4.42.1 in /opt/anaconda3/lib/python3.12/site-packages (from huggingface-hub>=0.28.1->gradio) (4.66.4)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /opt/anaconda3/lib/python3.12/site-packages (from pandas<3.0,>=1.0->gradio) (2.9.0.post0)\n",
"Requirement already satisfied: pytz>=2020.1 in /opt/anaconda3/lib/python3.12/site-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n",
"Requirement already satisfied: tzdata>=2022.7 in /opt/anaconda3/lib/python3.12/site-packages (from pandas<3.0,>=1.0->gradio) (2023.3)\n",
"Requirement already satisfied: annotated-types>=0.6.0 in /opt/anaconda3/lib/python3.12/site-packages (from pydantic>=2.0->gradio) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.27.1 in /opt/anaconda3/lib/python3.12/site-packages (from pydantic>=2.0->gradio) (2.27.1)\n",
"Requirement already satisfied: click>=8.0.0 in /opt/anaconda3/lib/python3.12/site-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
"Requirement already satisfied: shellingham>=1.3.0 in /opt/anaconda3/lib/python3.12/site-packages (from typer<1.0,>=0.12->gradio) (1.5.0)\n",
"Requirement already satisfied: rich>=10.11.0 in /opt/anaconda3/lib/python3.12/site-packages (from typer<1.0,>=0.12->gradio) (13.3.5)\n",
"Requirement already satisfied: six>=1.5 in /opt/anaconda3/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.16.0)\n",
"Requirement already satisfied: markdown-it-py<3.0.0,>=2.2.0 in /opt/anaconda3/lib/python3.12/site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.2.0)\n",
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/anaconda3/lib/python3.12/site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.15.1)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/anaconda3/lib/python3.12/site-packages (from requests->huggingface-hub>=0.28.1->gradio) (2.0.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/anaconda3/lib/python3.12/site-packages (from requests->huggingface-hub>=0.28.1->gradio) (2.2.2)\n",
"Requirement already satisfied: mdurl~=0.1 in /opt/anaconda3/lib/python3.12/site-packages (from markdown-it-py<3.0.0,>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.0)\n",
"Downloading gradio-5.20.1-py3-none-any.whl (62.3 MB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m62.3/62.3 MB\u001b[0m \u001b[31m11.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
"\u001b[?25hDownloading gradio_client-1.7.2-py3-none-any.whl (322 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m322.1/322.1 kB\u001b[0m \u001b[31m12.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
"Downloading groovy-0.1.2-py3-none-any.whl (14 kB)\n",
"Downloading huggingface_hub-0.29.2-py3-none-any.whl (468 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m468.1/468.1 kB\u001b[0m \u001b[31m12.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading python_multipart-0.0.20-py3-none-any.whl (24 kB)\n",
"Downloading ruff-0.9.10-py3-none-macosx_11_0_arm64.whl (10.2 MB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m10.2/10.2 MB\u001b[0m \u001b[31m14.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m0:01\u001b[0m\n",
"\u001b[?25hDownloading safehttpx-0.1.6-py3-none-any.whl (8.7 kB)\n",
"Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
"Downloading tomlkit-0.13.2-py3-none-any.whl (37 kB)\n",
"Downloading typer-0.15.2-py3-none-any.whl (45 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m45.1/45.1 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading ffmpy-0.5.0-py3-none-any.whl (6.0 kB)\n",
"Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
"Downloading websockets-15.0.1-cp312-cp312-macosx_11_0_arm64.whl (173 kB)\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m173.3/173.3 kB\u001b[0m \u001b[31m12.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hInstalling collected packages: pydub, websockets, tomlkit, semantic-version, ruff, python-multipart, groovy, ffmpy, aiofiles, huggingface-hub, typer, safehttpx, gradio-client, gradio\n",
" Attempting uninstall: tomlkit\n",
" Found existing installation: tomlkit 0.11.1\n",
" Uninstalling tomlkit-0.11.1:\n",
" Successfully uninstalled tomlkit-0.11.1\n",
" Attempting uninstall: typer\n",
" Found existing installation: typer 0.9.0\n",
" Uninstalling typer-0.9.0:\n",
" Successfully uninstalled typer-0.9.0\n",
"Successfully installed aiofiles-23.2.1 ffmpy-0.5.0 gradio-5.20.1 gradio-client-1.7.2 groovy-0.1.2 huggingface-hub-0.29.2 pydub-0.25.1 python-multipart-0.0.20 ruff-0.9.10 safehttpx-0.1.6 semantic-version-2.10.0 tomlkit-0.13.2 typer-0.15.2 websockets-15.0.1\n"
]
}
],
"source": [
"#pip install -U langchain-community\n",
"!pip install gradio"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8c816fe4-5454-4e30-bbf9-cda7214943f5",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/y9/krs1m7td1p33yj75p9f1s5740000gn/T/ipykernel_16004/3095841870.py:27: LangChainDeprecationWarning: The class `OpenAIEmbeddings` was deprecated in LangChain 0.0.9 and will be removed in 1.0. An updated version of the class exists in the :class:`~langchain-openai package and should be used instead. To use it run `pip install -U :class:`~langchain-openai` and import as `from :class:`~langchain_openai import OpenAIEmbeddings``.\n",
" vector_store = Chroma.from_documents(docs, embedding=OpenAIEmbeddings())\n"
]
},
{
"ename": "ValidationError",
"evalue": "1 validation error for OpenAIEmbeddings\n Value error, Did not find openai_api_key, please add an environment variable `OPENAI_API_KEY` which contains it, or pass `openai_api_key` as a named parameter. [type=value_error, input_value={'model_kwargs': {}, 'cli...20, 'http_client': None}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.10/v/value_error",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[11], line 27\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[38;5;66;03m# Create Vector Database\u001b[39;00m\n\u001b[1;32m 26\u001b[0m docs \u001b[38;5;241m=\u001b[39m load_docs()\n\u001b[0;32m---> 27\u001b[0m vector_store \u001b[38;5;241m=\u001b[39m Chroma\u001b[38;5;241m.\u001b[39mfrom_documents(docs, embedding\u001b[38;5;241m=\u001b[39mOpenAIEmbeddings())\n\u001b[1;32m 29\u001b[0m \u001b[38;5;66;03m# RAG Pipeline: Retrieve and Generate\u001b[39;00m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrag_pipeline\u001b[39m(query):\n",
"File \u001b[0;32m/opt/anaconda3/lib/python3.12/site-packages/langchain_core/_api/deprecation.py:214\u001b[0m, in \u001b[0;36mdeprecated.<locals>.deprecate.<locals>.finalize.<locals>.warn_if_direct_instance\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 212\u001b[0m warned \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 213\u001b[0m emit_warning()\n\u001b[0;32m--> 214\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m wrapped(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[0;32m/opt/anaconda3/lib/python3.12/site-packages/pydantic/main.py:214\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[0;34m(self, **data)\u001b[0m\n\u001b[1;32m 212\u001b[0m \u001b[38;5;66;03m# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks\u001b[39;00m\n\u001b[1;32m 213\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 214\u001b[0m validated_self \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m__pydantic_validator__\u001b[38;5;241m.\u001b[39mvalidate_python(data, self_instance\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n\u001b[1;32m 215\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m validated_self:\n\u001b[1;32m 216\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(\n\u001b[1;32m 217\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mA custom validator is returning a value other than `self`.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 218\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mReturning anything other than `self` from a top level model validator isn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt supported when validating via `__init__`.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 219\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSee the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m 220\u001b[0m stacklevel\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m,\n\u001b[1;32m 221\u001b[0m )\n",
"\u001b[0;31mValidationError\u001b[0m: 1 validation error for OpenAIEmbeddings\n Value error, Did not find openai_api_key, please add an environment variable `OPENAI_API_KEY` which contains it, or pass `openai_api_key` as a named parameter. [type=value_error, input_value={'model_kwargs': {}, 'cli...20, 'http_client': None}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.10/v/value_error"
]
}
],
"source": [
"import google.generativeai as genai\n",
"from langchain.vectorstores import Chroma\n",
"from langchain.embeddings import OpenAIEmbeddings\n",
"from langchain.schema import Document\n",
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
"from langchain_community.document_loaders import TextLoader\n",
"import gradio as gr\n",
"\n",
"# Configure Google Gemini API\n",
"GOOGLE_API_KEY = \"AIzaSyDRj3wAgqOCjc_D45W_u-G3y9dk5YDgxEo\"\n",
"genai.configure(api_key=GOOGLE_API_KEY)\n",
"model = genai.GenerativeModel(\"gemini-pro\")\n",
"\n",
"# Load and process documents\n",
"def load_docs():\n",
" raw_text = \"\"\"\n",
" Machine learning is a branch of artificial intelligence (AI) focused on building applications that learn from data and improve their accuracy over time.\n",
" Supervised learning uses labeled data, while unsupervised learning finds hidden patterns.\n",
" Reinforcement learning is based on rewards and penalties.\n",
" \"\"\"\n",
" text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=10)\n",
" docs = [Document(page_content=text) for text in text_splitter.split_text(raw_text)]\n",
" return docs\n",
"\n",
"# Create Vector Database\n",
"docs = load_docs()\n",
"vector_store = Chroma.from_documents(docs, embedding=OpenAIEmbeddings())\n",
"\n",
"# RAG Pipeline: Retrieve and Generate\n",
"def rag_pipeline(query):\n",
" results = vector_store.similarity_search(query, k=2)\n",
" context = \" \".join([doc.page_content for doc in results])\n",
" \n",
" # Pass context + query to Gemini\n",
" full_prompt = f\"Context: {context}\\n\\nQuestion: {query}\\nAnswer:\"\n",
" response = model.generate_content(full_prompt)\n",
" \n",
" return response.text\n",
"\n",
"# UI with Gradio\n",
"def chat_interface(question):\n",
" return rag_pipeline(question)\n",
"\n",
"ui = gr.Interface(fn=chat_interface, inputs=\"text\", outputs=\"text\", title=\"RAG Chat with Gemini\")\n",
"ui.launch()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "93bcb8cd-9202-45c1-98e3-9f9b06387fc2",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|