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

如何查看LangChain中MultiQueryRetriever Pipeline发给Ollama的最终Prompt?

查看LangChain发送给Ollama的完整最终Prompt的方法

方法一:在Prompt与Ollama之间插入检查步骤

修改你的qa_chain,在prompt和ollama组件之间添加一个自定义的RunnableLambda,专门打印格式化后的完整Prompt:

def print_final_prompt(prompt_content):
    print("发送给Ollama的完整Prompt内容:")
    print(prompt_content.to_string())
    return prompt_content

qa_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | RunnableLambda(print_final_prompt)  # 新增此步骤打印完整Prompt
    | ollama
    | StrOutputParser()
)

调用qa_chain.invoke()后,控制台就会输出拼接好上下文和用户问题的完整Prompt。

方法二:单独验证Prompt模板的拼接结果

先手动获取检索到的上下文,再传入Prompt模板生成完整内容:

# 获取检索到的上下文文档
retrieved_context = retriever.invoke("Give 10 quotes from this articles related to love?")
# 构造Prompt所需的输入参数
prompt_input = {"context": retrieved_context, "question": "Give 10 quotes from this articles related to love?"}
# 生成完整Prompt
final_prompt = prompt.invoke(prompt_input)
# 打印查看
print(final_prompt.to_string())

这种方式可以脱离整个LLM调用流程,单独验证Prompt模板的拼接效果。

方法三:开启LangChain的调试日志

通过开启详细日志,直接查看LLM调用前的输入内容:

import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("langchain.chains.llm").setLevel(logging.DEBUG)

运行qa_chain.invoke()后,日志中会输出发送给Ollama的完整Prompt信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 13:03:13