【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
yuyuyui1 小时前
LangChain框架-Model
langchain·rag
Csvn5 小时前
🌟 LangChain 30 天保姆级教程 · Day 30|终章!RAG 项目打包交付:Docker + CI/CD + 文档,打造企业级 AI 知识库!
langchain
Irissgwe6 小时前
LangChain 与 LangGraph 介绍(一)
人工智能·langchain·llm·langgraph
wangjialelele7 小时前
从零入门 LangChain:Python 语法详解 + 工具开发 + 结构化输出实战
开发语言·人工智能·python·语言模型·langchain
花千树-01010 小时前
ReAct Agent是什么?与传统LLM/Chatbot的本质区别(原理篇)
langchain·react·ai编程·chatbot·ai agent·langgraph·mcp
Irissgwe10 小时前
LangChain 与 LangGraph 介绍(二)
人工智能·langchain·llm·langgraph
秦歌6661 天前
LangChain-9-多agent电商助手案例
langchain
Arvid1 天前
LangGraph 记忆系统设计实战
langchain
不会敲代码11 天前
MCP 实战第二弹:集成高德地图、文件系统、Chrome DevTools,打造能看能写能操控浏览器的超级 Agent
langchain·llm·mcp
Csvn1 天前
🌟 LangChain 30 天保姆级教程 · Day 28|RAG 性能优化实战!缓存+异步+批处理,让 AI 响应快如闪电!
langchain