【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
nvd1114 小时前
LangChain LCEL 工具调用实战:从确定性链到动态 Agent
langchain
YHLG20 小时前
LangChain v1.0+ 入门详解:概念、架构、组件、模板与实战
python·langchain
西柚小萌新21 小时前
【人工智能:Agent】--7.Langchain短期记忆
langchain
眠りたいです21 小时前
使用LangChain进行AI应用构建-快速上手,定义模型和调用工具部分
人工智能·langchain·llm·ollama·python3.13
果粒蹬i1 天前
使用 LangChain 与 CrewAI 实现 AI Agent 的多步任务规划(零基础入门)
人工智能·langchain
doll ~CJ1 天前
Large Language Model(LLM)应用开发学习实践(一)
langchain·llm·ai agent·应用开发·通义千问系列大模型
光影少年1 天前
前端如何开发ai生成图片及流式回答
前端·人工智能·langchain
nvd112 天前
深入 ReAct Agent 的灵魂拷问:从幻觉到精准执行的调试实录
python·langchain
wopelo2 天前
LangChain v0.3 ReAct Agent 原理浅析
langchain·agent
TGITCIC2 天前
langchain入门(五)- 用mongodb管理提示词以及以restful service暴露
langchain·ai大模型·rag·ai agent·ai智能体·agent开发·大模型产品