• langchain主要模块(四):Memory


    原文:langchain主要模块(四):Memory_江小皮不皮的博客-CSDN博客
    langchain2之Memory

    langchain

    1.概念

    什么是LangChain?

    源起:LangChain产生源于Harrison与领域内的一些人交谈,这些人正在构建复杂的LLM应用,他在开发方式

    上看到了一些可以抽象的部分。一个应用可能需要多次提示LLM并解析其输出,因此需要编写大量的复制粘贴

    LangChain使这个开发过程更加简单。一经推出后,在社区被广泛采纳,不仅有众多用户,还有许多贡献者参

    与开源工作。

    还有大模型本身的问题,无法感知实时数据,无法和当前世界进行交互。

    LangChain是一个用于开发大语言模型的框架。

    主要特性:

    \1. 数据感知:能够将语⾔模型与其他数据源进⾏连接。

    \2. 代理性:允许语⾔模型与其环境进⾏交互。可以通过写⼯具的⽅式做各种事情,数据的写⼊更新。

    主要价值:

    1、组件化了需要开发LLM所需要的功能,提供了很多工具,方便使用。

    2、有一些现成的可以完整特定功能的链,也可以理解为提高了工具方便使用。

    2.主要模块

    LangChain 为以下模块提供了标准、可扩展的接口和外部集成,按照复杂程度从低到高列出:

    模型输入/输出 (Model I/O)

    语言模型进行接口交互

    数据连接 (Data connection)

    与特定于应用程序的数据进行接口交互

    链式组装 (Chains)

    构造调用序列

    代理 (Agents)

    根据高级指令让链式组装选择要使用的工具

    内存 (Memory)

    在链式组装的多次运行之间持久化应用程序状态

    回调 (Callbacks)

    记录和流式传输任何链式组装的中间步骤

    3.Memory

    默认情况下,链式模型和代理模型都是无状态的,这意味着它们将每个传入的查询独立处理(就像底层的 LLMs 和聊天模型本身一样)。在某些应用程序中,比如聊天机器人,记住先前的交互是至关重要的。无论是短期还是长期,都要记住先前的交互。Memory 类正是做到了这一点。 LangChain 提供了两种形式的记忆组件。首先,LangChain 提供了用于管理和操作以前的聊天消息的辅助工具。这些工具被设计成模块化的,无论如何使用都很有用。其次,LangChain 提供了将这些工具轻松整合到链式模型中的方法。

     
    
    1. from langchain.memory import ConversationBufferMemory
    2. from langchain.chains import ConversationChain
    3. conversation = ConversationChain(
    4. llm=model,
    5. verbose=True,
    6. memory=ConversationBufferMemory()
    7. )
     
    
    conversation.predict(input="你好啊!,我是张三")
    

    conversation.predict(input="你知道我的姓名吗")
    
     
    
    ConversationBufferMemory

    1. memory = ConversationBufferMemory()
    2. memory.save_context({"input": "你好啊!,我是张三"},
    3. {"output": "你好,张三!很高兴认识你!你有什么问题想要问我吗?"})
    4. memory.save_context({"input": "你知道我的姓名吗"},
    5. {"output": "当然知道!您叫做张三。请问有什么我可以帮助您的?"})
    memory.load_memory_variables({})
    

    {‘history’: ‘Human: 你好啊!,我是张三\nAI: 你好,张三!很高兴认识你!你有什么问题想要问我吗?\nHuman: 你知道我的姓名吗\nAI: 当然知道!您叫做张三。请问有什么我可以帮助您的?’}

    ConversationBufferWindowMemory

    通过k控制记忆数量

    1. from langchain.memory import ConversationBufferWindowMemory
    2. memory = ConversationBufferWindowMemory(k=1)
    3. memory.save_context({"input": "你好啊!,我是张三"},
    4. {"output": "你好,张三!很高兴认识你!你有什么问题想要问我吗?"})
    5. memory.save_context({"input": "你知道我的姓名吗"},
    6. {"output": "当然知道!您叫做张三。请问有什么我可以帮助您的?"})
    memory.load_memory_variables({})
    

    {‘history’: ‘Human: 你知道我的姓名吗\nAI: 当然知道!您叫做张三。请问有什么我可以帮助您的?’}

    ConversationTokenBufferMemory

    保留token数量

    1. from langchain.memory import ConversationTokenBufferMemory
    2. memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=20)
    3. memory.save_context({"input": "AI is what?!"},
    4. {"output": "Amazing!"})
    5. memory.save_context({"input": "Backpropagation is what?"},
    6. {"output": "Beautiful!"})
    7. memory.save_context({"input": "Chatbots are what?"},
    8. {"output": "Charming!"})
    memory.load_memory_variables({})
    

    {‘history’: ‘AI: Beautiful!\nHuman: Chatbots are what?\nAI: Charming!’}

    ConversationSummaryMemory

    总结对话中的内容

    1. from langchain.memory import ConversationSummaryBufferMemory
    2. # create a long string
    3. schedule = "There is a meeting at 8am with your product team. \
    4. You will need your powerpoint presentation prepared. \
    5. 9am-12pm have time to work on your LangChain \
    6. project which will go quickly because Langchain is such a powerful tool. \
    7. At Noon, lunch at the italian resturant with a customer who is driving \
    8. from over an hour away to meet you to understand the latest in AI. \
    9. Be sure to bring your laptop to show the latest LLM demo."
    10. memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=100)
    11. memory.save_context({"input": "Hello"}, {"output": "What's up"})
    12. memory.save_context({"input": "Not much, just hanging"},
    13. {"output": "Cool"})
    14. memory.save_context({"input": "What is on the schedule today?"},
    15. {"output": f"{schedule}"})
    memory.load_memory_variables({})
    

    {‘history’: ‘System: The human asks the AI what is on the schedule today. The AI responds that it is not currently set up to provide a schedule.\nAI: There is a meeting at 8am with your product team. You will need your powerpoint presentation prepared. 9am-12pm have time to work on your LangChain project which will go quickly because Langchain is such a powerful tool. At Noon, lunch at the italian resturant with a customer who is driving from over an hour away to meet you to understand the latest in AI. Be sure to bring your laptop to show the latest LLM demo.’}

    人工智能langchainChatGLM2-6BAIGCMemory

    上一篇:langchain主要模块(三):Chain

    下一篇:langchain主要模块(五):Agent以及Wandb

  • 相关阅读:
    7-1 后序和中序构造二叉树
    搜索引擎项目
    VBA技术资料MF64:遍历单元格搜索字符并高亮显示
    C++--Linux基础使用
    Java集合collection map stream流
    Google Chrome 浏览器以全屏模式打开
    IDEA的模板(Templates)
    以太网 TCP协议(三次握手、四次挥手)
    「UG/NX」Block UI 从列表选择部件SelectPartFromList
    MySQL压缩包方式安装,傻瓜式教学
  • 原文地址:https://blog.csdn.net/javastart/article/details/133887059