【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
Wilber的技术分享2 小时前
【大模型实战笔记 7】RAG技术:从原理到实战——基于Streamlit的智能文档问答系统
人工智能·笔记·langchain·llm·问答系统·rag·知识库检索
工藤学编程4 小时前
零基础学AI大模型之新版LangChain向量数据库VectorStore设计全解析
数据库·人工智能·langchain
韩曙亮20 小时前
【AI 大模型】LangChain 框架 ① ( LangChain 简介 | LangChain 模块 | LangChain 文档 )
人工智能·ai·langchain·llm·大语言模型·prompts·agents
呲溜滑_1 天前
langchain(node.js) 实际应用==》md文件检索
langchain·node.js
学Linux的语莫1 天前
LangGraph知识
运维·langchain
core5121 天前
基于 LangChain + 通义千问打造ReAct私募基金智能问答助手
ai·langchain·大模型·qwen·通义·千问·助手
boboo_2000_02 天前
基于SpringBoot+Langchain4j的AI机票预订系统
spring cloud·微服务·云原生·langchain
陈鋆2 天前
Langchain-Chatchat[四、RAG对话流程代码解析]
开发语言·python·langchain
我是zxb2 天前
LangChain 入门系列②:核心组件之 Model IO 快速入门指南
服务器·microsoft·langchain
沛沛老爹2 天前
LangGraph系列9 :调试、日志与可观测性 —— 当你的 AI 智能体突然精神分裂,如何 5 分钟定位故障?
人工智能·langchain·llm·调试·rag·langgraph·ai入门