【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
liu****43 分钟前
LangGraph-AI应用开发框架(二)
windows·langchain·大模型·工作流·langgraph
是小蟹呀^1 小时前
【总结】LangChain中的中间件Middleware
python·中间件·langchain·agent
FrontAI2 小时前
深入浅出 LangChain —— 第三章:模型抽象层
前端·人工智能·typescript·langchain·ai agent
Destiny_where2 小时前
Langgraph基础(4)-中断interrupt.实现图执行的动态暂停与外部交互
人工智能·python·langchain·langgraph
念念不忘 必有回响4 小时前
RAG 入门第三课:给你的知识库装上大脑(基于LangChain与Qwen3.5的本地RAG系统搭建)
langchain·rag
大模型真好玩6 小时前
大模型训练全流程实战指南工具篇(十一)—— 大模型训练参数调优实战:从小白到调参高手
人工智能·langchain·deepseek
星浩AI7 小时前
手把手带你跑通智能体 A2A 实战案例
后端·langchain·agent
繁星星繁9 小时前
【AI】LangChain与LangGraph展望
人工智能·langchain
yanghuashuiyue21 小时前
langchain AI应用框架研究【开发部署-篇四】
python·langchain
Where-1 天前
LangChain核心组件——Model I/O
langchain