chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:39:21 +08:00
commit bc34f6df14
1149 changed files with 328099 additions and 0 deletions
+327
View File
@@ -0,0 +1,327 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simple RAG From Scratch"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this tutorial, we will use BGE, Faiss, and OpenAI's GPT-4o-mini to build a simple RAG system from scratch."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 0. Preparation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Install the required packages in the environment:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -U numpy faiss-cpu FlagEmbedding openai"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose I'm a resident of New York Manhattan, and I want the AI bot to provide suggestion on where should I go for dinner. It's not reliable to let it recommend some random restaurant. So let's provide a bunch of our favorate restaurants."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"corpus = [\n",
" \"Cheli: A downtown Chinese restaurant presents a distinctive dining experience with authentic and sophisticated flavors of Shanghai cuisine. Avg cost: $40-50\",\n",
" \"Masa: Midtown Japanese restaurant with exquisite sushi and omakase experiences crafted by renowned chef Masayoshi Takayama. The restaurant offers a luxurious dining atmosphere with a focus on the freshest ingredients and exceptional culinary artistry. Avg cost: $500-600\",\n",
" \"Per Se: A midtown restaurant features daily nine-course tasting menu and a nine-course vegetable tasting menu using classic French technique and the finest quality ingredients available. Avg cost: $300-400\",\n",
" \"Ortomare: A casual, earthy Italian restaurant locates uptown, offering wood-fired pizza, delicious pasta, wine & spirits & outdoor seating. Avg cost: $30-50\",\n",
" \"Banh: Relaxed, narrow restaurant in uptown, offering Vietnamese cuisine & sandwiches, famous for its pho and Vietnam sandwich. Avg cost: $20-30\",\n",
" \"Living Thai: An uptown typical Thai cuisine with different kinds of curry, Tom Yum, fried rice, Thai ice tea, etc. Avg cost: $20-30\",\n",
" \"Chick-fil-A: A Fast food restaurant with great chicken sandwich, fried chicken, fries, and salad, which can be found everywhere in New York. Avg cost: 10-20\",\n",
" \"Joe's Pizza: Most famous New York pizza locates midtown, serving different flavors including classic pepperoni, cheese, spinach, and also innovative pizza. Avg cost: $15-25\",\n",
" \"Red Lobster: In midtown, Red Lobster is a lively chain restaurant serving American seafood standards amid New England-themed decor, with fair price lobsters, shrips and crabs. Avg cost: $30-50\",\n",
" \"Bourbon Steak: It accomplishes all the traditions expected from a steakhouse, offering the finest cuts of premium beef and seafood complimented by wine and spirits program. Avg cost: $100-150\",\n",
" \"Da Long Yi: Locates in downtown, Da Long Yi is a Chinese Szechuan spicy hotpot restaurant that serves good quality meats. Avg cost: $30-50\",\n",
" \"Mitr Thai: An exquisite midtown Thai restaurant with traditional dishes as well as creative dishes, with a wonderful bar serving cocktails. Avg cost: $40-60\",\n",
" \"Yichiran Ramen: Famous Japenese ramen restaurant in both midtown and downtown, serving ramen that can be designed by customers themselves. Avg cost: $20-40\",\n",
" \"BCD Tofu House: Located in midtown, it's famous for its comforting and flavorful soondubu jjigae (soft tofu stew) and a variety of authentic Korean dishes. Avg cost: $30-50\",\n",
"]\n",
"\n",
"user_input = \"I want some Chinese food\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Indexing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we need to figure out a fast but powerful enough method to retrieve docs in the corpus that are most closely related to our questions. Indexing is a good choice for us.\n",
"\n",
"The first step is embed each document into a vector. We use bge-base-en-v1.5 as our embedding model."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"from FlagEmbedding import FlagModel\n",
"\n",
"model = FlagModel('BAAI/bge-base-en-v1.5',\n",
" query_instruction_for_retrieval=\"Represent this sentence for searching relevant passages:\",\n",
" use_fp16=True)\n",
"\n",
"embeddings = model.encode(corpus, convert_to_numpy=True)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(14, 768)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"embeddings.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, let's create a Faiss index and add all the vectors into it.\n",
"\n",
"If you want 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": 14,
"metadata": {},
"outputs": [],
"source": [
"import faiss\n",
"import numpy as np\n",
"\n",
"index = faiss.IndexFlatIP(embeddings.shape[1])\n",
"\n",
"index.add(embeddings)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"index.ntotal"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Retrieve and Generate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we come to the most exciting part. Let's first embed our query and retrieve 3 most relevant document from it:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([['Cheli: A downtown Chinese restaurant presents a distinctive dining experience with authentic and sophisticated flavors of Shanghai cuisine. Avg cost: $40-50',\n",
" 'Da Long Yi: Locates in downtown, Da Long Yi is a Chinese Szechuan spicy hotpot restaurant that serves good quality meats. Avg cost: $30-50',\n",
" 'Yichiran Ramen: Famous Japenese ramen restaurant in both midtown and downtown, serving ramen that can be designed by customers themselves. Avg cost: $20-40']],\n",
" dtype='<U270')"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"q_embedding = model.encode_queries([user_input], convert_to_numpy=True)\n",
"\n",
"D, I = index.search(q_embedding, 3)\n",
"res = np.array(corpus)[I]\n",
"\n",
"res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then set up the prompt for the chatbot:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"prompt=\"\"\"\n",
"You are a bot that makes recommendations for restaurants. \n",
"Please be brief, answer in short sentences without extra information.\n",
"\n",
"These are the restaurants list:\n",
"{recommended_activities}\n",
"\n",
"The user's preference is: {user_input}\n",
"Provide the user with 2 recommended restaurants based on the user's preference.\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fill in your OpenAI API key below:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally let's see how the chatbot give us the answer!"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"client = OpenAI()\n",
"\n",
"response = client.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": prompt.format(user_input=user_input, recommended_activities=res)\n",
" }\n",
" ]\n",
").choices[0].message"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1. Cheli - Authentic Shanghai cuisine with sophisticated flavors. \n",
"2. Da Long Yi - Szechuan spicy hotpot with good quality meats.\n"
]
}
],
"source": [
"print(response.content)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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
}
+336
View File
@@ -0,0 +1,336 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# RAG with LangChain"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"LangChain is well adopted by open-source community because of its diverse functionality and clean API usage. In this tutorial we will show how to use LangChain to build an RAG pipeline."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 0. Preparation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, install all the required packages:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install pypdf langchain langchain-openai langchain-huggingface"
]
},
{
"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": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"M3-Embedding typically refers to a specific method or framework used in machine learning and natural language processing for creating embeddings, which are dense vector representations of data. The \"M3\" could indicate a particular model, method, or version related to embeddings, but without additional context, it's hard to provide a precise definition.\n",
"\n",
"If you have a specific context or source in mind where \"M3-Embedding\" is used, please provide more details, and I may be able to give a more accurate explanation!\n"
]
}
],
"source": [
"from langchain_openai.chat_models import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(model_name=\"gpt-4o-mini\")\n",
"\n",
"response = llm.invoke(\"What does M3-Embedding stands for?\")\n",
"print(response.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By quickly checking the GitHub [repo](https://github.com/FlagOpen/FlagEmbedding/tree/master/FlagEmbedding/BGE_M3) of BGE-M3. Since BGE-M3 paper is not in its training dataset, GPT is not capable to give us correct answer.\n",
"\n",
"Now, let's use the [paper](https://arxiv.org/pdf/2402.03216) of BGE-M3 to build an RAG application to answer our question precisely."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first step is to load the pdf of our paper:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.document_loaders import PyPDFLoader\n",
"\n",
"# Or download the paper and put a path to the local file instead\n",
"loader = PyPDFLoader(\"https://arxiv.org/pdf/2402.03216\")\n",
"docs = loader.load()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'source': 'https://arxiv.org/pdf/2402.03216', 'page': 0}\n"
]
}
],
"source": [
"print(docs[0].metadata)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The whole paper contains 18 pages. That's a huge amount of information. Thus we split the paper into chunks to construct a corpus."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
"\n",
"# initialize a splitter\n",
"splitter = RecursiveCharacterTextSplitter(\n",
" chunk_size=1000, # Maximum size of chunks to return\n",
" chunk_overlap=150, # number of overlap characters between chunks\n",
")\n",
"\n",
"# use the splitter to split our paper\n",
"corpus = splitter.split_documents(docs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Indexing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Indexing is one of the most important part in RAG. LangChain provides APIs for embedding models and vector databases that make things simple and straightforward.\n",
"\n",
"Here, we choose bge-base-en-v1.5 to embed all the chunks to vectors, and use Faiss as our vector database."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"from langchain_huggingface.embeddings import HuggingFaceEmbeddings\n",
"\n",
"embedding_model = HuggingFaceEmbeddings(model_name=\"BAAI/bge-base-en-v1.5\", \n",
"encode_kwargs={\"normalize_embeddings\": True})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then create a Faiss vector database given our corpus and embedding model. \n",
"\n",
"If you want 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": 15,
"metadata": {},
"outputs": [],
"source": [
"from langchain.vectorstores import FAISS\n",
"\n",
"vectordb = FAISS.from_documents(corpus, embedding_model)\n",
"\n",
"# (optional) save the vector database to a local directory\n",
"vectordb.save_local(\"vectorstore.db\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# Create retriever for later use\n",
"retriever = vectordb.as_retriever()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Retreive and Generate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's write a simple prompt template. Modify the contents to match your different use cases."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\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}\n",
"</context>\n",
"\n",
"Question: {input}\n",
"\"\"\"\n",
"\n",
"# Create a prompt template\n",
"prompt = ChatPromptTemplate.from_template(template)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now everything is ready. Assemble them to a chain and let the magic happen!"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains.combine_documents import create_stuff_documents_chain\n",
"from langchain.chains import create_retrieval_chain\n",
"\n",
"doc_chain = create_stuff_documents_chain(llm, prompt)\n",
"chain = create_retrieval_chain(retriever, doc_chain)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run the following cell, we can see that the chatbot can answer the question correctly!"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"M3-Embedding stands for a new embedding model that is distinguished for its versatility in multi-linguality, multi-functionality, and multi-granularity.\n"
]
}
],
"source": [
"response = chain.invoke({\"input\": \"What does M3-Embedding stands for?\"})\n",
"\n",
"# print the answer only\n",
"print(response['answer'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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
}
+384
View File
@@ -0,0 +1,384 @@
{
"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
}