【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 小时前
22_LangGraph核心组件
langchain
聚客AI4 小时前
📚LangChain框架下的检索增强:5步构建高效智能体系统
人工智能·langchain·llm
灵海之森8 小时前
langgraph快速搭建agent后端和react前端
langchain
大志说编程8 小时前
LangChain框架入门18: 十分钟带你搞定LLM工具调用
python·langchain·ai编程
玲小珑9 小时前
LangChain.js 完全开发手册(一)AI 应用开发入门
前端·langchain·ai编程
ReedFoley21 小时前
【笔记】动手学Ollama 第五章 Ollama 在 LangChain 中的使用 - Python 集成
笔记·langchain
AI大模型1 天前
万字长文!从 0 到 1 搭建基于 LangGraph 的 AI Agent
langchain·llm·agent
悲欢笑miki1 天前
Api调用大模型(实用)
langchain
大志说编程2 天前
LangChain框架入门17: 手把手教你创建LLM工具
python·langchain·ai编程
王国强20092 天前
LangChain 设计原理分析¹⁵ | AI 编排平台的演化方向
langchain