Spaces:
Sleeping
Sleeping
File size: 37,728 Bytes
c25f92a 4756a36 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a 4756a36 2ba4984 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: nest_asyncio in ./.venv/lib/python3.13/site-packages (1.6.0)\n",
"Requirement already satisfied: langchain_openai in ./.venv/lib/python3.13/site-packages (0.3.6)\n",
"Requirement already satisfied: langchain_huggingface in ./.venv/lib/python3.13/site-packages (0.1.2)\n",
"Requirement already satisfied: langchain_core in ./.venv/lib/python3.13/site-packages (0.3.37)\n",
"Requirement already satisfied: langchain in ./.venv/lib/python3.13/site-packages (0.3.19)\n",
"Requirement already satisfied: langchain_community in ./.venv/lib/python3.13/site-packages (0.3.18)\n",
"Requirement already satisfied: langchain-text-splitters in ./.venv/lib/python3.13/site-packages (0.3.6)\n",
"Requirement already satisfied: faiss-cpu in ./.venv/lib/python3.13/site-packages (1.10.0)\n",
"Requirement already satisfied: python-pptx==1.0.2 in ./.venv/lib/python3.13/site-packages (1.0.2)\n",
"Requirement already satisfied: nltk==3.9.1 in ./.venv/lib/python3.13/site-packages (3.9.1)\n",
"Requirement already satisfied: pymupdf in ./.venv/lib/python3.13/site-packages (1.25.3)\n",
"Requirement already satisfied: beautifulsoup4 in ./.venv/lib/python3.13/site-packages (4.13.3)\n",
"Requirement already satisfied: lxml in ./.venv/lib/python3.13/site-packages (5.3.1)\n",
"Requirement already satisfied: sentence-transformers in ./.venv/lib/python3.13/site-packages (3.4.1)\n",
"Requirement already satisfied: IProgress in ./.venv/lib/python3.13/site-packages (0.4)\n",
"Requirement already satisfied: huggingface_hub in ./.venv/lib/python3.13/site-packages (0.29.1)\n",
"Requirement already satisfied: ipywidgets in ./.venv/lib/python3.13/site-packages (8.1.5)\n",
"Requirement already satisfied: qdrant-client in ./.venv/lib/python3.13/site-packages (1.13.2)\n",
"Requirement already satisfied: Pillow>=3.3.2 in ./.venv/lib/python3.13/site-packages (from python-pptx==1.0.2) (11.1.0)\n",
"Requirement already satisfied: XlsxWriter>=0.5.7 in ./.venv/lib/python3.13/site-packages (from python-pptx==1.0.2) (3.2.2)\n",
"Requirement already satisfied: typing-extensions>=4.9.0 in ./.venv/lib/python3.13/site-packages (from python-pptx==1.0.2) (4.12.2)\n",
"Requirement already satisfied: click in ./.venv/lib/python3.13/site-packages (from nltk==3.9.1) (8.1.8)\n",
"Requirement already satisfied: joblib in ./.venv/lib/python3.13/site-packages (from nltk==3.9.1) (1.4.2)\n",
"Requirement already satisfied: regex>=2021.8.3 in ./.venv/lib/python3.13/site-packages (from nltk==3.9.1) (2024.11.6)\n",
"Requirement already satisfied: tqdm in ./.venv/lib/python3.13/site-packages (from nltk==3.9.1) (4.67.1)\n",
"Requirement already satisfied: openai<2.0.0,>=1.58.1 in ./.venv/lib/python3.13/site-packages (from langchain_openai) (1.63.2)\n",
"Requirement already satisfied: tiktoken<1,>=0.7 in ./.venv/lib/python3.13/site-packages (from langchain_openai) (0.9.0)\n",
"Requirement already satisfied: tokenizers>=0.19.1 in ./.venv/lib/python3.13/site-packages (from langchain_huggingface) (0.21.0)\n",
"Requirement already satisfied: transformers>=4.39.0 in ./.venv/lib/python3.13/site-packages (from langchain_huggingface) (4.49.0)\n",
"Requirement already satisfied: langsmith<0.4,>=0.1.125 in ./.venv/lib/python3.13/site-packages (from langchain_core) (0.3.10)\n",
"Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in ./.venv/lib/python3.13/site-packages (from langchain_core) (9.0.0)\n",
"Requirement already satisfied: jsonpatch<2.0,>=1.33 in ./.venv/lib/python3.13/site-packages (from langchain_core) (1.33)\n",
"Requirement already satisfied: PyYAML>=5.3 in ./.venv/lib/python3.13/site-packages (from langchain_core) (6.0.2)\n",
"Requirement already satisfied: packaging<25,>=23.2 in ./.venv/lib/python3.13/site-packages (from langchain_core) (24.2)\n",
"Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in ./.venv/lib/python3.13/site-packages (from langchain_core) (2.10.6)\n",
"Requirement already satisfied: SQLAlchemy<3,>=1.4 in ./.venv/lib/python3.13/site-packages (from langchain) (2.0.38)\n",
"Requirement already satisfied: requests<3,>=2 in ./.venv/lib/python3.13/site-packages (from langchain) (2.32.3)\n",
"Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in ./.venv/lib/python3.13/site-packages (from langchain) (3.11.12)\n",
"Requirement already satisfied: numpy<3,>=1.26.2 in ./.venv/lib/python3.13/site-packages (from langchain) (2.2.3)\n",
"Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in ./.venv/lib/python3.13/site-packages (from langchain_community) (0.6.7)\n",
"Requirement already satisfied: pydantic-settings<3.0.0,>=2.4.0 in ./.venv/lib/python3.13/site-packages (from langchain_community) (2.8.0)\n",
"Requirement already satisfied: httpx-sse<1.0.0,>=0.4.0 in ./.venv/lib/python3.13/site-packages (from langchain_community) (0.4.0)\n",
"Requirement already satisfied: soupsieve>1.2 in ./.venv/lib/python3.13/site-packages (from beautifulsoup4) (2.6)\n",
"Requirement already satisfied: torch>=1.11.0 in ./.venv/lib/python3.13/site-packages (from sentence-transformers) (2.6.0)\n",
"Requirement already satisfied: scikit-learn in ./.venv/lib/python3.13/site-packages (from sentence-transformers) (1.6.1)\n",
"Requirement already satisfied: scipy in ./.venv/lib/python3.13/site-packages (from sentence-transformers) (1.15.2)\n",
"Requirement already satisfied: six in ./.venv/lib/python3.13/site-packages (from IProgress) (1.17.0)\n",
"Requirement already satisfied: filelock in ./.venv/lib/python3.13/site-packages (from huggingface_hub) (3.17.0)\n",
"Requirement already satisfied: fsspec>=2023.5.0 in ./.venv/lib/python3.13/site-packages (from huggingface_hub) (2024.12.0)\n",
"Requirement already satisfied: comm>=0.1.3 in ./.venv/lib/python3.13/site-packages (from ipywidgets) (0.2.2)\n",
"Requirement already satisfied: ipython>=6.1.0 in ./.venv/lib/python3.13/site-packages (from ipywidgets) (8.32.0)\n",
"Requirement already satisfied: traitlets>=4.3.1 in ./.venv/lib/python3.13/site-packages (from ipywidgets) (5.14.3)\n",
"Requirement already satisfied: widgetsnbextension~=4.0.12 in ./.venv/lib/python3.13/site-packages (from ipywidgets) (4.0.13)\n",
"Requirement already satisfied: jupyterlab-widgets~=3.0.12 in ./.venv/lib/python3.13/site-packages (from ipywidgets) (3.0.13)\n",
"Requirement already satisfied: grpcio>=1.41.0 in ./.venv/lib/python3.13/site-packages (from qdrant-client) (1.70.0)\n",
"Requirement already satisfied: grpcio-tools>=1.41.0 in ./.venv/lib/python3.13/site-packages (from qdrant-client) (1.70.0)\n",
"Requirement already satisfied: httpx>=0.20.0 in ./.venv/lib/python3.13/site-packages (from httpx[http2]>=0.20.0->qdrant-client) (0.28.1)\n",
"Requirement already satisfied: portalocker<3.0.0,>=2.7.0 in ./.venv/lib/python3.13/site-packages (from qdrant-client) (2.10.1)\n",
"Requirement already satisfied: urllib3<3,>=1.26.14 in ./.venv/lib/python3.13/site-packages (from qdrant-client) (2.3.0)\n",
"Requirement already satisfied: aiohappyeyeballs>=2.3.0 in ./.venv/lib/python3.13/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.4.6)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in ./.venv/lib/python3.13/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.2)\n",
"Requirement already satisfied: attrs>=17.3.0 in ./.venv/lib/python3.13/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (25.1.0)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in ./.venv/lib/python3.13/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.5.0)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in ./.venv/lib/python3.13/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.1.0)\n",
"Requirement already satisfied: propcache>=0.2.0 in ./.venv/lib/python3.13/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (0.3.0)\n",
"Requirement already satisfied: yarl<2.0,>=1.17.0 in ./.venv/lib/python3.13/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.18.3)\n",
"Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in ./.venv/lib/python3.13/site-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (3.26.1)\n",
"Requirement already satisfied: typing-inspect<1,>=0.4.0 in ./.venv/lib/python3.13/site-packages (from dataclasses-json<0.7,>=0.5.7->langchain_community) (0.9.0)\n",
"Requirement already satisfied: protobuf<6.0dev,>=5.26.1 in ./.venv/lib/python3.13/site-packages (from grpcio-tools>=1.41.0->qdrant-client) (5.29.3)\n",
"Requirement already satisfied: setuptools in ./.venv/lib/python3.13/site-packages (from grpcio-tools>=1.41.0->qdrant-client) (75.8.0)\n",
"Requirement already satisfied: anyio in ./.venv/lib/python3.13/site-packages (from httpx>=0.20.0->httpx[http2]>=0.20.0->qdrant-client) (4.8.0)\n",
"Requirement already satisfied: certifi in ./.venv/lib/python3.13/site-packages (from httpx>=0.20.0->httpx[http2]>=0.20.0->qdrant-client) (2025.1.31)\n",
"Requirement already satisfied: httpcore==1.* in ./.venv/lib/python3.13/site-packages (from httpx>=0.20.0->httpx[http2]>=0.20.0->qdrant-client) (1.0.7)\n",
"Requirement already satisfied: idna in ./.venv/lib/python3.13/site-packages (from httpx>=0.20.0->httpx[http2]>=0.20.0->qdrant-client) (3.10)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in ./.venv/lib/python3.13/site-packages (from httpcore==1.*->httpx>=0.20.0->httpx[http2]>=0.20.0->qdrant-client) (0.14.0)\n",
"Requirement already satisfied: h2<5,>=3 in ./.venv/lib/python3.13/site-packages (from httpx[http2]>=0.20.0->qdrant-client) (4.2.0)\n",
"Requirement already satisfied: decorator in ./.venv/lib/python3.13/site-packages (from ipython>=6.1.0->ipywidgets) (5.2.1)\n",
"Requirement already satisfied: jedi>=0.16 in ./.venv/lib/python3.13/site-packages (from ipython>=6.1.0->ipywidgets) (0.19.2)\n",
"Requirement already satisfied: matplotlib-inline in ./.venv/lib/python3.13/site-packages (from ipython>=6.1.0->ipywidgets) (0.1.7)\n",
"Requirement already satisfied: pexpect>4.3 in ./.venv/lib/python3.13/site-packages (from ipython>=6.1.0->ipywidgets) (4.9.0)\n",
"Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in ./.venv/lib/python3.13/site-packages (from ipython>=6.1.0->ipywidgets) (3.0.50)\n",
"Requirement already satisfied: pygments>=2.4.0 in ./.venv/lib/python3.13/site-packages (from ipython>=6.1.0->ipywidgets) (2.19.1)\n",
"Requirement already satisfied: stack_data in ./.venv/lib/python3.13/site-packages (from ipython>=6.1.0->ipywidgets) (0.6.3)\n",
"Requirement already satisfied: jsonpointer>=1.9 in ./.venv/lib/python3.13/site-packages (from jsonpatch<2.0,>=1.33->langchain_core) (3.0.0)\n",
"Requirement already satisfied: orjson<4.0.0,>=3.9.14 in ./.venv/lib/python3.13/site-packages (from langsmith<0.4,>=0.1.125->langchain_core) (3.10.15)\n",
"Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in ./.venv/lib/python3.13/site-packages (from langsmith<0.4,>=0.1.125->langchain_core) (1.0.0)\n",
"Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in ./.venv/lib/python3.13/site-packages (from langsmith<0.4,>=0.1.125->langchain_core) (0.23.0)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in ./.venv/lib/python3.13/site-packages (from openai<2.0.0,>=1.58.1->langchain_openai) (1.9.0)\n",
"Requirement already satisfied: jiter<1,>=0.4.0 in ./.venv/lib/python3.13/site-packages (from openai<2.0.0,>=1.58.1->langchain_openai) (0.8.2)\n",
"Requirement already satisfied: sniffio in ./.venv/lib/python3.13/site-packages (from openai<2.0.0,>=1.58.1->langchain_openai) (1.3.1)\n",
"Requirement already satisfied: annotated-types>=0.6.0 in ./.venv/lib/python3.13/site-packages (from pydantic<3.0.0,>=2.7.4->langchain_core) (0.7.0)\n",
"Requirement already satisfied: pydantic-core==2.27.2 in ./.venv/lib/python3.13/site-packages (from pydantic<3.0.0,>=2.7.4->langchain_core) (2.27.2)\n",
"Requirement already satisfied: python-dotenv>=0.21.0 in ./.venv/lib/python3.13/site-packages (from pydantic-settings<3.0.0,>=2.4.0->langchain_community) (1.0.1)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in ./.venv/lib/python3.13/site-packages (from requests<3,>=2->langchain) (3.4.1)\n",
"Requirement already satisfied: greenlet!=0.4.17 in ./.venv/lib/python3.13/site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.1.1)\n",
"Requirement already satisfied: networkx in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (3.4.2)\n",
"Requirement already satisfied: jinja2 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (3.1.5)\n",
"Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.4.127 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (12.4.127)\n",
"Requirement already satisfied: nvidia-cuda-runtime-cu12==12.4.127 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (12.4.127)\n",
"Requirement already satisfied: nvidia-cuda-cupti-cu12==12.4.127 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (12.4.127)\n",
"Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (9.1.0.70)\n",
"Requirement already satisfied: nvidia-cublas-cu12==12.4.5.8 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (12.4.5.8)\n",
"Requirement already satisfied: nvidia-cufft-cu12==11.2.1.3 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (11.2.1.3)\n",
"Requirement already satisfied: nvidia-curand-cu12==10.3.5.147 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (10.3.5.147)\n",
"Requirement already satisfied: nvidia-cusolver-cu12==11.6.1.9 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (11.6.1.9)\n",
"Requirement already satisfied: nvidia-cusparse-cu12==12.3.1.170 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (12.3.1.170)\n",
"Requirement already satisfied: nvidia-cusparselt-cu12==0.6.2 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (0.6.2)\n",
"Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (2.21.5)\n",
"Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (12.4.127)\n",
"Requirement already satisfied: nvidia-nvjitlink-cu12==12.4.127 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (12.4.127)\n",
"Requirement already satisfied: triton==3.2.0 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (3.2.0)\n",
"Requirement already satisfied: sympy==1.13.1 in ./.venv/lib/python3.13/site-packages (from torch>=1.11.0->sentence-transformers) (1.13.1)\n",
"Requirement already satisfied: mpmath<1.4,>=1.1.0 in ./.venv/lib/python3.13/site-packages (from sympy==1.13.1->torch>=1.11.0->sentence-transformers) (1.3.0)\n",
"Requirement already satisfied: safetensors>=0.4.1 in ./.venv/lib/python3.13/site-packages (from transformers>=4.39.0->langchain_huggingface) (0.5.2)\n",
"Requirement already satisfied: threadpoolctl>=3.1.0 in ./.venv/lib/python3.13/site-packages (from scikit-learn->sentence-transformers) (3.5.0)\n",
"Requirement already satisfied: hyperframe<7,>=6.1 in ./.venv/lib/python3.13/site-packages (from h2<5,>=3->httpx[http2]>=0.20.0->qdrant-client) (6.1.0)\n",
"Requirement already satisfied: hpack<5,>=4.1 in ./.venv/lib/python3.13/site-packages (from h2<5,>=3->httpx[http2]>=0.20.0->qdrant-client) (4.1.0)\n",
"Requirement already satisfied: parso<0.9.0,>=0.8.4 in ./.venv/lib/python3.13/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.4)\n",
"Requirement already satisfied: ptyprocess>=0.5 in ./.venv/lib/python3.13/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets) (0.7.0)\n",
"Requirement already satisfied: wcwidth in ./.venv/lib/python3.13/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets) (0.2.13)\n",
"Requirement already satisfied: mypy-extensions>=0.3.0 in ./.venv/lib/python3.13/site-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain_community) (1.0.0)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.13/site-packages (from jinja2->torch>=1.11.0->sentence-transformers) (3.0.2)\n",
"Requirement already satisfied: executing>=1.2.0 in ./.venv/lib/python3.13/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (2.2.0)\n",
"Requirement already satisfied: asttokens>=2.1.0 in ./.venv/lib/python3.13/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (3.0.0)\n",
"Requirement already satisfied: pure-eval in ./.venv/lib/python3.13/site-packages (from stack_data->ipython>=6.1.0->ipywidgets) (0.2.3)\n"
]
}
],
"source": [
"# !pip install nest_asyncio \\\n",
"# langchain_openai langchain_huggingface langchain_core langchain langchain_community langchain-text-splitters \\\n",
"# python-pptx==1.0.2 nltk==3.9.1 pymupdf lxml \\\n",
"# sentence-transformers IProgress \\\n",
"# huggingface_hub ipywidgets \\\n",
"# qdrant-client"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"\n",
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import getpass\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter Your OpenAI API Key: \")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"hf_username = getpass.getpass(\"Enter Your Hugging Face Username: \")\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a5c203d394cb4c1d933c1af73ff1c112",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from huggingface_hub import notebook_login\n",
"notebook_login()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'type': 'user', 'id': '67624d1b57e77fe6e0c87ae5', 'name': 'drewgenai', 'fullname': 'Drew DeMarco', 'email': 'drewgenai@gmail.com', 'emailVerified': True, 'canPay': False, 'periodEnd': None, 'isPro': False, 'avatarUrl': 'https://cdn-avatars.huggingface.co/v1/production/uploads/no-auth/L6eLaZmCK4jqW3ZTLYIAR.png', 'orgs': [], 'auth': {'type': 'access_token', 'accessToken': {'displayName': 'newotken', 'role': 'write', 'createdAt': '2025-02-12T04:11:04.130Z'}}}\n"
]
}
],
"source": [
"from huggingface_hub import whoami\n",
"print(whoami())\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mkdir: cannot create directory ‘example_files’: File exists\n",
"mkdir: cannot create directory ‘output’: File exists\n"
]
}
],
"source": [
"!mkdir example_files\n",
"!mkdir output"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.document_loaders import DirectoryLoader\n",
"from langchain_community.document_loaders import PyMuPDFLoader\n",
"\n",
"path = \"example_files/\"\n",
"text_loader = DirectoryLoader(path, glob=\"*.pdf\", loader_cls=PyMuPDFLoader)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1️⃣ Header-Based Chunking (Title-Based Splitter)\n",
"Uses document structure to split on headings, section titles, or patterns.\n",
"Works well for structured documents with named assessments, numbered lists, or headers.\n",
"Example: If it detects Chronic Pain Adjustment Index (CPAI-10), it groups everything under that title.\n",
"2️⃣ Semantic Chunking (Text-Meaning Splitter)\n",
"Uses embeddings or sentence similarity to decide where to break chunks.\n",
"Prevents splitting mid-context if sentences are closely related.\n",
"Example: Groups all related pain-assessment questions into one chunk."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"#!pip install langchain_experimental"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from langchain_experimental.text_splitter import SemanticChunker\n",
"\n",
"from langchain.embeddings import HuggingFaceInferenceAPIEmbeddings\n",
"\n",
"from langchain_huggingface import HuggingFaceEmbeddings\n",
"\n",
"\n",
"model_id = \"Snowflake/snowflake-arctic-embed-m\"\n",
"embedding_model = HuggingFaceEmbeddings(model_name=model_id)\n",
"# model_id = \"Snowflake/snowflake-arctic-embed-m-v2.0\"\n",
"# embedding_model = HuggingFaceEmbeddings(model_name=model_id, model_kwargs={\"trust_remote_code\": True})\n",
"\n",
"\n",
"semantic_splitter = SemanticChunker(embedding_model)\n",
"\n",
"all_documents = text_loader.load()\n",
"documents_with_metadata = []\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"from langchain.schema import Document\n",
"\n",
"for doc in all_documents:\n",
" source_name = doc.metadata.get(\"source\", \"unknown\") # Get document source\n",
"\n",
" # Use SemanticChunker to intelligently split text\n",
" chunks = semantic_splitter.split_text(doc.page_content)\n",
"\n",
" # Convert chunks into LangChain Document format with metadata\n",
" for chunk in chunks:\n",
" doc_chunk = Document(page_content=chunk, metadata={\"source\": source_name})\n",
" documents_with_metadata.append(doc_chunk)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"\n",
"#!pip install -qU huggingface_hub\n",
"#!pip install -qU ipywidgets\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"from sentence_transformers import SentenceTransformer\n",
"from langchain.vectorstores import Qdrant\n",
"from langchain.embeddings import HuggingFaceEmbeddings\n",
"\n",
"\n",
"# Load documents into Qdrant\n",
"qdrant_vectorstore = Qdrant.from_documents(\n",
" documents_with_metadata,\n",
" embedding_model,\n",
" location=\":memory:\", # In-memory for testing\n",
" collection_name=\"document_comparison\",\n",
")\n",
"\n",
"# Create a retriever\n",
"qdrant_retriever = qdrant_vectorstore.as_retriever()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"RAG_PROMPT = \"\"\"\n",
"CONTEXT:\n",
"{context}\n",
"\n",
"QUERY:\n",
"{question}\n",
"\n",
"You are a helpful assistant. Use the available context to answer the question.\n",
"\n",
"Return the response in **valid JSON format** with the following structure:\n",
"\n",
"[\n",
" {{\n",
" \"Derived Description\": \"A short name for the matched concept\",\n",
" \"Protocol_1\": \"Protocol 1 - Matching Element\",\n",
" \"Protocol_2\": \"Protocol 2 - Matching Element\"\n",
" }},\n",
" ...\n",
"]\n",
"\n",
"### Rules:\n",
"1. Only output **valid JSON** with no explanations, summaries, or markdown formatting.\n",
"2. Ensure each entry in the JSON list represents a single matched data element from the two protocols.\n",
"3. If no matching element is found in a protocol, leave it empty (\"\").\n",
"4. **Do NOT include headers, explanations, or additional formatting**—only return the raw JSON list.\n",
"5. It should include all the elements in the two protocols.\n",
"6. If it cannot match the element, create the row and include the protocol it did find and put \"could not match\" in the other protocol column.\n",
"7. protocol should be the between\n",
"\"\"\"\n",
"\n",
"rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)\n",
"\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"openai_chat_model = ChatOpenAI(model=\"gpt-4o-mini\")\n",
"\n",
"from operator import itemgetter\n",
"from langchain.schema.output_parser import StrOutputParser\n",
"\n",
"rag_chain = (\n",
" {\"context\": itemgetter(\"question\") | qdrant_retriever, \"question\": itemgetter(\"question\")}\n",
" | rag_prompt | openai_chat_model | StrOutputParser()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"question_text = \"\"\"You are a helpful assistant. Use the available context to answer the question.\n",
"\n",
"Between these two files containing protocols, identify and match **entire assessment sections** based on conceptual similarity. Do NOT match individual questions.\n",
"\n",
"### **Output Format:**\n",
"Return the response in **valid JSON format** structured as a list of dictionaries, where each dictionary contains:\n",
"\n",
"[\n",
" {\n",
" \"Derived Description\": \"A short name describing the matched sections\",\n",
" \"Protocol_1\": \"Exact section heading from Protocol 1\",\n",
" \"Protocol_2\": \"Exact section heading from Protocol 2\"\n",
" }\n",
"]\n",
"\n",
"### **Matching Criteria:**\n",
"1. **Match entire assessment sections** based on their purpose and overall topic.\n",
"3. If a section in one protocol **has no match**, include it but leave the other protocol's field blank.\n",
"4. The **\"Derived Description\"** should be a **concise label** summarizing the section’s purpose, . It should describe the overall concept of the matched sections.\n",
"\n",
"### **Rules:**\n",
"1. **Only output valid JSON**—no explanations, summaries, or markdown formatting.\n",
"2. **Ensure each entry represents a single section-to-section match.**\n",
"4. **Prioritize conceptual similarity over exact wording** when aligning sections.\n",
"5. If no match is found, leave the unmatched protocol entry blank.\n",
"\n",
"### **Example Output:**\n",
"[\n",
" {\n",
" \"Derived Description\": \"Pain Coping Strategies\",\n",
" \"Protocol_1\": \"Pain Coping Strategy Scale (PCSS-9)\",\n",
" \"Protocol_2\": \"Chronic Pain Adjustment Index (CPAI-10)\"\n",
" },\n",
" {\n",
" \"Derived Description\": \"Work Stress and Fatigue\",\n",
" \"Protocol_1\": \"Work-Related Stress Scale (WRSS-8)\",\n",
" \"Protocol_2\": \"Occupational Fatigue Index (OFI-7)\"\n",
" },\n",
"]\n",
"\n",
"Do not add any additional text, explanations, or formatting—**only return the raw JSON list**.\n",
"\"\"\"\n",
"\n",
"\n",
"\n",
"# The questions within elements will be similar between the two documents and can be used to match the elements.\n",
"\n",
"# 1. Derived description from the two documents describing the index/measure/scale.\n",
"# 2. A column for each standard.\n",
"# 3. In the column for each name/version, the data element used to capture that description that will be the shortened item between ()\n",
"\n",
"# There should only be one row for each scale/index/etc.\n",
"# The description should not be one of the questions but a name that best describes the similar data elements.\"\"\"\n",
"\n",
"response_text = rag_chain.invoke({\"question\": question_text})\n",
"# response = rag_chain.invoke({\"question\": question_text})"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✅ CSV file saved: matching_data_elements.csv\n"
]
}
],
"source": [
"import json\n",
"import pandas as pd\n",
"\n",
"def parse_rag_output(response_text):\n",
" \"\"\"Extract structured JSON data from the RAG response.\"\"\"\n",
" try:\n",
" structured_data = json.loads(response_text)\n",
"\n",
" # Ensure similarity score is always included\n",
" for item in structured_data:\n",
" item.setdefault(\"Similarity Score\", \"N/A\") # Default if missing\n",
"\n",
" return structured_data\n",
" except json.JSONDecodeError:\n",
" print(\"Error: Response is not valid JSON.\")\n",
" return None\n",
"\n",
"def save_to_csv(data, directory=\"./output\", filename=\"matching_data_elements.csv\"):\n",
" \"\"\"Save structured data to CSV.\"\"\"\n",
" if not data:\n",
" print(\"No data to save.\")\n",
" return\n",
"\n",
" file_path = os.path.join(directory, filename)\n",
" df = pd.DataFrame(data, columns=[\"Derived Description\", \"Protocol_1\", \"Protocol_2\"]) # Ensure correct columns\n",
" df.to_csv(file_path, index=False)\n",
" print(f\"✅ CSV file saved: {filename}\")\n",
"\n",
"# Run the pipeline\n",
"structured_output = parse_rag_output(response_text)\n",
"save_to_csv(structured_output)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[\\n {\\n \"Derived Description\": \"Memory Recall\",\\n \"Protocol_1_Name\": \"I struggle to remember names and faces. (Scale: 0-3)\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Memory Retention\",\\n \"Protocol_1_Name\": \"I retain new information effectively.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Mnemonic Techniques\",\\n \"Protocol_1_Name\": \"I practice mnemonic techniques to improve recall.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Task Management Difficulty\",\\n \"Protocol_1_Name\": \"could not match\",\\n \"Protocol_2_Name\": \"I find it difficult to keep track of multiple responsibilities. (Scale: 0-3)\"\\n },\\n {\\n \"Derived Description\": \"Mental Fatigue in Problem-Solving\",\\n \"Protocol_1_Name\": \"could not match\",\\n \"Protocol_2_Name\": \"I get mentally fatigued quickly when problem-solving. (Scale: 0-3)\"\\n },\\n {\\n \"Derived Description\": \"Task Organization Techniques\",\\n \"Protocol_1_Name\": \"could not match\",\\n \"Protocol_2_Name\": \"I use structured techniques to organize my tasks. (Scale: 0-3)\"\\n }\\n]'"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# rag_chain.invoke({\"question\" : \"Based on the types of questions asked under each heading. can you identify the headings in one document that most closely match the second document. list them e.g paincoping/doc1 painstrategy/doc2\"})"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[\\n {\\n \"Derived Description\": \"Memory Recall\",\\n \"Protocol_1_Name\": \"I struggle to remember names and faces.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Retaining Information\",\\n \"Protocol_1_Name\": \"I retain new information effectively.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Mnemonic Techniques\",\\n \"Protocol_1_Name\": \"could not match\",\\n \"Protocol_2_Name\": \"I practice mnemonic techniques to improve recall.\"\\n },\\n {\\n \"Derived Description\": \"Pain Management Preparation\",\\n \"Protocol_1_Name\": \"I mentally prepare myself before engaging in painful activities.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Pain Minimization Techniques\",\\n \"Protocol_1_Name\": \"I use relaxation techniques to minimize pain perception.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Breathing Exercises for Pain\",\\n \"Protocol_1_Name\": \"I use breathing exercises to manage pain episodes.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Avoiding Painful Activities\",\\n \"Protocol_1_Name\": \"I avoid specific physical activities that increase my pain.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Work Exhaustion\",\\n \"Protocol_1_Name\": \"I feel exhausted after a standard workday.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Motivation and Stress\",\\n \"Protocol_1_Name\": \"I struggle to stay motivated due to workplace stress.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Handling Multiple Responsibilities\",\\n \"Protocol_1_Name\": \"could not match\",\\n \"Protocol_2_Name\": \"I find it difficult to keep track of multiple responsibilities.\"\\n },\\n {\\n \"Derived Description\": \"Mental Fatigue from Problem-Solving\",\\n \"Protocol_1_Name\": \"could not match\",\\n \"Protocol_2_Name\": \"I get mentally fatigued quickly when problem-solving.\"\\n },\\n {\\n \"Derived Description\": \"Structured Task Organization\",\\n \"Protocol_1_Name\": \"could not match\",\\n \"Protocol_2_Name\": \"I use structured techniques to organize my tasks.\"\\n },\\n {\\n \"Derived Description\": \"Overwhelmed by Responsibilities\",\\n \"Protocol_1_Name\": \"I feel overwhelmed when handling multiple responsibilities.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Disconnecting from Work\",\\n \"Protocol_1_Name\": \"I find it difficult to disconnect from work-related concerns.\",\\n \"Protocol_2_Name\": \"could not match\"\\n },\\n {\\n \"Derived Description\": \"Sleep Disturbances from Work Stress\",\\n \"Protocol_1_Name\": \"I experience sleep disturbances due to work-related stress.\",\\n \"Protocol_2_Name\": \"could not match\"\\n }\\n]'"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# rag_chain.invoke({\"question\" : \"Based on the types of questions asked under each heading. can you identify the headings in one document that most closely match the second document. list them e.g paincoping/doc1 painstrategy/doc2. these are example headings not the ones in the actual documents. just list the matches not the rational. Can you list multiple matches?\"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|