File size: 10,414 Bytes
3db4bdd | 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 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# RAG with LlamaIndex"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"LlamaIndex is a very popular framework to help build connections between data sources and LLMs. It is also a top choice when people would like to build an RAG framework. In this tutorial, we will go through how to use LlamaIndex to aggregate bge-base-en-v1.5 and GPT-4o-mini to an RAG application."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 0. Preparation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First install the required packages in the environment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-llms-openai llama-index-embeddings-huggingface llama-index-vector-stores-faiss\n",
"%pip install llama_index "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then fill the OpenAI API key below:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# For openai key\n",
"import os\n",
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"BGE-M3 is a very powerful embedding model, We would like to know what does that 'M3' stands for.\n",
"\n",
"Let's first ask GPT the question:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"M3-Embedding stands for Multimodal Multiscale Embedding. It is a technique used in machine learning and data analysis to embed high-dimensional data into a lower-dimensional space while preserving the structure and relationships within the data. This technique is particularly useful for analyzing complex datasets that contain multiple modalities or scales of information.\n"
]
}
],
"source": [
"from llama_index.llms.openai import OpenAI\n",
"\n",
"# non-streaming\n",
"response = OpenAI().complete(\"What does M3-Embedding stands for?\")\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By checking the description in GitHub [repo](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/BGE_M3) of BGE-M3, we are pretty sure that GPT is giving us hallucination. Let's build an RAG pipeline to solve the problem!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, download BGE-M3 [paper](https://arxiv.org/pdf/2402.03216) to a directory, and load it through `SimpleDirectoryReader`. \n",
"\n",
"Note that `SimpleDirectoryReader` can read all the documents under that directory and supports a lot of commonly used [file types](https://docs.llamaindex.ai/en/stable/module_guides/loading/simpledirectoryreader/#supported-file-types)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import SimpleDirectoryReader\n",
"\n",
"reader = SimpleDirectoryReader(\"data\")\n",
"# reader = SimpleDirectoryReader(\"DIR_TO_FILE\")\n",
"documents = reader.load_data()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `Settings` object is a global settings for the RAG pipeline. Attributes in it have default settings and can be modified by users (OpenAI's GPT and embedding model). Large attributes like models will be only loaded when being used.\n",
"\n",
"Here, we specify the `node_parser` to `SentenceSplitter()` with our chosen parameters, use the open-source `bge-base-en-v1.5` as our embedding model, and `gpt-4o-mini` as our llm."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import Settings\n",
"from llama_index.core.node_parser import SentenceSplitter\n",
"from llama_index.embeddings.huggingface import HuggingFaceEmbedding\n",
"from llama_index.llms.openai import OpenAI\n",
"\n",
"# set the parser with parameters\n",
"Settings.node_parser = SentenceSplitter(\n",
" chunk_size=1000, # Maximum size of chunks to return\n",
" chunk_overlap=150, # number of overlap characters between chunks\n",
")\n",
"\n",
"# set the specific embedding model\n",
"Settings.embed_model = HuggingFaceEmbedding(model_name=\"BAAI/bge-base-en-v1.5\")\n",
"\n",
"# set the llm we want to use\n",
"Settings.llm = OpenAI(model=\"gpt-4o-mini\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Indexing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Indexing is one of the most important part in RAG. LlamaIndex integrates a great amount of vector databases. Here we will use Faiss as an example."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First check the dimension of the embeddings, which will need for initializing a Faiss index."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"768\n"
]
}
],
"source": [
"embedding = Settings.embed_model.get_text_embedding(\"Hello world\")\n",
"dim = len(embedding)\n",
"print(dim)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then create the index with Faiss and our documents. Here LlamaIndex help capsulate the Faiss function calls. If you would like to know more about Faiss, refer to the tutorial of [Faiss and indexing](https://github.com/FlagOpen/FlagEmbedding/tree/master/Tutorials/3_Indexing)."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import faiss\n",
"from llama_index.vector_stores.faiss import FaissVectorStore\n",
"from llama_index.core import StorageContext, VectorStoreIndex\n",
"\n",
"# init Faiss and create a vector store\n",
"faiss_index = faiss.IndexFlatL2(dim)\n",
"vector_store = FaissVectorStore(faiss_index=faiss_index)\n",
"\n",
"# customize the storage context using our vector store\n",
"storage_context = StorageContext.from_defaults(\n",
" vector_store=vector_store\n",
")\n",
"\n",
"# use the loaded documents to build the index\n",
"index = VectorStoreIndex.from_documents(\n",
" documents, storage_context=storage_context\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Retrieve and Generate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With a well constructed index, we can now build the query engine to accomplish our task:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"query_engine = index.as_query_engine()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following cell displays the default prompt template for Q&A in our pipeline:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Context information is below.\n",
"---------------------\n",
"{context_str}\n",
"---------------------\n",
"Given the context information and not prior knowledge, answer the query.\n",
"Query: {query_str}\n",
"Answer: \n"
]
}
],
"source": [
"# check the default promt template\n",
"prompt_template = query_engine.get_prompts()['response_synthesizer:text_qa_template']\n",
"print(prompt_template.get_template())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(Optional) You could modify the prompt to match your use cases:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"You are a Q&A chat bot.\n",
"Use the given context only, answer the question.\n",
"\n",
"<context>\n",
"{context_str}\n",
"</context>\n",
"\n",
"Question: {query_str}\n",
"\n"
]
}
],
"source": [
"from llama_index.core import PromptTemplate\n",
"\n",
"template = \"\"\"\n",
"You are a Q&A chat bot.\n",
"Use the given context only, answer the question.\n",
"\n",
"<context>\n",
"{context_str}\n",
"</context>\n",
"\n",
"Question: {query_str}\n",
"\"\"\"\n",
"\n",
"new_template = PromptTemplate(template)\n",
"query_engine.update_prompts(\n",
" {\"response_synthesizer:text_qa_template\": new_template}\n",
")\n",
"\n",
"prompt_template = query_engine.get_prompts()['response_synthesizer:text_qa_template']\n",
"print(prompt_template.get_template())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, let's see how does the RAG application performs on our query!"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"M3-Embedding stands for Multi-Linguality, Multi-Functionality, and Multi-Granularity.\n"
]
}
],
"source": [
"response = query_engine.query(\"What does M3-Embedding stands for?\")\n",
"print(response)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "test",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|