You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何去除LangChain中LLM响应的AI:/Assistant:前缀

解决LLM响应自动添加'Assistant:'/'AI:'前缀问题

我基于RAG搭建了一款聊天应用,使用LangChain配置了如下提示链:

## Prompt Chain Setup ##
retrieval_qa_chat_prompt = hub.pull("langchain-ai/retrieval-qa-chat")
retriever = chroma_db.as_retriever()

contextualize_q_system_prompt = (
    "Given a chat history and the latest user question "
    "which might reference context in the chat history, "
    "formulate a standalone question which can be understood "
    "without the chat history. Do NOT answer the question, "
    "just reformulate it if needed and otherwise return it as is."
)

contextualize_q_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", contextualize_q_system_prompt),
        #MessagesPlaceholder("chat_history"), <- removed to stop question_generator
        ("human", "{input}"),
    ]
)
history_aware_retriever = create_history_aware_retriever(
    llm, retriever, contextualize_q_prompt
)


system_prompt = (
    "Use the following pieces of retrieved context to answer "
    "the question. If you don't know the answer, say that you "
    "don't know. Keep the answer concise and ensure that any"
    "configuration file samples or examples use JSON format."
    "\n\n"
    "{context}"
)

qa_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system_prompt),
        MessagesPlaceholder("chat_history"),
        ("human", "{input}"),
    ]
)
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)

rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)

我通过LM Studio本地服务器,以默认设置调用microsoft/Phi-3-mini-4k-instruct-gguf模型。但LLM每次响应都会带有'Assistant:'或'AI:'前缀(示例响应:AI: The "isRequired" field indicates whether or not a form field must be filled out for the form to be submitted. If set to true.....),而我的UI已能明确区分AI与人类消息,该前缀属于冗余内容。我已尝试在提示词中要求模型不要添加该前缀,但无效果,请问如何解决?


解决方案

1. 调整LM Studio的模型聊天模板

本地模型的前缀通常由预设聊天模板控制,直接修改模板可从根源解决问题:

  • 打开LM Studio,加载目标模型后进入「Model Settings」页面
  • 找到「Chat Template」选项,查看当前模板内容
  • 定位到助手回复的模板段(例如类似<|assistant|>{assistant_message}或Assistant: {assistant_message}的结构)
  • 删除模板中助手回复的前缀标识(如Assistant:、AI:或<|assistant|>后的冒号),确保模板仅输出回复内容

2. 在LangChain中添加输出后处理逻辑

通过字符串处理强制移除响应前缀,适用于无法修改模型模板的场景:

# 调用RAG链获取结果
result = rag_chain.invoke({"input": user_query, "chat_history": chat_history})
raw_answer = result["answer"]

# 定义需要移除的前缀列表
target_prefixes = ["AI:", "Assistant:"]

# 遍历前缀并移除
processed_answer = raw_answer
for prefix in target_prefixes:
    if processed_answer.startswith(prefix):
        processed_answer = processed_answer[len(prefix):].strip()

# 使用处理后的响应内容
print(processed_answer)

3. 强化LangChain系统提示的明确性

优化系统提示,确保模型清晰接收到“禁止添加前缀”的指令:
修改system_prompt为:

system_prompt = (
    "Use the following pieces of retrieved context to answer "
    "the question. If you don't know the answer, say that you "
    "don't know. Keep the answer concise and ensure that any"
    "configuration file samples or examples use JSON format."
    "\n\n"
    "IMPORTANT: Do NOT add any prefixes like 'AI:' or 'Assistant:' at the beginning of your answer."
    "\n\n"
    "{context}"
)

内容的提问来源于stack exchange,提问作者JamesMatson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 13:02:31