16.langchain 入门到放弃(七)Chains-retrievalQA_chain

452 阅读2分钟

16.langchain 入门到放弃(七)Chains-retrievalQA_chain

  retrievalQA顾名思义,就是生成检索QA链。该链首先执行检索步骤以获取相关文档,然后将这些文档传递到 LLM 以生成响应。

执行步骤
1、首先检索步骤以获取相关文档【示例中:先到向量数据库中查找相似度高的文档】
2、然后根据相似度高的文档,生成prompt,然后调用llm生成回答

image

  源文件bluetooth.txt

无线蓝牙耳机,规格:单个耳机尺寸:1.5'' x 1.3''。为什么我们热爱它:这款无线蓝物美价廉
瑜伽垫,规格:尺寸:24'' x 68''。为什么我们热爱它:我们的瑜伽垫拥有出色的...
书本的大小,规格:寸:2.6'' x 2.3''。为什么我们热爱它:书中自有黄金屋
键盘,规格:尺寸:50 X 30。为什么我们需要它:能够让我们快速打字.

  示例代码

from langchain.chains.retrieval_qa.base import RetrievalQA
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.llms.ollama import Ollama
from langchain_community.vectorstores import FAISS

textLoad = TextLoader('../source/bluetooth.txt').load()

textSplit = RecursiveCharacterTextSplitter(chunk_size=50, chunk_overlap=0)

docs = textSplit.split_documents(textLoad)

# 初始化 HuggingFace 的 embeddings 对象
embeddings = HuggingFaceEmbeddings(model_name="../localLLM/all-MiniLM-L6-v2",
                                   model_kwargs={'device': 'cpu'})

# 存储到FAISS向量数据库 存储在内存中
embeddingDB = FAISS.from_documents(docs, embeddings)

model = Ollama(base_url="http://localhost:11434", model="llama3")

'''
1、首先先到向量数据库中查找相似度高的文档,
2、然后根据相似度高的文档,生成prompt,然后调用llm生成回答
'''
qa = RetrievalQA.from_chain_type(llm=model, chain_type="stuff", retriever=embeddingDB.as_retriever(),
                                 return_source_documents=True)

query = "我们为什么喜欢书?"
result = qa({"query": query})
print(result)

  执行结果为

{'query': '我们为什么喜欢书?', 'result': 'According to the context, we love books because "书中自有黄金屋" (There are golden rooms within the book).', 'source_documents': [Document(page_content='键盘,规格:尺寸:50 X 30。为什么我们需要它:能够让我们快速打字.', metadata={'source': '../source/bluetooth.txt'}), Document(page_content="瑜伽垫,规格:尺寸:24'' x 68''。为什么我们热爱它:我们的瑜伽垫拥有出色的...", metadata={'source': '../source/bluetooth.txt'}), Document(page_content="无线蓝牙耳机,规格:单个耳机尺寸:1.5'' x 1.3''。为什么我们热爱它:这款无线蓝物美价廉", metadata={'source': '../source/bluetooth.txt'}), Document(page_content="书本的大小,规格:寸:2.6'' x 2.3''。为什么我们热爱它:书中自有黄金屋", metadata={'source': '../source/bluetooth.txt'})]}

Process finished with exit code 0