带有 RetrievalQA Chain 的 Map_Reduce 提示 - Langchain

问题描述 投票:0回答:1

在下面的代码中,您可以看到我如何使用 Langchain with Memory 的 ParentDocumentRetriever 构建 RAG 模型。目前我正在使用带有默认 chain_type="stuff" 的 RetrievalQA-Chain。不过我想尝试不同的链类型,例如“map_reduce”。但是当替换 chain_type="map_reduce" 并创建检索 QA 链时,我收到以下错误:

ValidationError: 1 validation error for RefineDocumentsChain
prompt
  extra fields not permitted (type=value_error.extra)

我假设我的提示未正确构建,但我该如何更改它才能使其正常工作?我看到“map_reduce”需要两个不同的提示:“map_prompt”和“combine_prompt”。但我不确定如何更改典型 RAG 检索任务的提示,用户可以与模型交互并要求模型为他回答问题。这是我的代码:

from langchain.chains import RetrievalQA
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.callbacks.manager import CallbackManager
from langchain.document_loaders import PyPDFLoader, DirectoryLoader

loader = DirectoryLoader("MY_PATH_TO_PDF_FILES",
                         glob='*.pdf',
                         loader_cls=PyPDFLoader)
documents = loader.load()

# This text splitter is used to create the parent documents - The big chunks
parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=400)

# This text splitter is used to create the child documents - The small chunks
child_splitter = RecursiveCharacterTextSplitter(chunk_size=400)

# The vectorstore to use to index the child chunks
from chromadb.errors import InvalidDimensionException
try:
    vectorstore = Chroma(collection_name="split_parents", embedding_function=bge_embeddings, persist_directory="chroma_db")
except InvalidDimensionException:
    Chroma().delete_collection()
    vectorstore = Chroma(collection_name="split_parents", embedding_function=bge_embeddings, persist_directory="chroma_db")

# The storage layer for the parent documents
store = InMemoryStore()

big_chunks_retriever = ParentDocumentRetriever(
    vectorstore=vectorstore,
    docstore=store,
    child_splitter=child_splitter,
    parent_splitter=parent_splitter,
)

big_chunks_retriever.add_documents(documents)

qa_template = """
Use the following information from the context (separated with <ctx></ctx>) to answer the question.
Answer in German only, because the user does not understand English! \
If you don't know the answer, answer with "Unfortunately, I don't have the information." \
If you don't find enough information below, also answer with "Unfortunately, I don't have the information." \
------
<ctx>
{context}
</ctx>
------
<hs>
{chat_history}
</hs>
------
{query}
Answer:
"""

prompt = PromptTemplate(template=qa_template,
                            input_variables=['context','history', 'question'])

chain_type_kwargs={
        "verbose": True,
        "prompt": prompt,
        "memory": ConversationSummaryMemory(
            llm=build_llm(),
            memory_key="history",
            input_key="question",
            return_messages=True)}

refine = RetrievalQA.from_chain_type(llm=build_llm(),
                                 chain_type="map_reduce",
                                 return_source_documents=True,
                                 chain_type_kwargs=chain_type_kwargs,
                                 retriever=big_chunks_retriever,
                                 verbose=True)

query = "Hi, I am Max, can you help me??"
refine(query)
python information-retrieval langchain nlp-question-answering
1个回答
0
投票

你已经快找到答案了。 取决于你要做什么,让我们看下面的代码:

qa_template = """
Use the following information from the context (separated with <ctx></ctx>) to answer the question.
Answer in German only, because the user does not understand English! \
If you don't know the answer, answer with "Unfortunately, I don't have the information." \
If you don't find enough information below, also answer with "Unfortunately, I don't have the information." \
------
<ctx>
{context}
</ctx>
------
<hs>
{chat_history}
</hs>
------
{question}
Answer:
"""

prompt = PromptTemplate(template=qa_template,
                            input_variables=['context','history', 'question'])
combine_custom_prompt='''
Generate a summary of the following text that includes the following elements:

* A title that accurately reflects the content of the text.
* An introduction paragraph that provides an overview of the topic.
* Bullet points that list the key points of the text.
* A conclusion paragraph that summarizes the main points of the text.

Text:`{context}`
'''


combine_prompt_template = PromptTemplate(
    template=combine_custom_prompt, 
    input_variables=['context']
)

chain_type_kwargs={
        "verbose": True,
        "question_prompt": prompt,
        "combine_prompt": combine_prompt_template,
        "combine_document_variable_name": "context",
        "memory": ConversationSummaryMemory(
            llm=OpenAI(),
            memory_key="history",
            input_key="question",
            return_messages=True)}


refine = RetrievalQA.from_chain_type(llm=OpenAI(),
                                 chain_type="map_reduce",
                                 return_source_documents=True,
                                 chain_type_kwargs=chain_type_kwargs,
                                 retriever=big_chunks_retriever,
                                 verbose=True)
© www.soinside.com 2019 - 2024. All rights reserved.