【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
_一条咸鱼_19 小时前
LangChain正则表达式(19)
人工智能·面试·langchain
前端双越老师20 小时前
使用 langChain.js 实现 RAG 知识库语义搜索
人工智能·langchain·node.js
都叫我大帅哥21 小时前
LangChain的TXT文档加载:从入门到实战的终极指南
python·langchain
聚客AI3 天前
🔥 大模型开发进阶:基于LangChain的异步流式响应与性能优化
人工智能·langchain·agent
软件测试君3 天前
向量数据库 Chroma 和 Milvus的使用
langchain·aigc·openai
成都犀牛3 天前
LangChain 内存(Memory)
人工智能·机器学习·langchain
FLYINGPIG4 天前
【Langchain】超详细构建Langchain架构下的MCP与Agent智能结合体
langchain·llm
_一条咸鱼_4 天前
LangChain多模态提示词设计探索的源码级深度剖析(16)
人工智能·面试·langchain
_一条咸鱼_4 天前
LangChain输出解析器的作用与类型解析(17)
人工智能·面试·langchain
_pass_4 天前
LangChain框架 Prompts、Agents 应用
langchain