【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
花千树-01036 分钟前
Java 接入多家大模型 API 实战对比
java·开发语言·人工智能·ai·langchain·ai编程
Pkmer1 小时前
为基于LLM应用开发而生的LangChain框架
langchain·llm
疯狂成瘾者3 小时前
text_splitter常见方法
python·langchain
qq_白羊座4 小时前
Langchain、Cursor、python的关系
开发语言·python·langchain
Clarence Liu5 小时前
langchain源码研究 - deepagents设计思想学习
人工智能·驱动开发·学习·langchain
杨艺韬5 小时前
LangChain设计与实现-第11章-Chain 组合模式
langchain·agent
杨艺韬5 小时前
LangChain设计与实现-第7章-输出解析与结构化输出
langchain·agent
杨艺韬5 小时前
LangChain设计与实现-第5章-语言模型抽象层
langchain·agent
杨艺韬5 小时前
LangChain设计与实现-第14章-Agent 架构与执行循环
langchain·agent
杨艺韬5 小时前
LangChain设计与实现-第10章-向量存储与检索器
langchain·agent