【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
刘立军13 小时前
本地大模型编程实战(33)用SSE实现大模型的流式输出
架构·langchain·全栈
ChinaRainbowSea2 天前
9. LangChain4j + 整合 Spring Boot
java·人工智能·spring boot·后端·spring·langchain·ai编程
玲小珑2 天前
LangChain.js 完全开发手册(八)Agent 智能代理系统开发
前端·langchain·ai编程
RainbowSea2 天前
10. LangChain4j + 持久化实操详细说明
langchain·llm·ai编程
RainbowSea2 天前
9. LangChain4j + 整合 Spring Boot
langchain·llm·ai编程
kunwen1233 天前
机器学习、深度学习
rnn·langchain·cnn·transformer·langgraph
Awesome Baron4 天前
《Learning Langchain》阅读笔记13-Agent(1):Agent Architecture
笔记·langchain·llm
coder_pig4 天前
👦抠腚男孩的AI学习之旅 | 7、LangChain (三) - 实战:知识库问答机器人 (RAG )
langchain·aigc·ai编程
阿加犀智能5 天前
使用Langchain生成本地rag知识库并搭载大模型
服务器·python·langchain
乔巴先生245 天前
LLMCompiler:基于LangGraph的并行化Agent架构高效实现
人工智能·python·langchain·人机交互