{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "toc_visible": true, "authorship_tag": "ABX9TyOUem37lhhg0mJYauho+pvb", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "source": [ "!pip install -q llama-index==0.10.30 openai==1.12.0 cohere==4.47 tiktoken==0.6.0 newspaper3k==0.2.8" ], "metadata": { "id": "4CW8ux1RSdem", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "155feab4-8ae6-43da-a07f-8a1f4b677c2b" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.1/211.1 kB\u001b[0m \u001b[31m4.0 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[31m8.7 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[31m9.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.4/7.4 MB\u001b[0m \u001b[31m43.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", " Building wheel for tinysegmenter (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" ] } ] }, { "cell_type": "code", "source": [ "import os\n", "\n", "# Set the \"OPENAI_API_KEY\" in the Python environment. Will be used by OpenAI client later.\n", "os.environ[\"OPENAI_API_KEY\"] = \"[OPENAI_API_KEY]\"\n", "USESCRAPER_API_KEY = \"[USESCRAPER_API_KEY]\"" ], "metadata": { "id": "wxDPsVXSAj6_" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "There are two primary methods for extracting webpage content. The first method involves having a list of URLs; one can iterate through this list to retrieve the content of each page. The second method, web crawling, requires using a script or service to extract page URLs from a sitemap or manually following links on the page to access all the content. Initially, we will explore web scraping techniques before discussing how to use a service like usescraper.com to perform web crawling." ], "metadata": { "id": "VSc7-1mljmrp" } }, { "cell_type": "markdown", "source": [ "# 1. Scraping using `newspaper` Library" ], "metadata": { "id": "D3r2tYHgeIK9" } }, { "cell_type": "markdown", "source": [ "## Define URLs" ], "metadata": { "id": "it43ZQf8jatw" } }, { "cell_type": "code", "source": [ "urls = [\n", " \"https://docs.llamaindex.ai/en/stable/understanding\",\n", " \"https://docs.llamaindex.ai/en/stable/understanding/using_llms/using_llms/\",\n", " \"https://docs.llamaindex.ai/en/stable/understanding/indexing/indexing/\",\n", " \"https://docs.llamaindex.ai/en/stable/understanding/querying/querying/\"\n", "]" ], "metadata": { "id": "x74PqfQ7eIzD" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Get Page Contents" ], "metadata": { "id": "tgxfpfSsjcMC" } }, { "cell_type": "code", "source": [ "import newspaper\n", "\n", "pages_content = []\n", "\n", "# Retrieve the Content\n", "for url in urls:\n", "\ttry:\n", "\t\tarticle = newspaper.Article( url )\n", "\t\tarticle.download()\n", "\t\tarticle.parse()\n", "\t\tif len(article.text) > 0:\n", "\t\t\tpages_content.append({ \"url\": url, \"title\": article.title, \"text\": article.text })\n", "\texcept:\n", "\t\tcontinue" ], "metadata": { "id": "Q6Xs1OhUfVQV" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "pages_content[0]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3cNdJNi2g1ly", "outputId": "f5184c15-6b55-47ee-98ee-646a06290a4c" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'url': 'https://docs.llamaindex.ai/en/stable/understanding',\n", " 'title': 'Building an LLM Application',\n", " 'text': \"Building an LLM application#\\n\\nWelcome to the beginning of Understanding LlamaIndex. This is a series of short, bite-sized tutorials on every stage of building an LLM application to get you acquainted with how to use LlamaIndex before diving into more advanced and subtle strategies. If you're an experienced programmer new to LlamaIndex, this is the place to start.\\n\\nKey steps in building an LLM application#\\n\\nTip If you've already read our high-level concepts page you'll recognize several of these steps.\\n\\nThere are a series of key steps involved in building any LLM-powered application, whether it's answering questions about your data, creating a chatbot, or an autonomous agent. Throughout our documentation, you'll notice sections are arranged roughly in the order you'll perform these steps while building your app. You'll learn about:\\n\\nUsing LLMs : whether it's OpenAI or any number of hosted LLMs or a locally-run model of your own, LLMs are used at every step of the way, from indexing and storing to querying and parsing your data. LlamaIndex comes with a huge number of reliable, tested prompts and we'll also show you how to customize your own.\\n\\nLoading : getting your data from wherever it lives, whether that's unstructured text, PDFs, databases, or APIs to other applications. LlamaIndex has hundreds of connectors to every data source over at LlamaHub.\\n\\nIndexing : once you've got your data there are an infinite number of ways to structure access to that data to ensure your applications is always working with the most relevant data. LlamaIndex has a huge number of these strategies built-in and can help you select the best ones.\\n\\nStoring : you will probably find it more efficient to store your data in indexed form, or pre-processed summaries provided by an LLM, often in a specialized database known as a Vector Store (see below). You can also store your indexes, metadata and more.\\n\\nQuerying : every indexing strategy has a corresponding querying strategy and there are lots of ways to improve the relevance, speed and accuracy of what you retrieve and what the LLM does with it before returning it to you, including turning it into structured responses such as an API.\\n\\nPutting it all together : whether you are building question & answering, chatbots, an API, or an autonomous agent, we show you how to get your application into production.\\n\\nTracing and debugging : also called observability , it's especially important with LLM applications to be able to look into the inner workings of what's going on to help you debug problems and spot places to improve.\\n\\nEvaluating: every strategy has pros and cons and a key part of building, shipping and evolving your application is evaluating whether your change has improved your application in terms of accuracy, performance, clarity, cost and more. Reliably evaluating your changes is a crucial part of LLM application development.\\n\\nReady to dive in? Head to using LLMs.\"}" ] }, "metadata": {}, "execution_count": 57 } ] }, { "cell_type": "code", "source": [ "len( pages_content )" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WleP60A3gkQM", "outputId": "8c79ab53-e47b-4227-eb6f-0286b8ba2d15" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5" ] }, "metadata": {}, "execution_count": 38 } ] }, { "cell_type": "markdown", "source": [ "## Convert to Document" ], "metadata": { "id": "i5mCiRfGjfNx" } }, { "cell_type": "code", "source": [ "from llama_index.core.schema import Document\n", "\n", "# Convert the chunks to Document objects so the LlamaIndex framework can process them.\n", "documents = [Document(text=row['text'], metadata={\"title\": row['title'], \"url\": row['url']}) for row in pages_content]" ], "metadata": { "id": "TOJ3K-CBfVDR" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# 2. Submit the Crawler Job" ], "metadata": { "id": "CkjEyEmkJevT" } }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "tYpchBo5-brp", "outputId": "927f84c5-c13a-408c-8802-df90bc05c733" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{'org': '581', 'id': '7YE3T8VSPJVSCYE6EDQ90DJNFT', 'urls': ['https://docs.llamaindex.ai/en/stable/understanding/'], 'exclude_globs': [], 'exclude_elements': 'nav, header, footer, script, style, noscript, svg, [role=\"alert\"], [role=\"banner\"], [role=\"dialog\"], [role=\"alertdialog\"], [role=\"region\"][aria-label*=\"skip\" i], [aria-modal=\"true\"]', 'output_format': 'markdown', 'output_expiry': 604800, 'min_length': 50, 'page_limit': 10000, 'force_crawling_mode': 'link', 'block_resources': True, 'include_linked_files': False, 'createdAt': 1713883978029, 'status': 'starting', 'use_browser': True, 'sitemapPageCount': 0, 'notices': []}\n" ] } ], "source": [ "import requests\n", "import json\n", "\n", "payload = {\n", " \"urls\": [\"https://docs.llamaindex.ai/en/stable/understanding/\"], # list of urls to crawl\n", " \"output_format\": \"markdown\", # text, html, markdown\n", " \"output_expiry\": 604800, # Automatically delete after X seconds\n", " \"min_length\": 50, # Skip pages with less than X characters\n", " \"page_limit\": 10000, # Maximum number of pages to crawl\n", " \"force_crawling_mode\": \"link\", # \"link\" follows links in the page reccursively, or \"sitemap\" to find pages from website's sitemap\n", " \"block_resources\": True, # skip loading images, stylesheets, or scripts\n", " \"include_linked_files\": False # include files (PDF, text, ...) in output\n", "}\n", "headers = {\n", " \"Authorization\": \"Bearer \" + USESCRAPER_API_KEY,\n", " \"Content-Type\": \"application/json\"\n", "}\n", "\n", "response = requests.request(\"POST\", \"https://api.usescraper.com/crawler/jobs\", json=payload, headers=headers)\n", "\n", "response = json.loads( response.text )\n", "\n", "print(response)" ] }, { "cell_type": "markdown", "source": [ "## Get the Status" ], "metadata": { "id": "nx_4MjHxJgxh" } }, { "cell_type": "code", "source": [ "url = \"https://api.usescraper.com/crawler/jobs/{}\".format(response['id'])\n", "\n", "status_res = requests.request(\"GET\", url, headers=headers)\n", "\n", "status_res = json.loads( status_res.text )\n", "\n", "print( status_res['status'] )\n", "print( status_res['progress'] )" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZLJ0BUR8c1a8", "outputId": "cfd3aee9-68bf-4171-9340-abe2d03fa5ac" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "running\n", "{'scraped': 9, 'discarded': 0, 'failed': 0}\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Get the Data" ], "metadata": { "id": "vHcRJIDsJh2i" } }, { "cell_type": "code", "source": [ "url = \"https://api.usescraper.com/crawler/jobs/{}/data\".format(response['id'])\n", "\n", "data_res = requests.request(\"GET\", url, headers=headers)\n", "\n", "data_res = json.loads( data_res.text )\n", "\n", "print( data_res )" ], "metadata": { "id": "J4dUn4cmGGab" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "print( \"URL:\", data_res['data'][0]['meta']['url'] )\n", "print( \"Title:\", data_res['data'][0]['meta']['meta']['title'] )\n", "print( \"Content:\", data_res['data'][0]['text'][0:500], \"...\" )" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "F8VEQvJkITLJ", "outputId": "b54ec108-7221-4230-8b61-d0a4be503a66" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "URL: https://docs.llamaindex.ai/en/stable/understanding/putting_it_all_together/graphs/\n", "Title: Knowledge Graphs - LlamaIndex\n", "Content: \n", "[ Skip to content ](https://docs.llamaindex.ai/en/stable/understanding/putting_it_all_together/graphs/#knowledge-graphs)\n", "#Knowledge Graphs[#](https://docs.llamaindex.ai/en/stable/understanding/putting_it_all_together/graphs/#knowledge-graphs)\n", "LlamaIndex contains some fantastic guides for building with knowledge graphs.\n", "\n", "Check out the end-to-end tutorials/workshops below. Also check out our [knowledge graph query engine guides](https://docs.llamaindex.ai/en/stable/module_guides/deploying/query_ ...\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Convert to Document" ], "metadata": { "id": "rt2nyuLhSYLR" } }, { "cell_type": "code", "source": [ "from llama_index.core.schema import Document\n", "\n", "# Convert the chunks to Document objects so the LlamaIndex framework can process them.\n", "documents = [Document(text=row['text'], metadata={\"title\": row['meta']['meta']['title'], \"url\": row['meta']['url']}) for row in data_res['data']]" ], "metadata": { "id": "YEieGzSFSXas" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Create RAG Pipeline" ], "metadata": { "id": "vqbJG5a1i3Jo" } }, { "cell_type": "code", "source": [ "from llama_index.llms.openai import OpenAI\n", "\n", "llm = OpenAI(model=\"gpt-3.5-turbo\")" ], "metadata": { "id": "wxmiQDv3SXV6" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from llama_index.embeddings.openai import OpenAIEmbedding\n", "\n", "embed_model = OpenAIEmbedding(model=\"text-embedding-3-large\")" ], "metadata": { "id": "tCVhv4OkSXTV" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from llama_index.core.node_parser import SentenceSplitter\n", "\n", "text_splitter = SentenceSplitter(chunk_size=512, chunk_overlap=30)" ], "metadata": { "id": "quwJI61dNVr-" }, "execution_count": null, "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": "6KpeCRMBUgup" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from llama_index.core import VectorStoreIndex\n", "\n", "index = VectorStoreIndex.from_documents( documents )" ], "metadata": { "id": "nWTBidwoZSO0" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "query_engine = index.as_query_engine()" ], "metadata": { "id": "RUuJO0IIYSeU" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "res = query_engine.query(\"What is a query engine?\")" ], "metadata": { "id": "6_s2LkH6YX1V" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "res.response" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 71 }, "id": "02zdJNqIZKep", "outputId": "76340610-0d98-4fd0-d237-ddb9f1752391" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'A query engine is a fundamental component used in querying processes. It is responsible for retrieving the most relevant documents from an index based on a query, postprocessing the retrieved nodes if needed, and then synthesizing a response by combining the query, relevant data, and prompt to be sent to the language model for generating an answer.'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 28 } ] }, { "cell_type": "code", "source": [ "# 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(\"-_\"*20)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PuCcgP0nZSIl", "outputId": "e136cdbb-2ee4-4dfb-f532-f6c9365e519e" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Node ID\t 081b6c8c-d9ea-4476-bac0-1008facd3db8\n", "Title\t Querying - LlamaIndex\n", "URL\t https://docs.llamaindex.ai/en/stable/understanding/querying/querying/\n", "Score\t 0.46212738505767387\n", "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n", "Node ID\t 3786c195-c5de-4bba-98b6-996031349a88\n", "Title\t Querying - LlamaIndex\n", "URL\t https://docs.llamaindex.ai/en/stable/understanding/querying/querying/\n", "Score\t 0.43141762602042416\n", "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n" ] } ] } ] }