【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
吴佳浩7 小时前
Langchain 浅出
python·langchain·llm
FreeCode18 小时前
LangGraph1.0智能体开发:运行时系统
python·langchain·agent
重整旗鼓~2 天前
1.大模型使用
java·语言模型·langchain
hnode2 天前
🚀 前端开发者的 AI 入门指南:5 分钟搭建你的第一个 RAG 智能问答系统
langchain
大模型真好玩2 天前
LangChain1.0实战之多模态RAG系统(二)——多模态RAG系统图片分析与语音转写功能实现
人工智能·langchain·mcp
大模型教程2 天前
谷歌AI Agent技术指南深度解读,从概念到生产
langchain·llm·agent
爱装代码的小瓶子2 天前
【初识AI】大模型和LangChain?
人工智能·langchain
AI大模型2 天前
LangChain、LangGraph、LangSmith这些AI开发框架有什么区别?一篇文章解释清楚
langchain·llm·agent
爬点儿啥2 天前
[Ai Agent] 09 LangGraph 进阶:构建可控、可协作的多智能体系统
人工智能·ai·langchain·大模型·agent·langgraph
吴佳浩2 天前
LangChain / LLM 开发中:invoke() 与 predict() 的区别
python·langchain·llm