【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
java资料站34 分钟前
LangChain 中文入门教程
langchain
QWsin3 小时前
【LangGraph Server】 LangGraph Server是什么?
人工智能·langchain·langgraph·langsmith
米小虾3 小时前
从对话到行动:AI Agent 架构演进与工程实践指南
人工智能·langchain·agent
Csvn3 小时前
🌟 LangChain 30 天保姆级教程 · Day 4|用 LLMChain 把 Prompt 和 LLM 打包成可复用组件!
langchain
coderlin_6 小时前
langchain 基础
microsoft·langchain
QC·Rex6 小时前
国产大模型应用实践:从 0 到 1 搭建企业级 AI 助手
人工智能·langchain·大语言模型·rag·企业应用·ai 助手
轻舟行77 小时前
langchain从入门到入土 (一)langchain的历程及应用场景
人工智能·python·langchain
m0_488913018 小时前
收藏备用!大厂AI Agent开发岗位解析+小白友好学习路线(程序员必看)
人工智能·学习·langchain·大模型·agent·就业·大模型学习
子夜江寒17 小时前
用 LangChain 构建 LLM 应用:从模型调用到输出解析
langchain
chushiyunen1 天前
langchain实现agent智能体笔记
笔记·langchain