File size: 27,703 Bytes
1a51ea0 417e17f 1a51ea0 417e17f |
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 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"## Install the requirements"
],
"metadata": {
"id": "z9h3lrtQRhYv"
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "AKBQrqNsjrLB",
"outputId": "0c4fb7c8-6960-45ea-ec65-5b3849ef0337"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[?25l \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m0.0/56.5 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m56.5/56.5 kB\u001b[0m \u001b[31m3.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m7.4/7.4 MB\u001b[0m \u001b[31m52.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m226.7/226.7 kB\u001b[0m \u001b[31m15.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m1.8/1.8 MB\u001b[0m \u001b[31m47.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m72.9/72.9 kB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m76.4/76.4 kB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m28.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m28.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m211.1/211.1 kB\u001b[0m \u001b[31m13.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m37.9/37.9 MB\u001b[0m \u001b[31m17.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m9.6/9.6 MB\u001b[0m \u001b[31m84.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m101.8/101.8 kB\u001b[0m \u001b[31m6.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m157.3/157.3 kB\u001b[0m \u001b[31m8.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m81.3/81.3 kB\u001b[0m \u001b[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m141.9/141.9 kB\u001b[0m \u001b[31m8.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m295.8/295.8 kB\u001b[0m \u001b[31m18.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m97.6/97.6 kB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m476.0/476.0 kB\u001b[0m \u001b[31m27.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m49.3/49.3 kB\u001b[0m \u001b[31m3.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h Building wheel for html2text (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for tinysegmenter (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for spider-client (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for feedfinder2 (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for jieba3k (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for sgmllib3k (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
]
}
],
"source": [
"pip install -q llama-index==0.10.30 openai==1.12.0 tiktoken==0.6.0 llama-index-readers-web firecrawl-py==1.2.3"
]
},
{
"cell_type": "markdown",
"source": [
"### SET THE ENVIRONMENT VARIABLES"
],
"metadata": {
"id": "TKh_dKV6Rm9a"
}
},
{
"cell_type": "code",
"source": [
"import os\n",
"os.environ[\"OPENAI_API_KEY\"] = \"OPENAI_API_KEY\"\n",
"FIRECRAWL_API_KEY = \"FIRECRAWL_API_KEY\""
],
"metadata": {
"id": "QZgBdtZRJfze"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# SCRAPE WITH FIRECRAWL"
],
"metadata": {
"id": "tiBy7Q74Vkt_"
}
},
{
"cell_type": "markdown",
"source": [
"## IMPORT THE FIRECRAWL WEBREADER\n",
"\n",
"Firecrawl allows you to turn entire websites into LLM-ready markdown\n",
"\n",
"Get the API key here\n",
"https://www.firecrawl.dev/app/api-keys"
],
"metadata": {
"id": "GQP8VA_gRr2g"
}
},
{
"cell_type": "code",
"source": [
"from llama_index.readers.web import FireCrawlWebReader"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YuLILEoNj2lG",
"outputId": "293ad6c5-4859-46bb-e25a-b588c014867a"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"[nltk_data] Downloading package punkt_tab to\n",
"[nltk_data] /usr/local/lib/python3.10/dist-\n",
"[nltk_data] packages/llama_index/core/_static/nltk_cache...\n",
"[nltk_data] Unzipping tokenizers/punkt_tab.zip.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"\n",
"# using firecrawl to crawl a website\n",
"firecrawl_reader = FireCrawlWebReader(\n",
" api_key=FIRECRAWL_API_KEY, # Replace with your actual API key from https://www.firecrawl.dev/\n",
" mode=\"scrape\",\n",
")\n",
"\n",
"# Load documents from a single page URL\n",
"documents = firecrawl_reader.load_data(url=\"https://towardsai.net/\")"
],
"metadata": {
"id": "x-ls9L0QWC3S"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from llama_index.core import VectorStoreIndex\n",
"\n",
"index = VectorStoreIndex.from_documents(documents)\n",
"query_engine = index.as_query_engine()"
],
"metadata": {
"id": "zL1BOBWhWrfe"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"res = query_engine.query(\"What is towards AI aim?\")\n",
"\n",
"print(res.response)\n",
"\n",
"print(\"-----------------\")\n",
"# Show the retrieved nodes\n",
"for src in res.source_nodes:\n",
" print(\"Node ID\\t\", src.node_id)\n",
" print(\"Title\\t\", src.metadata['title'])\n",
" print(\"URL\\t\", src.metadata['sourceURL'])\n",
" print(\"Score\\t\", src.score)\n",
" print(\"Description\\t\", src.metadata.get(\"description\"))\n",
" print(\"-_\"*20)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "AV4Fg-haWspw",
"outputId": "04642e46-1f4f-4e89-972a-326cca793f34"
},
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Towards AI aims to make AI and machine learning accessible to all by providing courses, blogs, tutorials, books, newsletters, and a community platform.\n",
"-----------------\n",
"Node ID\t fd7ec7d6-aaf7-4350-b1fd-7bb9f256abf1\n",
"Title\t Towards AI\n",
"URL\t https://towardsai.net/\n",
"Score\t 0.8927276434780216\n",
"Description\t Towards AI is an online publication, which focuses on sharing high-quality publications, news, articles, and stories on AI and technology related topics.\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
"Node ID\t e8b70bef-9f08-45e9-bb0b-c6177f711740\n",
"Title\t Towards AI\n",
"URL\t https://towardsai.net/\n",
"Score\t 0.8873490308374337\n",
"Description\t Towards AI is an online publication, which focuses on sharing high-quality publications, news, articles, and stories on AI and technology related topics.\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# CRAWL A WEBSITE"
],
"metadata": {
"id": "lRjJrc7VVaNX"
}
},
{
"cell_type": "markdown",
"source": [
"## Load The CSV\n",
"\n",
"CSV contains the list of tools and url of the page which we use to get information about the tool."
],
"metadata": {
"id": "Uj6dqla8SEwd"
}
},
{
"cell_type": "code",
"source": [
"import requests\n",
"import csv\n",
"\n",
"# Google Sheets file URL (CSV export link)\n",
"url = 'https://docs.google.com/spreadsheets/d/1gHB-aQJGt9Nl3cyOP2GorAkBI_Us2AqkYnfqrmejStc/export?format=csv'\n",
"\n",
"# Send a GET request to fetch the CSV file\n",
"response = requests.get(url)\n",
"\n",
"response_list = []\n",
"# Check if the request was successful\n",
"if response.status_code == 200:\n",
" # Decode the content to a string\n",
" content = response.content.decode('utf-8')\n",
"\n",
" # Use the csv.DictReader to read the content as a dictionary\n",
" csv_reader = csv.DictReader(content.splitlines(), delimiter=',')\n",
" response_list = [row for row in csv_reader]\n",
"else:\n",
" print(f\"Failed to retrieve the file: {response.status_code}\")\n"
],
"metadata": {
"id": "aWTgax6zZpLx"
},
"execution_count": 8,
"outputs": []
},
{
"cell_type": "code",
"source": [
"website_list = response_list[3:5] # crawling 2 website for demo"
],
"metadata": {
"id": "atFrRz4MgmaR"
},
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import pprint\n",
"print(\"CSV data\")\n",
"pprint.pprint(website_list)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OHWjBFSQMWZk",
"outputId": "f589564e-5cc3-4031-8481-55b3843d9380"
},
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"CSV data\n",
"[{'': '',\n",
" 'Category': 'Database',\n",
" 'Description': 'Persistent key-value store for fast storage environments',\n",
" 'Is a direct URL company /tool website?': 'Yes',\n",
" 'Name': 'RocksDB',\n",
" 'Tool Type': 'Database',\n",
" 'URL': 'https://rocksdb.org/'},\n",
" {'': '',\n",
" 'Category': 'Database',\n",
" 'Description': 'Document-oriented NoSQL database',\n",
" 'Is a direct URL company /tool website?': 'Yes',\n",
" 'Name': 'MongoDB',\n",
" 'Tool Type': 'Database',\n",
" 'URL': 'https://www.mongodb.com/lp/cloud/atlas/try4?utm_source=google&utm_campaign=search_gs_pl_evergreen_atlas_core_prosp-brand_gic-null_apac-ph_ps-all_desktop_eng_lead&utm_term=mongodb&utm_medium=cpc_paid_search&utm_ad=e&utm_ad_campaign_id=12212624359&adgroup=115749710543&cq_cmp=12212624359&gad_source=1&gclid=CjwKCAjw5Ky1BhAgEiwA5jGujmI0-QgV5DXTwtMUH6mJur8nIVAxkMMSoNHvp_519fBdvutBriWLHxoCe8AQAvD_BwE'}]\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Initialize the Firecrawl"
],
"metadata": {
"id": "S1SO6txzTBT_"
}
},
{
"cell_type": "code",
"source": [
"import os\n",
"from firecrawl import FirecrawlApp\n",
"app = FirecrawlApp(api_key=FIRECRAWL_API_KEY)"
],
"metadata": {
"id": "o8GphTUy7IS_"
},
"execution_count": 13,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import time\n",
"\n",
"# Crawl websites and handle responses\n",
"url_response = {}\n",
"crawl_per_min = 1 # Max crawl per minute\n",
"\n",
"# Track crawls\n",
"crawled_websites = 0\n",
"scraped_pages = 0\n",
"\n",
"for i, website_dict in enumerate(website_list):\n",
" url = website_dict.get('URL')\n",
" print(f\"Crawling: {url}\")\n",
"\n",
" try:\n",
" response = app.crawl_url(\n",
" url,\n",
" params={\n",
" 'limit': 10, # Limit pages to scrape per site.\n",
" 'scrapeOptions': {'formats': ['markdown', 'html']}\n",
" }\n",
" )\n",
" crawled_websites += 1\n",
"\n",
" except Exception as exc:\n",
" print(f\"Failed to fetch {url} -> {exc}\")\n",
" continue\n",
"\n",
" # Store the scraped data and associated info in the response dict\n",
" url_response[url] = {\n",
" \"scraped_data\": response.get(\"data\"),\n",
" \"csv_data\": website_dict\n",
" }\n",
"\n",
" # Pause to comply with crawl per minute limit for free version its 1 crawl per minute\n",
" if i!=len(website_list) and (i + 1) % crawl_per_min == 0:\n",
" print(\"Pausing for 1 minute to comply with crawl limit...\")\n",
" time.sleep(60) # Pause for 1 minute after every crawl\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8-G8fB6KyGP7",
"outputId": "a06cfbb4-a5c0-4a49-8c8e-254943b34c52"
},
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Crawling: https://rocksdb.org/\n",
"Pausing for 1 minute to comply with crawl limit...\n",
"Crawling: https://www.mongodb.com/lp/cloud/atlas/try4?utm_source=google&utm_campaign=search_gs_pl_evergreen_atlas_core_prosp-brand_gic-null_apac-ph_ps-all_desktop_eng_lead&utm_term=mongodb&utm_medium=cpc_paid_search&utm_ad=e&utm_ad_campaign_id=12212624359&adgroup=115749710543&cq_cmp=12212624359&gad_source=1&gclid=CjwKCAjw5Ky1BhAgEiwA5jGujmI0-QgV5DXTwtMUH6mJur8nIVAxkMMSoNHvp_519fBdvutBriWLHxoCe8AQAvD_BwE\n",
"Pausing for 1 minute to comply with crawl limit...\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Create llamaindex documents from the scraped content"
],
"metadata": {
"id": "gwu6VIMvTKp1"
}
},
{
"cell_type": "code",
"source": [
"from llama_index.core import Document\n",
"documents = []\n",
"\n",
"for _, scraped_content in url_response.items():\n",
" csv_data = scraped_content.get(\"csv_data\")\n",
" scraped_results = scraped_content.get(\"scraped_data\")\n",
"\n",
" for scraped_site_dict in scraped_results:\n",
" for result in scraped_results:\n",
" markdown_content = result.get(\"markdown\")\n",
" title = result.get(\"metadata\").get(\"title\")\n",
" url = result.get(\"metadata\").get(\"sourceURL\")\n",
" documents.append(\n",
" Document(\n",
" text=markdown_content,\n",
" metadata={\n",
" \"title\": title,\n",
" \"url\": url,\n",
" \"description\": csv_data.get(\"Description\"),\n",
" \"category\": csv_data.get(\"Category\")\n",
" }\n",
" )\n",
" )\n"
],
"metadata": {
"id": "fHSEWg7FBdSS"
},
"execution_count": 21,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Create The RAG Pipeline."
],
"metadata": {
"id": "uwtRhcpQT294"
}
},
{
"cell_type": "code",
"source": [
"from llama_index.llms.openai import OpenAI\n",
"from llama_index.embeddings.openai import OpenAIEmbedding\n",
"from llama_index.core.node_parser import SentenceSplitter\n",
"\n",
"llm = OpenAI(model=\"gpt-4o-mini\")\n",
"embed_model = OpenAIEmbedding(model=\"text-embedding-3-large\")\n",
"text_splitter = SentenceSplitter(chunk_size=512, chunk_overlap=30)"
],
"metadata": {
"id": "iDOgwesPI-Kq"
},
"execution_count": 16,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from llama_index.core import Settings\n",
"\n",
"Settings.llm = llm\n",
"Settings.embed_model = embed_model\n",
"Settings.text_splitter = text_splitter"
],
"metadata": {
"id": "R8Oi4MiJJQii"
},
"execution_count": 17,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from llama_index.core import VectorStoreIndex\n",
"\n",
"index = VectorStoreIndex.from_documents(documents)\n",
"query_engine = index.as_query_engine()"
],
"metadata": {
"id": "61ZLk2GoJ4VH"
},
"execution_count": 18,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from IPython.display import Markdown, display\n",
"def display_response(response):\n",
" display(Markdown(f\"<b>{response}</b>\"))"
],
"metadata": {
"id": "R1ImxmsoUNVo"
},
"execution_count": 19,
"outputs": []
},
{
"cell_type": "code",
"source": [
"res = query_engine.query(\"I want to use key value store which is the best db?\")\n",
"display_response(res)\n",
"\n",
"print(\"-----------------\")\n",
"# Show the retrieved nodes\n",
"for src in res.source_nodes:\n",
" print(\"Node ID\\t\", src.node_id)\n",
" print(\"Title\\t\", src.metadata['title'])\n",
" print(\"URL\\t\", src.metadata['url'])\n",
" print(\"Score\\t\", src.score)\n",
" print(\"Description\\t\", src.metadata.get(\"description\"))\n",
" print(\"Category\\t\", src.metadata.get(\"category\"))\n",
" print(\"-_\"*20)\n",
"\n",
"\n",
"res = query_engine.query(\"Compare the best No sql dbs?\")\n",
"\n",
"display_response(res.response)\n",
"\n",
"print(\"-----------------\")\n",
"# Show the retrieved nodes\n",
"for src in res.source_nodes:\n",
" print(\"Node ID\\t\", src.node_id)\n",
" print(\"Title\\t\", src.metadata['title'])\n",
" print(\"URL\\t\", src.metadata['url'])\n",
" print(\"Score\\t\", src.score)\n",
" print(\"Description\\t\", src.metadata.get(\"description\"))\n",
" print(\"Category\\t\", src.metadata.get(\"category\"))\n",
" print(\"-_\"*20)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 616
},
"id": "pcFcFgCvJ7Y8",
"outputId": "84449163-47ed-4d15-a080-2a18600e5bc4"
},
"execution_count": 20,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "<b>RocksDB is a high-performance, adaptable key-value store optimized for fast storage environments. It is designed for maximum performance using a log structured database engine written in C++. RocksDB can handle a variety of workloads, from database storage engines to application data caching, making it a versatile option for different data needs.</b>"
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"-----------------\n",
"Node ID\t d8913762-56d9-46e7-be6a-1472e8af426d\n",
"Title\t RocksDB | A persistent key-value store | RocksDB\n",
"URL\t http://rocksdb.org/\n",
"Score\t 0.49236056175089676\n",
"Description\t Persistent key-value store for fast storage environments\n",
"Category\t Database\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.Markdown object>"
],
"text/markdown": "<b>MongoDB Atlas and RocksDB are both popular NoSQL databases, each with its own strengths. MongoDB Atlas is a cloud-based document-oriented database that offers a fully managed service with features like secure default settings, multi-cloud support, and a document model that aligns well with application code. On the other hand, RocksDB is a persistent key-value store known for its high performance in fast storage environments, particularly excelling in terms of low write amplification and write performance. Both databases cater to different use cases and have unique advantages based on the specific requirements of the application or workload.</b>"
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"-----------------\n",
"Node ID\t 0e8a82be-ca4d-4526-b45b-7b78d2323d42\n",
"Title\t MongoDB Atlas: Cloud Document Database | MongoDB\n",
"URL\t https://www.mongodb.com/lp/cloud/atlas/try4?utm_source=google&utm_campaign=search_gs_pl_evergreen_atlas_core_prosp-brand_gic-null_apac-ph_ps-all_desktop_eng_lead&utm_term=mongodb&utm_medium=cpc_paid_search&utm_ad=e&utm_ad_campaign_id=12212624359&adgroup=115749710543&cq_cmp=12212624359&gad_source=1&gclid=CjwKCAjw5Ky1BhAgEiwA5jGujmI0-QgV5DXTwtMUH6mJur8nIVAxkMMSoNHvp_519fBdvutBriWLHxoCe8AQAvD_BwE\n",
"Score\t 0.46048151159026907\n",
"Description\t Document-oriented NoSQL database\n",
"Category\t Database\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n",
"Node ID\t 4f6405f8-eec6-482b-b4d8-d9cc76e436a3\n",
"Title\t Blog | RocksDB\n",
"URL\t https://rocksdb.org/blog/\n",
"Score\t 0.39345216447105164\n",
"Description\t Persistent key-value store for fast storage environments\n",
"Category\t Database\n",
"-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "PZW4aivJYQ5I"
},
"execution_count": 20,
"outputs": []
}
]
}
|