【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
Tbisnic11 小时前
LangChain的 六大核心组件与 RAG 知识库构建
人工智能·python·ai·langchain·rag·langgraph
gb421528713 小时前
python中pypdf库和langchain-unstructured库在解析pdf文件的时候的区别?
python·langchain·pdf
badhope15 小时前
用RAG做了个智能客服,上线第一天就被用户骂了——我的7天实战复盘
人工智能·langchain
Wang's Blog21 小时前
AI Agent白手起家44: LangChain 文档切分实战 — 长度、文本架构与语义切片
人工智能·langchain
jaboo1221 小时前
postgresql从入门到精通
数据库·postgresql·langchain
闲猫2 天前
LangChain / Integrations / Integrations by component / Tool
java·数据库·langchain
程序员AI工坊2 天前
Agent 开发:ReAct 循环与工具调用实战——从单次调用到自主 Agent
人工智能·后端·python·langchain·agent·react
Wang's Blog2 天前
AI Agent白手起家41: LangChain 链的高级应用:函数、记忆、路由与容错
langchain
Wang's Blog2 天前
AI Agent白手起家37: LangChain 输出解析器实战:文本、JSON、XML 与 Pydantic
xml·langchain·json
梦想三三2 天前
Qwen Function Calling实战:重构电商客服AI Agent
人工智能·python·langchain·大模型·rag