【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
是上好佳佳佳呀6 小时前
【LangChain|Day03】LangChain 链式调用 Chains 笔记
笔记·langchain
Niuguangshuo7 小时前
LangChain学习之旅(三):用Memory赋予模型记忆
学习·langchain
矩阵科学11 小时前
Langchain.js 实战五:Agent 实战
langchain·node.js
菜鸟小九11 小时前
hello agent(智能体经典范式、框架开发实践)
python·langchain·agent
不吃青椒!1 天前
LangGraph 流式事件处理:从实战到体系
ai·langchain·状态模式
染指11101 天前
24.RAG进阶(Advanced RAG)-摘要索引
langchain·rag
IManiy1 天前
知识点之LangGraph 中的四个核心概念:State、Node、Edge 和Checkpoint
langchain
雮尘1 天前
LangGraph 与 LangSmith 入门教程(JS/TS 版)
前端·人工智能·langchain
veminhe1 天前
解决了使用langchain调用聊天模型报的错
langchain
颜酱1 天前
LangChain上手 Agent:让大模型自己调用工具解决问题
langchain