为基于LangChain的Llama 2 RAG系统自定义提示词实现德语输出
解决Llama 2 + LangChain RAG强制德语回答的提示词配置方案
核心问题定位
直接在LLM实例参数中传入提示词无效,因为RetrievalQA链会优先使用自身的检索增强提示模板,覆盖LLM的默认设置。正确的配置位置是在创建RetrievalQA链时,通过chain_type_kwargs参数注入自定义提示。
具体实现步骤
1. 基于Llama 2默认模板修改,添加德语强制规则
Llama 2基础模型的默认提示模板如下:
[INST] <<SYS>> You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. <</SYS>> {question} [/INST]
我们需要在系统提示段添加强制德语回答的指令,修改后的模板示例:
[INST] <<SYS>> You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. **Antworte immer auf Deutsch, unabhängig von der Sprache der Frage.** If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. <</SYS>> Gegeben sei der folgende Kontext: {context} Beantworte die Frage: {question} [/INST]
注:将指令和上下文/问题提示也改为德语可进一步强化模型的输出语言倾向,若需保留英文基础提示,仅添加强制德语的英文指令也可生效。
2. 在RetrievalQA链中配置自定义提示
使用LangChain的PromptTemplate构建自定义模板,然后传入RetrievalQA链的chain_type_kwargs:
from langchain.prompts import PromptTemplate from langchain.chains import RetrievalQA # 构建带德语强制要求的自定义提示模板 custom_rag_template = """[INST] <<SYS>> You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. **Always respond in German, no matter what language the question is asked in.** If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. <</SYS>> Given the following context: {context} Answer the question: {question} [/INST] """ custom_prompt = PromptTemplate( template=custom_rag_template, input_variables=["context", "question"] ) # 创建RetrievalQA链时注入自定义提示 qa_chain = RetrievalQA.from_chain_type( llm=your_local_llama2_instance, # 替换为你的本地Llama 2实例 chain_type="stuff", # 根据需求选择chain_type:stuff/map_reduce/refine/map_rerank retriever=your_vectorstore_retriever, # 替换为你的向量库检索器 chain_type_kwargs={"prompt": custom_prompt}, # 关键配置:传入自定义提示 return_source_documents=True # 可选:返回源文档用于验证检索准确性 )
3. 验证输出效果
调用链时,模型会强制以德语回答:
response = qa_chain({"query": "What is the core argument of the document?"}) print(response["result"]) # 输出应为德语内容
注意事项
- 严格遵循Llama 2的
[INST]/<<SYS>>语法格式,否则模型可能无法正确解析提示逻辑。 - 若使用Llama 2 Chat模型,需对应调整模板(部分Chat模型的系统提示包裹方式略有不同)。
- 若采用
map_reduce或refine类型的链,需分别配置map_prompt、combine_prompt(或refine_prompt),确保每个阶段的提示都包含德语强制指令。
内容的提问来源于stack exchange,提问作者Maxl Gemeinderat
相关产品推荐
相关产品推荐

