【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
To_OC2 小时前
手搓 LangChain 工具调用:原来 Agent 的核心逻辑,就是个 while 循环
人工智能·langchain·llm
Boop_wu8 小时前
[LangChain]LangChain聊天模型
langchain
常常有10 小时前
LangChain 学习记录 01:为什么需要 LangChain?从 API 调用到 AI 应用框架
人工智能·langchain
湿韵轮回10 小时前
LangChain LCEL,用 | 串联AI的魔法语言
服务器·人工智能·langchain
小李云雾11 小时前
Langchain【3】---提示词模板、输出解析、LCEL链式调用
java·人工智能·程序人生·langchain
知行合一。。。13 小时前
LangChain--07--智能体、Agent的基本用法
android·数据库·langchain
吃饱了得干活14 小时前
别只会传 temperature!一文挖透 LangChain 模型配置:Profile、全参数与运行时动态覆盖
python·langchain
龙腾亚太15 小时前
基础差/零基础人员具身智能学习路线
langchain·qlora·大模型培训·智能体培训·具身智能培训·llm 实战教程
xywww16821 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
糖果店的幽灵1 天前
从 GPT Researcher 学习 LangChain
gpt·学习·langchain