【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 分钟前
RAG 全链路深度拆解:“大白”学习RAG的极简指南
人工智能·langchain·llm
Tzarevich34 分钟前
别再信它“一本正经地胡说”了!用 RAG终结大模型“幻觉”
后端·langchain·llm
chaors1 小时前
Langchain入门到精通0x07:基于Web网页的RAG实战
人工智能·langchain·ai编程
chaors1 小时前
Langchain入门到精通0x08:摘要链(load_summarize_chain)
人工智能·langchain·ai编程
爱打代码的小林5 小时前
用 LangChain 解析大模型输出
java·python·langchain·大模型
1941s15 小时前
02-LangChain 框架入门:模型抽象与 Prompt 模板
人工智能·langchain·prompt
chaors18 小时前
Langchain入门到精通0x08:预置链
人工智能·langchain·ai编程
chaors21 小时前
Langchain入门到精通0x0a:天气小助手实战
人工智能·langchain·ai编程
羑悻的小杀马特1 天前
LangChain实战:工具调用+结构化输出,让AI从“聊天“变“干活“
android·人工智能·langchain