LangChain始终显示相同模板内容,重启内核无效的技术问题
问题:LangChain中RetrievalQAWithSourcesChain模板无法自定义,始终显示固定内容
使用LangChain的RetrievalQAWithSourcesChain时,无论是否尝试指定自定义模板,链始终显示相同的默认模板及示例内容,重启内核后问题仍存在。期望模板支持自定义或为空。
相关代码
chain = RetrievalQAWithSourcesChain.from_chain_type( llm, chain_type="stuff", retriever=db.as_retriever() ) print(chain)
输出结果
Out:RetrievalQAWithSourcesChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, combine_documents_chain=StuffDocumentsChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, input_key='input_documents', output_key='output_text', llm_chain=LLMChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, prompt=PromptTemplate(input_variables=['summaries', 'question'], output_parser=None, partial_variables={}, template='Given the following extracted parts of a long document and a question, create a final answer with references ("SOURCES"). \nIf you don\'t know the answer, just say that you don\'t know. Don\'t try to make up an answer.\nALWAYS return a "SOURCES" part in your answer.\n\nQUESTION: Which state/country\'s law governs the interpretation of the contract?\n=========\n Content: This Agreement is governed by English law and the parties submit to the exclusive jurisdiction of the English courts in relation to any dispute (contractual or non-contractual) concerning this Agreement save that either party may apply to any court for an injunction or other relief to protect its Intellectual Property Rights.\nSource: 28-pl\nContent: No Waiver. Failure or delay in exercising any right or remedy under this Agreement shall not constitute a waiver of such (or any other) right or remedy.\n\n11.7 Severability. The invalidity, illegality or unenforceability of any term (or part of a term) of this Agreement shall not affect the continuation in force of the remainder of the term (if any) and this Agreement.\n\nWell I know this nation.\nSource: 34-pl\n=========\nFINAL ANSWER: The president did not mention Michael Jackson.\nSOURCES:\n\nQUESTION: {question}\n=========\n{summaries}\n=========\n FINAL ANSWER:', template_format='f-string', validate_template=True), llm=HuggingFaceHub(cache=None, verbose=False, callbacks=None, callback_manager=None, tags=None, metadata=None, client=InferenceAPI(api_url='https://api-inference.huggingface.co/pipeline/text2text-generation/mrm8488/t5-base-finetuned-question-generation-ap', task='text2text-generation', options={'wait_for_model': True, 'use_gpu': False}), repo_id='mrm8488/t5-base-finetuned-question-generation-ap', task=None, model_kwargs={'temperature': 0.9, 'max_new_tokens': 700}, huggingfacehub_api_token='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'), output_key='text', output_parser=StrOutputParser(), return_final_only=True, llm_kwargs={}), document_prompt=PromptTemplate(input_variables=['page_content', 'source'], output_parser=None, partial_variables={}, template='Content: {page_content}\nSource: {source}', template_format='f-string', validate_template=True), document_variable_name='summaries', document_separator='\n\n'), question_key='question', input_docs_key='docs', answer_key='answer', sources_answer_key='sources', return_source_documents=False, retriever=VectorStoreRetriever(tags=['Chroma', 'HuggingFaceEmbeddings'], metadata=None, vectorstore=<langchain.vectorstores.chroma.Chroma object at 0x7f56d774ae30>, search_type='similarity', search_kwargs={}), reduce_k_below_max_tokens=False, max_tokens_limit=3375)
解决方法
- 放弃使用
from_chain_type的默认配置,手动构建链组件并传入自定义模板:
from langchain.chains import RetrievalQAWithSourcesChain, StuffDocumentsChain, LLMChain from langchain.prompts import PromptTemplate # 自定义主查询模板 custom_prompt = PromptTemplate( input_variables=["summaries", "question"], template="""根据以下文档片段和问题,生成带来源标注的最终答案。 若无法回答,请直接说明,禁止编造内容。必须包含SOURCES部分。 QUESTION: {question} ========= {summaries} ========= FINAL ANSWER: """ ) # 自定义文档片段格式化模板(可选) document_prompt = PromptTemplate( input_variables=["page_content", "source"], template="内容: {page_content}\n来源: {source}" ) # 构建LLM链 llm_chain = LLMChain(llm=llm, prompt=custom_prompt) # 构建文档整合链 stuff_chain = StuffDocumentsChain( llm_chain=llm_chain, document_prompt=document_prompt, document_variable_name="summaries" ) # 构建最终的检索QA链 chain = RetrievalQAWithSourcesChain( combine_documents_chain=stuff_chain, retriever=db.as_retriever() ) print(chain)
- 上述代码中,可完全自定义主模板和文档格式化模板的内容、格式,替换默认的示例文本
- 需保证模板中的变量名(如
summaries、question)与链组件中使用的变量名一致
内容的提问来源于stack exchange,提问作者Rishabh Gupta
相关产品推荐
相关产品推荐

