【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
夜雨深秋来3 小时前
多租户 AI Agent 平台架构设计与实践
架构·langchain·agent
杨大厨wd4 小时前
LangChain 实战:从 0 搭一个带历史记忆的智能客服 Agent
langchain
前进的李工5 小时前
智能Agent实战指南:记忆组件嵌入技巧(记忆)
开发语言·前端·javascript·python·langchain·agent
ftpeak6 小时前
AI开发之LangGraph教程4~记忆 (Memory)
python·ai·langchain·langgraph
终生成长者7 小时前
04LangChain SQL 问答系统知识点详解
数据库·python·sql·langchain
狐狐生风8 小时前
LangGraph 重构个人知识库问答系统(稳定 + 可扩展版)
python·langchain·rag·langgraph·agentai
JaydenAI10 小时前
[Deep Agents:LangChain的Agent Harness-04]TodoListMiddleware的任务拆解与状态流转
langchain·todolist·middleware·deep agents
MY_TEUCK10 小时前
【MY_TRUCK - AI 应用】RAG 与 LangChain 入门:检索增强生成、向量检索与链式编排
人工智能·机器学习·langchain
狐狐生风10 小时前
LangGraph 核心概念全解笔记
人工智能·python·langchain·prompt·langgraph
@atweiwei11 小时前
LangChainRust Agent 引擎:Graph 构建到执行
rust·langchain·llm·agent·rag·langchaingraph