【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
郝学胜-神的一滴7 小时前
AI 编程智能体 20:技术栈选型与架构落地全攻略
人工智能·python·架构·langchain·1024程序员节·智能体
编程见习者8 小时前
Deep Agents介绍
人工智能·python·langchain
云和数据.ChenGuang20 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
染指11101 天前
162.Agent-LangGraph-检查点的作用
人工智能·python·langchain·agents
叼烟扛炮1 天前
[LangChain] 学习笔记(02):LCEL 链式调用与动态模型配置实战
服务器·开发语言·网络·笔记·学习·langchain
YIAN1 天前
RAG 学到最后,我真正学会的不是“检索”,而是让系统知道自己缺什么
langchain·node.js·llm
lee_curry1 天前
第十周完整学习笔记:继续升级为“检索更准、权限安全、索引可维护、失败可恢复、长期可运行”的企业级 RAG 系统
python·ai·langchain·agent·langgraph
破烂三角裤2 天前
AI 没有记忆?我给儿童故事 Agent 搭了一套“长期记忆”系统
langchain
云和数据.ChenGuang2 天前
光哥说Python 异常深度解析:从异常模型到工程化实践
java·前端·人工智能·python·langchain
Maiko Star2 天前
【HM-LC】LangChain RAG查询优化详解:查询重写、混合检索与重排序实战
langchain·rag