提升响应:LlamaIndex 和 MongoDB 助力 RAG

引言
检索增强生成(RAG)系统通过增强大型语言模型(LLM)提供上下文相关响应的能力,彻底改变了我们与 LLM 交互的方式。这些系统将 LLM 连接到数据库,使其能够检索语义相关信息以增强其响应。在本全面指南中,我们将探讨如何使用 LlamaIndex 和 MongoDB 构建自己的 RAG 系统,从而使您能够开发动态且上下文感知的应用程序。
定义
检索增强生成(RAG):一种系统设计模式,它将信息检索技术与生成式人工智能模型集成,通过从外部数据源检索附加上下文来补充用户查询的响应,从而提高响应的相关性和准确性。
LLamaIndex:一个 LLM/数据框架,旨在促进数据源与专有和开源 LLM 之间的连接,抽象了数据摄取和 RAG 管道实现相关的复杂性。
集成优势
将 LlamaIndex 与 MongoDB 集成提供多项优势:
高效数据检索:MongoDB 可作为操作型和向量数据库,高效存储和检索 RAG 系统所需的向量嵌入和操作数据。
可扩展性:LlamaIndex 抽象了数据摄取和 RAG 管道实现相关的复杂性,使开发人员能够构建可快速适应各种领域的可扩展应用程序。
上下文相关性:通过利用 MongoDB 的索引功能和 LlamaIndex 的检索模型,开发人员可以确保 LLM 响应具有上下文相关性和准确性。
代码实现
让我们深入了解实际的实现步骤
步骤一:安装库
!pip install llama-index
!pip install llama-index-vector-stores-mongodb
!pip install llama-index-embeddings-openai
!pip install pymongo
!pip install datasets
!pip install pandas
步骤 2:OPENAI 密钥设置
import os
os.environ["OPENAI_API_KEY"] = ""
步骤 3:数据加载和处理
from datasets import load_dataset
import pandas as pd
# https://huggingface.co/datasets/MongoDB/embedded_movies
# Make sure you have an Hugging Face token(HF_TOKEN) in your development environemnt
dataset = load_dataset("MongoDB/airbnb_embeddings")
# Convert the dataset to a pandas dataframe
dataset_df = pd.DataFrame(dataset['train'])
## Processing
dataset_df = dataset_df.drop(columns=['text_embeddings'])
dataset_df.head(5)
步骤 4:LLAMAINDEX LLM 配置
from llama_index.core.settings import Settings
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
embed_model = OpenAIEmbedding(model="text-embedding-3-small", dimensions=256)
llm = OpenAI()
Settings.llm = llm
Settings.embed_model = embed_model
步骤 5:创建 LLAMAINDEX 自定义文档和节点
import json
from llama_index.core import Document
from llama_index.core.schema import MetadataMode
# Convert the DataFrame to a JSON string representation
documents_json = dataset_df.to_json(orient='records')
# Load the JSON string into a Python list of dictionaries
documents_list = json.loads(documents_json)
llama_documents = []
for document in documents_list:
# Value for metadata must be one of (str, int, float, None)
document["amenities"] = json.dumps(document["amenities"])
document["images"] = json.dumps(document["images"])
document["host"] = json.dumps(document["host"])
document["address"] = json.dumps(document["address"])
document["availability"] = json.dumps(document["availability"])
document["review_scores"] = json.dumps(document["review_scores"])
document["reviews"] = json.dumps(document["reviews"])
document["image_embeddings"] = json.dumps(document["image_embeddings"])
# Create a Document object with the text and excluded metadata for llm and embedding models
llama_document = Document(
text=document["description"],
metadata=document,
excluded_llm_metadata_keys=["_id", "transit", "minimum_nights", "maximum_nights", "cancellation_policy", "last_scraped", "calendar_last_scraped", "first_review", "last_review", "security_deposit", "cleaning_fee", "guests_included", "host", "availability", "reviews", "image_embeddings"],
excluded_embed_metadata_keys=["_id", "transit", "minimum_nights", "maximum_nights", "cancellation_policy", "last_scraped", "calendar_last_scraped", "first_review", "last_review", "security_deposit", "cleaning_fee", "guests_included", "host", "availability", "reviews", "image_embeddings"],
metadata_template="{key}=>{value}",
text_template="Metadata: {metadata_str}\n-----\nContent: {content}",
)
llama_documents.append(llama_document)
# Observing an example of what the LLM and Embedding model receive as input
print(
"\nThe LLM sees this: \n",
llama_documents[0].get_content(metadata_mode=MetadataMode.LLM),
)
print(
"\nThe Embedding model sees this: \n",
llama_documents[0].get_content(metadata_mode=MetadataMode.EMBED),
)
步骤 6:创建节点
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.schema import MetadataMode
parser = SentenceSplitter(chunk_size=5000)
nodes = parser.get_nodes_from_documents(llama_documents)
for node in nodes:
node_embedding = embed_model.get_text_embedding(
node.get_content(metadata_mode=MetadataMode.EMBED)
)
node.embedding = node_embedding
步骤 7:MongoDB 向量数据库连接与设置
Creating a database and collection within MongoDB is made simple with MongoDB Atlas.
1. First, register for a MongoDB Atlas account. For existing users, sign into MongoDB Atlas.
2. Follow the instructions. Select Atlas UI as the procedure to deploy your first cluster.
3. Create the database: `airbnb`.
4. Within the database` airbnb`, create the collection ‘listings_reviews’.
5. Create a vector search index named vector_index for the ‘listings_reviews’ collection. This index enables the RAG application to retrieve records as additional context to supplement user queries via vector search. Below is the JSON definition of the data collection vector search index.
import pymongo
from google.colab import userdata
def get_mongo_client(mongo_uri):
"""Establish connection to the MongoDB."""
try:
client = pymongo.MongoClient(mongo_uri)
print("Connection to MongoDB successful")
return client
except pymongo.errors.ConnectionFailure as e:
print(f"Connection failed: {e}")
return None
mongo_uri = userdata.get('MONGO_URI_2')
if not mongo_uri:
print("MONGO_URI not set in environment variables")
mongo_client = get_mongo_client(mongo_uri)
DB_NAME="movies"
COLLECTION_NAME="movies_records"
db = mongo_client[DB_NAME]
collection = db[COLLECTION_NAME]
以下代码通过对集合执行 delete_many() 操作,确保当前数据库集合为空。
collection.delete_many({})
步骤 8:数据摄取
from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch
vector_store = MongoDBAtlasVectorSearch(mongo_client, db_name=DB_NAME, collection_name=COLLECTION_NAME, index_name="vector_index")
vector_store.add(nodes)
步骤 8:使用用户查询索引
from llama_index.core import VectorStoreIndex
import pprint
from llama_index.core.response.notebook_utils import display_response
index = VectorStoreIndex.from_vector_store(vector_store)
query_engine = index.as_query_engine(similarity_top_k=3)
query = "I want to stay in a place that's warm and friendly, and not too far from resturants, can you recommend a place? Include a reason as to why you've chosen your selection"
response = query_engine.query(query)
display_response(response)
pprint.pprint(response.source_nodes)
结论
遵循本指南中概述的步骤,您可以开发自己的 LlamaIndex 和 MongoDB RAG 系统,从而增强应用程序提供上下文相关和准确响应的能力。这种集成不仅简化了开发过程,还确保了处理大型数据集时的可扩展性和效率。拥抱 RAG 系统的强大功能,革新您的人工智能应用程序,并提供增强的用户体验。
“保持联系,并通过各种平台支持我的工作
Medium:您可以在 https://medium.com/@andysingal 阅读我的最新文章和见解
Paypal:喜欢我的文章吗?请我喝杯咖啡吧!https://paypal.me/alphasingal?country.x=US&locale.x=en_US"
请求和问题:如果您有想让我参与的项目,或者对我的概念有任何疑问,请随时告诉我。我一直在寻找未来笔记本的新想法,并且乐于帮助解决您可能遇到的任何疑问。
资源