【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年环法自行车赛温格高的表现有哪些亮点?"
    ]
相关推荐
营赢盈英2 天前
How can I change from OpenAI to ChatOpenAI in langchain and Flask?
python·langchain·flask·openai api·py-langchain
营赢盈英8 天前
Question mutiple pdf‘s using openai, pinecone, langchain
python·langchain·pdf·pinecone·openai api
笑笑布丁9 天前
langchain入门系列之六 使用langchain构建PDF解析助手
chatgpt·langchain·pdf
AI大模型_学习君10 天前
大模型技术 | 基于 Langchain 和 Streamlit,构建多 PDF RAG 聊天机器人
人工智能·langchain·机器人·pdf·大语言模型·ai大模型·rag
小陈phd14 天前
大模型从入门到精通——基于智谱AI和LangChain实现RAG应用(一)
人工智能·python·langchain
AI-智能14 天前
《Llama 3大模型》技术报告中英文版,95页pdf
人工智能·ai·langchain·大模型·llm·prompt·ai大模型
黑金IT15 天前
构建高效NLP管道:PydanticOutputParser与Langchain的结合
python·langchain·ai编程
Coding Happily15 天前
Openai API + langchain 分析小型pdf文档
python·langchain·pdf
我爱学Python!17 天前
Langchain[3]:Langchain架构演进与功能扩展:流式事件处理、事件过滤机制、回调传播策略及装饰器应用
人工智能·microsoft·langchain·大语言模型·embedding·ai大模型·计算机技术
AI小白龙*17 天前
这本书已经无敌!一本书学懂NLP自然语言(附PDF文档)
人工智能·自然语言处理·langchain·大模型·llm·prompt·github