【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
海底的星星fly1 小时前
【Prompt学习技能树地图】LangChain原理及应用操作指南
人工智能·语言模型·langchain·prompt
呲溜滑_1 小时前
langchain(node.js) 实际应用订单助手
microsoft·langchain·node.js
nvd1115 小时前
一个简单的GitHub AI Agent 实现指南
人工智能·langchain
FreeCode16 小时前
一文了解LangGraph智能体设计开发过程:Thinking in LangGraph
python·langchain·agent
曦云沐16 小时前
第二篇:LangChain 1.0 模块化架构与依赖管理
人工智能·langchain·智能体
Dr_哈哈17 小时前
🚀 LangGraph 保姆级教程:从零构建你的第一个 AI Agent 工作流
langchain·node.js·agent
天星烛20 小时前
Milvus 分词器学习笔记
langchain
再会呀20 小时前
[Ai Agent] 10 MCP基础:打破孤岛,让MCP连接万物
langchain·mcp
陈鋆20 小时前
Langchain-Chatchat[三、知识库管理的 RAG]
windows·langchain
大白的编程笔记1 天前
LangChain 系统详解
langchain