博客

  • LangChain的记忆组件

    第一部分:什么是记忆组件?

    记忆组件是对话系统中的一项关键技术,它允许系统存储和引用对话历史中的信息。在LangChain中,记忆组件可以作为独立工具使用,也可以整合进链式处理中。记忆组件需要支持两个基本操作:读取和写入。

    第二部分:他们是如何工作的?

    在LangChain与LLM的每次交互中,链将执行两次记忆操作:

    1. 读取:在执行核心逻辑之前,链会从记忆系统中读取信息,以此来增强用户输入。
    2. 写入:在核心逻辑执行后、返回答案前,链会将当前的输入和输出写入记忆中,为将来的运行提供参考。

    记忆组件的设计需考虑两个核心问题:如何存储历史信息,以及如何查询这些信息。

    第三部分:三种基础记忆组件详解

    LangChain提供了三种基本记忆组件类型:

    1. ConversationBufferMemory:直接记录对话历史消息的列表。
    2. ConversationBufferWindowMemory:记录对话历史,但仅保留最近的K个交互。
    3. ConversationSummaryMemory:随时间总结对话内容,并存储当前摘要。

    以下是它们的具体用法示例:

    3.1 ConversationBufferMemory

    from langchain.memory import ConversationBufferMemory
    
    memory = ConversationBufferMemory()
    memory.save_context({"input": "Hi, LangChain!"}, {"output": "Hey!"})
    memory.chat_memory.messages  # 查看消息列表

    调用 load_memory_variables 函数可以获取对话历史的文本:

    memory.load_memory_variables({})

    3.2 ConversationBufferWindowMemory

    这个组件使用滑动窗口机制,确保了记忆的容量管理。

    from langchain.memory import ConversationBufferWindowMemory
    
    memory = ConversationBufferWindowMemory(k=1)
    memory.save_context({"input": "Hi, LangChain!"}, {"output": "Hey!"})
    memory.save_context({"input": "Where are you?"}, {"output": "By your side"})
    memory.load_memory_variables({})
    memory.chat_memory.messages

    3.3 ConversationSummaryMemory

    这种记忆类型利用LLM生成对话的摘要,适合长对话。

    from langchain.memory import ConversationSummaryMemory
    from langchain.llms import OpenAI
    
    memory = ConversationSummaryMemory(llm=OpenAI(temperature=0, openai_api_key="您的有效openai api key"))
    memory.save_context({"input": "Hi, LangChain!"}, {"output": "Hey!"})
    memory.save_context({"input": "How to start with Next.js development?"}, {"output": "You can get started with its official developer guide."})
    memory.save_context({"input": "Show me the link of the guide."}, {"output": "I'm looking for you now. Please stand by!"})
    memory.load_memory_variables({})

    随着交互次数的增加,ConversationSummaryMemory的Token开销相对平稳,这对于有许多交互的对话非常有效。

    另外提到的Summary Buffer Memory结合了Summary和Buffer的优点,但本文未详细介绍。

    以上内容提供了对LangChain记忆组件的概览和具体代码示例,您可以通过访问官方文档来学习更多。

  • LangChain 中的链

    链(chain)是 LangChain 中的一个重要概念,它可以将 LLM(大型语言模型)进行串联,或者将链与链之间串联起来。链大大简化了复杂应用程序的实现,并使其模块化,这也使调试、维护和改进应用程序变得更容易。

    基础的链:LLMChain

    最基础的链是 LLMChain,它接受 LLM、提示词模版等组件,然后返回 LLM 的回复。

    实跑代码

    以下是一个使用 LLMChain 的简单示例:

    from langchain.llms import OpenAI
    from langchain.prompts import PromptTemplate
    from langchain.chains import LLMChain
    
    # 创建 LLM 实例
    llm = OpenAI(temperature=0, openai_api_key="YOUR_API_KEY")
    
    # 创建提示词模版
    prompt = PromptTemplate(
        input_variables=["color"],
        template="What is the hex code of color {color}?",
    )
    
    # 创建 LLMChain 实例
    chain = LLMChain(llm=llm, prompt=prompt)
    
    # 基于链提问
    print(chain.run("green"))
    print(chain.run("cyan"))
    print(chain.run("magento"))

    输出:

    The hex code of color green is #00FF00.
    The hex code of color cyan is #00FFFF.
    The hex code for the color Magento is #E13939.

    从 LangChainHub 加载链

    LangChainHub 是一个托管各种 LangChain 组件的项目,其中包括链。您可以使用 load_chain 函数从 LangChainHub 加载链。

    from langchain.chains import load_chain
    import os
    
    # 设置 OpenAI API Key
    os.environ['OPENAI_API_KEY'] = "YOUR_API_KEY"
    
    # 加载链
    chain = load_chain("lc://chains/llm-math/chain.json")
    
    # 基于链提问
    chain.run("whats the area of a circle with radius 2?")

    输出:

    > Entering new LLMMathChain chain...
    whats the area of a circle with radius 2?
    Answer: 12.566370614359172
    > Finished chain.
    
    Answer: 12.566370614359172

    作业

    使用今天学到的用法,自己跑一下代码,然后将结果截图分享出来。

人生梦想 - 关注前沿的计算机技术 acejoy.com 🐾 步子哥の博客 🐾 背多分论坛 🐾 借一步网 沪ICP备2024052574号-1