【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
玄同7658 小时前
LangChain 核心组件全解析:构建大模型应用的 “乐高积木”
人工智能·python·语言模型·langchain·llm·nlp·知识图谱
小芳矶16 小时前
使用 Langgraph 构建本地 RAG 知识库:从文档加载到检索
python·langchain
麦兜*16 小时前
深入剖析新一代AI Native技术栈:从向量数据库与LangChain应用架构到多模态大模型微调与智能体工作流的全链路实战
数据库·人工智能·langchain
西柚小萌新17 小时前
【人工智能:Agent】--11.Langchain高级用法
langchain
薛定谔的猫198217 小时前
四、基于LangChain与HuggingFace Pipeline的本地大语言模型对话系统搭建指南(使用阿里千问-模型)
人工智能·语言模型·langchain
花间相见17 小时前
【AI开发】—— LangChain框架
人工智能·python·langchain
墨_浅-18 小时前
0帧起手学langchain之马冬梅
langchain
BeforeEasy1 天前
从零搭建一个完整的ai-agent小项目
人工智能·langchain
西柚小萌新2 天前
【人工智能:Agent】--9.2.Langchain自定义中间件
langchain
Loo国昌2 天前
深入理解 FastAPI:Python高性能API框架的完整指南
开发语言·人工智能·后端·python·langchain·fastapi