【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
默 语5 小时前
Spring-AI vs LangChain4J:Java生态的AI框架选型指南
java·人工智能·spring·ai·langchain·langchain4j·spring-ai
leafff1236 小时前
一文了解:智能体大模型LangChain 和 Dify有什么区别?
人工智能·架构·langchain
W_Meng_H1 天前
LangChain Agent - 火山引擎大模型推理与回答的完整流式输出
langchain·火山引擎
Jack___Xue1 天前
LangChain实战快速入门笔记(三)--LangChain使用之Chains
人工智能·笔记·langchain
Elwin Wong1 天前
将你的LangChian Agent可视化
人工智能·langchain·agent
工藤学编程1 天前
零基础学AI大模型之LangChain核心:Runnable接口底层实现
人工智能·langchain
Jack___Xue2 天前
LangChain实战快速入门笔记(二)--LangChain使用之Model I/O
笔记·langchain
AI大模型2 天前
【连载】零基础跟我学做AI Agent(第3课:用LangChain开发一个做题Agent)
langchain·llm·agent
micro_cloud_fly2 天前
langchain langgraph历史会话的 json序列化
python·langchain·json
工藤学编程2 天前
零基础学AI大模型之LangChain Retriever
人工智能·langchain