【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
chaors9 小时前
DeepResearchSystem 0x01:Agent 基础
langchain·aigc·ai编程
一只小bit11 小时前
LangGraph 子图使用和房源搜索Agent综合案例实现
机器学习·langchain·llm·langgraph
bloglin9999913 小时前
langchain 和 langgraph 和 react
javascript·react.js·langchain
吃饱了得干活18 小时前
LangChain Agent 高级玩法:命名、结构化输出与流式模式
langchain·llm·agent
phltxy20 小时前
LangGraph智能租房助手实践
大数据·人工智能·python·深度学习·语言模型·langchain
Esaka_Forever21 小时前
LangChain RunnableSequence 获取中间输出的几种方案(替代SequentialChain获取中间结果)
langchain
早点睡啊Y1 天前
深入学LangChain官方文档(二十二):Frontend 高级形态——Headless Tools、Time Travel 与 Generative UI
ui·langchain·状态模式
大郭鹏宇1 天前
基于 LangGraph 构建智能分诊系统(一):项目概述与环境搭建
大数据·人工智能·microsoft·langchain
玉鸯1 天前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
chaors2 天前
DeepRearchSystem 0x00:初识
langchain·llm·ai编程