【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
一起逃去看海吧2 小时前
LangChain-01
langchain
打小就很皮...7 小时前
基于 Python + LangChain + SQL 生成自动查询数据实战
数据库·sql·langchain
新知图书10 小时前
带搜索工具的对话 Agent示例与解析
人工智能·langchain·agent·智能体·langgraph
code bean10 小时前
【Langchain】 ChatPromptTemplate:从“手动拼字符串“到“专业模板“的进化之路
人工智能·机器学习·langchain
Traving Yu13 小时前
LangChain
人工智能·langchain
青衫码上行13 小时前
如何接入AI大模型
java·人工智能·ai·langchain·ai编程
code bean1 天前
【LangChain】 入门:从分步调用到链式编程
langchain
C137的本贾尼1 天前
从零认识 Spring AI:Java 开发者的 AI 第一课
python·langchain
Fleshy数模1 天前
基于 LangChain 实现 PDF 文档检索:从加载到向量检索全流程
人工智能·数据挖掘·langchain·大模型