File size: 7,867 Bytes
4b2e474 dda976b 08b8fbf dda976b 08b8fbf dda976b 08b8fbf dda976b 4b2e474 dda976b 4b2e474 dda976b 08b8fbf dda976b 4b2e474 dda976b 08b8fbf dda976b 08b8fbf dda976b fcc14cf dda976b 08b8fbf dda976b 08b8fbf dda976b fcc14cf dda976b 4b2e474 dda976b 4b2e474 dda976b 08b8fbf dda976b 08b8fbf dda976b 4b2e474 dda976b 4b2e474 dda976b 4b2e474 dda976b 08b8fbf dda976b 08b8fbf dda976b 08b8fbf dda976b 08b8fbf dda976b 08b8fbf dda976b 08b8fbf dda976b a393007 dda976b a393007 dda976b 08b8fbf dda976b 4b2e474 dda976b 4b2e474 dda976b 08b8fbf dda976b 4b2e474 dda976b 08b8fbf dda976b a393007 dda976b |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"outputs": [],
"source": [
"<a href=\"https://colab.research.google.com/github/towardsai/ai-tutor-rag-system/blob/main/notebooks/04-RAG_with_VectorStore.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5BGJ3fxhOk2V"
},
"source": [
"# Install Packages and Setup Variables"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QPJzr-I9XQ7l",
"outputId": "9949a0e5-8bf2-4ae7-9921-1f9dfbece9ae"
},
"outputs": [],
"source": [
"!pip install -q llama-index==0.10.5 openai==1.12.0 cohere==4.47 tiktoken==0.6.0 chromadb==0.4.22"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"id": "riuXwpSPcvWC"
},
"outputs": [],
"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>\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "I9JbAzFcjkpn"
},
"source": [
"# Load the Dataset (CSV)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_Tif8-JoRH68"
},
"source": [
"## Download"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4fQaa1LN1mXL"
},
"source": [
"The dataset includes several articles from the TowardsAI blog, which provide an in-depth explanation of the LLaMA2 model. Read the dataset as a long string."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"id": "-QTUkdfJjY4N"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
" Dload Upload Total Spent Left Speed\n",
"100 169k 100 169k 0 0 743k 0 --:--:-- --:--:-- --:--:-- 747k\n"
]
}
],
"source": [
"!curl -o ./mini-dataset.csv https://raw.githubusercontent.com/AlaFalaki/tutorial_notebooks/main/data/mini-llama-articles.csv"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zk-4alIxROo8"
},
"source": [
"## Read File"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7CYwRT6R0o0I",
"outputId": "6f0f05ae-c92f-45b2-bbc3-d12add118021"
},
"outputs": [
{
"data": {
"text/plain": [
"841"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import csv\n",
"\n",
"text = \"\"\n",
"\n",
"# Load the file as a JSON\n",
"with open(\"./mini-dataset.csv\", mode=\"r\", encoding=\"ISO-8859-1\") as file:\n",
" csv_reader = csv.reader(file)\n",
"\n",
" for row in csv_reader:\n",
" text += row[0]\n",
"\n",
"# The number of characters in the dataset.\n",
"len( text )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "S17g2RYOjmf2"
},
"source": [
"# Chunking"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "STACTMUR1z9N",
"outputId": "8ce58d6b-a38d-48e3-8316-7435907488cf"
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chunk_size = 512\n",
"chunks = []\n",
"\n",
"# Split the long text into smaller manageable chunks of 512 characters.\n",
"for i in range(0, len(text), chunk_size):\n",
" chunks.append(text[i:i + chunk_size])\n",
"\n",
"len( chunks )"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"id": "CtdsIUQ81_hT"
},
"outputs": [],
"source": [
"from llama_index.core import Document\n",
"\n",
"# Convert the chunks to Document objects so the LlamaIndex framework can process them.\n",
"documents = [Document(text=t) for t in chunks]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OWaT6rL7ksp8"
},
"source": [
"# Save on Chroma"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"id": "mXi56KTXk2sp"
},
"outputs": [],
"source": [
"import chromadb\n",
"\n",
"# create client and a new collection\n",
"# chromadb.EphemeralClient saves data in-memory.\n",
"chroma_client = chromadb.PersistentClient(path=\"./mini-chunked-dataset\")\n",
"chroma_collection = chroma_client.create_collection(\"mini-chunked-dataset\")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"id": "jKXURvLtkuTS"
},
"outputs": [],
"source": [
"from llama_index.vector_stores.chroma import ChromaVectorStore\n",
"from llama_index.core import StorageContext\n",
"\n",
"# Define a storage context object using the created vector database.\n",
"vector_store = ChromaVectorStore(chroma_collection=chroma_collection)\n",
"storage_context = StorageContext.from_defaults(vector_store=vector_store)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"id": "WsD52wtrlESi"
},
"outputs": [],
"source": [
"from llama_index.core import VectorStoreIndex\n",
"\n",
"# Add the documents to the database and create Index / embeddings\n",
"index = VectorStoreIndex.from_documents(\n",
" documents, storage_context=storage_context\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8JPD8yAinVSq"
},
"source": [
"# Query Dataset"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"id": "mzS13x1ZlZ5X"
},
"outputs": [],
"source": [
"from llama_index.llms.openai import OpenAI\n",
"# Define a query engine that is responsible for retrieving related pieces of text,\n",
"# and using a LLM to formulate the final answer.\n",
"\n",
"llm = OpenAI(temperature=0, model=\"gpt-3.5-turbo-0125\", max_tokens=512)\n",
"query_engine = index.as_query_engine(llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "AYsQ4uLN_Oxg",
"outputId": "bf2181ad-27f6-40a2-b792-8a2714a60c29"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The LLaMA2 model has 7 billion parameters.\n"
]
}
],
"source": [
"response = query_engine.query(\n",
" \"How many parameters LLaMA2 model has?\"\n",
")\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"authorship_tag": "ABX9TyNQkVEh0x7hcM9U+6JSEkSG",
"include_colab_link": true,
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|