【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
ChaHae-In5 小时前
Agent核心能力详解
langchain
染指111021 小时前
119.Agent-LangChain核心组件-Runtime运行时
人工智能·langchain·agent
用户3134672143541 天前
Agent 开发学习笔记(六):LCEL:把组件串成链
langchain·agent
艾醒(AiXing-w)1 天前
LangChain 1.0 智能体开发(三):Agent 记忆管理——从短期对话到跨会话长期记忆
数据库·人工智能·langchain
KimLiu1 天前
LCODER之AI Agent开发实战一 :问数项目智能体搭建(3)元数据知识库的构建
langchain·llm·agent
烛之武1 天前
LangChain笔记
langchain·大模型·agent·mcp
运维@小兵2 天前
LangChain系列——中间件(Middleware)
langchain·langchain中间件
hyunbar2 天前
LangChain 实战:中间件Summarization详解
langchain·agent·harness
归去来 兮2 天前
LangChain从入门到精通
python·langchain·langgraph
沉下心来学鲁班2 天前
初识DeepAgents搭建第一个智能体
人工智能·python·langchain