【langchain学习】使用Langchain生成多视角查询

使用Langchain生成多视角查询

  1. 导入所需库

    python 复制代码
    from langchain.prompts import ChatPromptTemplate
    from langchain_core.output_parsers import StrOutputParser
    from langchain_core.runnables import RunnablePassthrough
    from config import llm
  2. 设置提示模板

    python 复制代码
    prompt = ChatPromptTemplate.from_template(
        """
        You are an intelligent assistant. Your task is to generate 5 questions based on the provided question in different wording and different perspectives to retrieve relevant documents from a vector database. By generating multiple perspectives on the user question, your goal is to help the user overcome some of the limitations of the distance-based similarity search. Provide these alternative questions separated by newlines. Original question: {question}
        """
    )
  3. 定义查询生成过程

    python 复制代码
    generate_queries = (
        {"question": RunnablePassthrough()}
        | prompt
        | llm
        | StrOutputParser()
        | (lambda x: x.split("\n"))
    )
  4. 调用生成多视角查询

    python 复制代码
    result = generate_queries.invoke("温格高是如何赢下2023年环法自行车赛的?")
    print(result)
  5. 示例输出

    python 复制代码
    [
        "温格高在2023年环法自行车赛中采用了哪些策略?",
        "2023年环法自行车赛温格高获胜的关键因素是什么?",
        "温格高在2023年环法自行车赛中面对哪些挑战?",
        "温格高是如何准备2023年环法自行车赛的?",
        "2023年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
心本无晴.28 分钟前
LangChain 完全指南:从入门到生产级智能体构建
langchain
qq_5470261791 小时前
LangChain 模型(Models)
windows·microsoft·langchain
JaydenAI1 小时前
[拆解LangChain执行引擎]一个实例理解LangChain的几种流模式
ai·langchain·agent·pregel
大模型真好玩16 小时前
LangChain DeepAgents 速通指南(二)—— Summarization中间件为Agent作记忆加减法
人工智能·langchain·agent
2501_9269783318 小时前
思想波与引力共振理论:统一物理主义意识框架的革命性探索--AGI理论系统基础12
人工智能·经验分享·架构·langchain·agi
无聊的小坏坏18 小时前
RAG 实战 (下):打造多轮对话知识助手
langchain·rag·大模型应用
彭于晏Yan18 小时前
LangChain4j实战三:图像模型
java·spring boot·后端·langchain
ZaneAI19 小时前
🚀 Claude Agent SDK 使用指南:会话管理(Session )
langchain·agent·claude
JaydenAI1 天前
[LangChain之链]Runnable,不仅要可执行,还要可存储、可传输、可重建、可配置和可替换
python·langchain
UIUV1 天前
AI Agent 开发实战:从原理到最小化实现
后端·langchain·node.js