File size: 12,936 Bytes
3270681 3881a7b 3270681 3881a7b e4e186c 3881a7b 3270681 3881a7b 3270681 3881a7b 567c34a 3881a7b 3270681 3881a7b e4e186c 3881a7b c45e65e 3881a7b c45e65e 3881a7b e4e186c 3270681 3881a7b 3270681 3881a7b e4e186c 3881a7b 3270681 3881a7b c45e65e 3881a7b e4e186c 3881a7b c45e65e 3881a7b c45e65e 3881a7b c45e65e 3881a7b e4e186c 3881a7b e4e186c 3881a7b e4e186c 3881a7b e4e186c 3881a7b e4e186c 3881a7b c45e65e 3881a7b e4e186c 3881a7b c45e65e e4e186c c45e65e 3881a7b c45e65e 3881a7b 3270681 3881a7b |
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 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyO9EXKHngvJa9fUydE3Tlen",
"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": [
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/03-RAG_with_LlamaIndex.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Install Packages and Setup Variables"
],
"metadata": {
"id": "v9bpz99INAc1"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BeuFJKlj9jKz",
"outputId": "a14a78f4-e43e-4aef-bc69-4ced559df34e"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m226.7/226.7 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m52.3/52.3 kB\u001b[0m \u001b[31m3.8 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[31m9.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m15.4/15.4 MB\u001b[0m \u001b[31m26.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m21.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m4.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m3.1/3.1 MB\u001b[0m \u001b[31m35.5 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[31m3.9 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[31m2.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m136.0/136.0 kB\u001b[0m \u001b[31m10.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m3.9/3.9 MB\u001b[0m \u001b[31m27.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m290.4/290.4 kB\u001b[0m \u001b[31m22.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m30.8/30.8 MB\u001b[0m \u001b[31m32.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m49.4/49.4 kB\u001b[0m \u001b[31m2.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install -q llama-index==0.10.30 openai==1.12.0 cohere==4.47 tiktoken==0.6.0"
]
},
{
"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\"] = \"<YOUR_OPENAI_KEY>\""
],
"metadata": {
"id": "XuzgSNqcABpV"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Load Dataset"
],
"metadata": {
"id": "f5eV5EnvNCMM"
}
},
{
"cell_type": "markdown",
"source": [
"## Download"
],
"metadata": {
"id": "q-7mRQ-mNJlm"
}
},
{
"cell_type": "markdown",
"source": [
"The dataset includes several articles from the TowardsAI blog, which provide an in-depth explanation of the LLaMA2 model."
],
"metadata": {
"id": "3PsdOdMUNmEi"
}
},
{
"cell_type": "code",
"source": [
"!wget https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3ImRCP7pACaI",
"outputId": "c782f06a-5fcb-4134-e197-e2a9c3193ce9"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"--2024-04-09 18:54:34-- https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 173646 (170K) [text/plain]\n",
"Saving to: βmini-llama-articles.csvβ\n",
"\n",
"mini-llama-articles 100%[===================>] 169.58K --.-KB/s in 0.09s \n",
"\n",
"2024-04-09 18:54:35 (1.89 MB/s) - βmini-llama-articles.csvβ saved [173646/173646]\n",
"\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Read File"
],
"metadata": {
"id": "bZZLK_wyEc-L"
}
},
{
"cell_type": "code",
"source": [
"import csv\n",
"\n",
"rows = []\n",
"\n",
"# Load the CSV file\n",
"with open(\"./mini-llama-articles.csv\", mode=\"r\", encoding=\"utf-8\") as file:\n",
" csv_reader = csv.reader(file)\n",
"\n",
" for idx, row in enumerate( csv_reader ):\n",
" if idx == 0: continue; # Skip header row\n",
" rows.append( row )\n",
"\n",
"# The number of characters in the dataset.\n",
"print( \"number of articles:\", len( rows ) )"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "miUqycqAEfr7",
"outputId": "911985c6-6884-48ff-fa24-869d44a1a012"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"number of articles: 14\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Generate Embedding"
],
"metadata": {
"id": "f86yksB9K571"
}
},
{
"cell_type": "code",
"source": [
"from llama_index.core import Document\n",
"\n",
"# Convert the texts to Document objects so the LlamaIndex framework can process them.\n",
"documents = [Document(text=row[1]) for row in rows]"
],
"metadata": {
"id": "iXrr5-tnEfm9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from llama_index.core import VectorStoreIndex\n",
"from llama_index.core.node_parser import SentenceSplitter\n",
"\n",
"# Build index / generate embeddings using OpenAI.\n",
"index = VectorStoreIndex.from_documents(\n",
" documents,\n",
" transformations=[SentenceSplitter(chunk_size=768, chunk_overlap=64)],\n",
")"
],
"metadata": {
"id": "Bsa7Q-DoNWBk"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Save the generated embeddings.\n",
"# index.storage_context.persist(persist_dir=\"indexes\")"
],
"metadata": {
"id": "xxB0A9ZYM-OD"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Query Dataset"
],
"metadata": {
"id": "3DoUxd8KK--Q"
}
},
{
"cell_type": "code",
"source": [
"# Define a query engine that is responsible for retrieving related pieces of text,\n",
"# and using a LLM to formulate the final answer.\n",
"query_engine = index.as_query_engine()"
],
"metadata": {
"id": "bUaNH97dEfh9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"response = query_engine.query(\n",
" \"How many parameters LLaMA2 model has?\"\n",
")\n",
"print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KHK4V_GRR6ZG",
"outputId": "8d656836-622a-4261-e24a-9cadf857b376"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The Llama 2 model has 7 billion, 13 billion, 34 billion, and 70 billion parameters.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"response = query_engine.query(\n",
" \"When will Llama3 will be released?\"\n",
")\n",
"print(response)"
],
"metadata": {
"id": "S-BmyTBbNd9y",
"outputId": "c6a4ec79-7555-4b4d-f212-0b5864c7bded",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The release date for Llama3 is not provided in the given context information.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Test with smaller chunk size\n",
"# transformations=[SentenceSplitter(chunk_size=512, chunk_overlap=20)]\n",
"\n",
"response = query_engine.query(\n",
" \"How many parameters LLaMA2 model has?\"\n",
")\n",
"print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tEgFx_aeFS5e",
"outputId": "0353f9e4-0f63-4739-eb5b-717bf19572ef"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The LLaMA2 model has a range of 7 billion to 65 billion parameters.\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "oZt_sG86RwZ3"
},
"execution_count": null,
"outputs": []
}
]
} |