from pymongo import MongoClient
from bson import ObjectId
from langchain.vectorstores import MongoDBVectorStore
from langchain.llms import LLM
from langchain.prompts import LLMPrompt
from langchain.vectorstores import VectorStore
from langchain.chains import RetrievalQA
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import DPRVectorStore
# 连接到MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["rag"]
# 初始化MongoDBVectorStore
vector_store = MongoDBVectorStore(db=db, collection_name="vectors")
# 初始化LLM
llm = LLM()
# 创建LLMPrompt实例
prompt = LLMPrompt(
input_variables=["query"],
template="The answer to the query is: {}",
)
# 创建DPRVectorStore实例
dpr_vector_store = DPRVectorStore.from_prebuilt("text-encoder-distilbert-base-nli-stsb-mean-tokens")
# 创建RetrievalQA实例
qa_chain = RetrievalQA(
llm=llm,
vector_store=vector_store,
dpr_vector_store=dpr_vector_store,
prompt=prompt,
retriever_type="vector",
search_kwargs={"size": 5},
input_text_splitter=CharacterTextSplitter(),
)
# 查询函数
def query(query: str):
result = qa_chain.run(query=query)
return result
# 示例查询
query_result = query("What is the capital of France?")
print(query_result)
这段代码展示了如何使用MongoDB作为矢量数据库,并结合LLM、DPR向量存储和RetrievalQA链来实现一个简单的RAG系统的查询功能。代码中创建了连接到MongoDB的客户端、初始化了MongoDBVectorStore和LLM,并定义了一个查询函数,该函数使用RetrievalQA链来处理用户查询并返回结果。