【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
ai大师7 小时前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4
爱喝喜茶爱吃烤冷面的小黑黑11 小时前
小黑一层层削苹果皮式大模型应用探索:langchain中智能体思考和执行工具的demo
python·langchain·代理模式
大千AI17 小时前
LangChain Core架构解析:模块化设计与LCEL原语实现原理
langchain
Chatopera 研发团队17 小时前
智能体开发,实现自定义知识库,基于 LangChain,qwen 7b, ollama, chatopera | LLMs
langchain
硅谷神农18 小时前
第一章:AI与LangChain初探 —— 你的第一个“智能”程序
langchain
硅谷神农18 小时前
第二章:模型 (Models) —— AI应用的大脑
langchain
你那个道上的1 天前
LangChain4j学习与实践
langchain·ai编程
缘友一世2 天前
LangChain【8】之工具包深度解析:从基础使用到高级实践
langchain
bytebeats2 天前
强大的代理通信其实是 A2A + MCP + LangChain
langchain·mcp
架构师那点事儿2 天前
一文带你俯瞰大模型领域的世界
langchain·aigc·ai编程