17.langchain 入门到放弃(七)Chains-retrievalQAWithPrompt_chain
retrievalQA顾名思义,就是生成检索QA链。该链首先执行检索步骤以获取相关文档,然后将这些文档传递到 LLM 以生成响应。
执行步骤
1、首先检索步骤以获取相关文档【示例中:先到向量数据库中查找相似度高的文档】
2、然后根据相似度高的文档,生成prompt,然后调用llm生成回答
源文件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
from langchain_core.prompts import PromptTemplate
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")
template = """Use the following pieces of context to answer the question at the end. If you don't know the answer,
just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible.
Always say "thanks for asking!" at the end of the answer.
{context}
Question: {question}
Helpful Answer:"""
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
'''
1、首先先到向量数据库中查找相似度高的文档,
2、然后根据相似度高的文档,生成prompt,然后调用llm生成回答
'''
qa = RetrievalQA.from_chain_type(llm=model, chain_type="stuff", retriever=embeddingDB.as_retriever(),
chain_type_kwargs={"prompt":QA_CHAIN_PROMPT},
return_source_documents=True)
query = "我们为什么喜欢鼠标?"
result = qa({"query": query})
print(result)
执行结果为
{'query': '我们为什么喜欢鼠标?', 'result': "I don't know the answer to this question because there is no mention of a mouse in the provided context. Thanks for asking!", '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