【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
CodeLinghu9 小时前
《LLM大语言模型+RAG实战+Langchain+ChatGLM-4+Transformer》
语言模型·langchain·transformer
CodeCodeBond2 天前
RAG:实现基于本地知识库结合大模型生成(LangChain4j快速入门#1)
java·后端·ai·语言模型·langchain·个人开发·ai编程
花千树-0103 天前
LangChain教程 - RAG - PDF解析
langchain·pdf
梦想画家4 天前
LangChain:使用表达式语言优化提示词链
langchain·提示词链
橙意满满的西瓜大侠5 天前
langchain基础(二)
langchain
uncle_ll6 天前
ChatGPT大模型极简应用开发-CH5-使用 LangChain 框架和插件增强 LLM 的功能
gpt·chatgpt·langchain·llm·rag
码--到成功7 天前
langchain 入门(一)
langchain
爱吃面的猫9 天前
Langchain+文心一言调用
langchain·文心一言
爱吃面的猫9 天前
Langchain+讯飞星火大模型Spark Max调用
langchain
大模型铲屎官12 天前
玩转 LangChain:从文档加载到高效问答系统构建的全程实战
人工智能·python·ai·langchain·nlp·文档加载·问答系统构建