”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > LangChain部分利用LangChain中的内存和存储:综合指南

LangChain部分利用LangChain中的内存和存储:综合指南

发布于2024-08-25
浏览:566

LangChain Part  Leveraging Memory and Storage in LangChain: A Comprehensive Guide

LangChain 第 4 部分 - 在 LangChain 中利用内存和存储:综合指南

代码可以在这里找到:GitHub - jamesbmour/blog_tutorials:

在对话式人工智能和语言模型不断发展的世界中,维护上下文和有效管理信息流是构建智能应用程序的关键组成部分。 LangChain 是一个强大的框架,专为处理大型语言模型 (LLM) 而设计,为内存管理和数据持久性提供了强大的工具,从而能够创建上下文感知系统。

在本指南中,我们将深入探讨利用 LangChain 中的内存和存储来构建更智能、响应速度更快的应用程序的细微差别。

1. 在 LangChain 中使用内存

LangChain中的内存管理允许应用程序保留上下文,使交互更加连贯和上下文相关。让我们探索不同的内存类型及其用例。

1.1.记忆的类型

LangChain提供了多种内存类型来应对不同的场景。在这里,我们将重点关注两种关键类型:

ConversationBufferMemory

这种记忆类型非常适合短期上下文保留、捕捉和回忆对话中最近的互动。

from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()
memory.save_context({"input": "Hi, I'm Alice"}, {"output": "Hello Alice, how can I help you today?"})
memory.save_context({"input": "What's the weather like?"}, {"output": "I'm sorry, I don't have real-time weather information. Is there anything else I can help you with?"})

print(memory.load_memory_variables({}))

对话摘要内存

对于较长的对话,ConversationSummaryMemory 是一个不错的选择。它总结了要点,保持了上下文,但没有过多的细节。

from langchain.memory import ConversationSummaryMemory
from langchain.llms import Ollama 

llm = Ollama(model='phi3',temperature=0)
memory = ConversationSummaryMemory(llm=llm)
memory.save_context({"input": "Hi, I'm Alice"}, {"output": "Hello Alice, how can I help you today?"})
memory.save_context({"input": "I'm looking for a good Italian restaurant"}, {"output": "Great! I'd be happy to help you find a good Italian restaurant. Do you have any specific preferences or requirements, such as location, price range, or specific dishes you're interested in?"})

print(memory.load_memory_variables({}))

1.2.为您的使用案例选择正确的内存类型

选择合适的内存类型取决于几个因素:

  • 持续时间和复杂性:短会话受益于 ConversationBufferMemory 的详细上下文保留,而长期交互可能需要通过 ConversationSummaryMemory 进行总结。
  • 细节与概述:确定详细的交互历史记录还是高级摘要对您的应用程序更有价值。
  • 性能:考虑内存大小和检索速度之间的权衡。

用例:

  • ConversationBufferMemory:快速客户支持或常见问题解答式交互的理想选择。
  • ConversationSummaryMemory:最适合长期参与,例如项目管理或持续的客户互动。

1.3.将内存集成到链和代理中

内存可以无缝集成到LangChain链和代理中,以增强会话能力。

from langchain.chains import ConversationChain  
from langchain.memory import ConversationBufferMemory
# llm = OpenAI(temperature=0)
memory = ConversationBufferMemory()
conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True
)

conversation.predict(input="Hi, I'm Alice")
conversation.predict(input="What's my name?")

此示例说明了如何使用 ConversationBufferMemory 记住以前的交互,从而实现更自然的对话。

2. 保存和检索数据

持久存储可确保跨会话维护对话历史记录和上下文,从而实现交互的连续性。

2.1.存储对话历史记录和状态

对于基本持久性,您可以将基于文件的存储与 JSON 结合使用:

import json

class PersistentMemory:
    def __init__(self, file_path):
        self.file_path = file_path
        self.load_memory()

    def load_memory(self):
        try:
            with open(self.file_path, 'r') as f:
                self.chat_memory = json.load(f)
        except FileNotFoundError:
            self.chat_memory = {'messages': []}

    def save_memory(self):
        with open(self.file_path, 'w') as f:
            json.dump({'messages': self.chat_memory['messages']}, f)

# Usage
memory = PersistentMemory(file_path='conversation_history.json')
print(memory.chat_memory)

此方法允许您以简单、人类可读的格式保存对话历史记录。

2.2.与数据库和存储系统集成

为了实现更可扩展和更高效的存储,建议与 SQLite 等数据库集成:

import sqlite3

class SQLiteMemory:
    def __init__(self, db_path):
        self.db_path = db_path
        self.conn = sqlite3.connect(db_path)
        self.create_table()

    def create_table(self):
        cursor = self.conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS conversations
            (id INTEGER PRIMARY KEY, input TEXT, output TEXT)
        ''')
        self.conn.commit()

    def save_context(self, inputs, outputs):
        cursor = self.conn.cursor()
        cursor.execute('INSERT INTO conversations (input, output) VALUES (?, ?)',
                       (inputs['input'], outputs['output']))
        self.conn.commit()

    def load_memory_variables(self, inputs):
        cursor = self.conn.cursor()
        cursor.execute('SELECT input, output FROM conversations ORDER BY id DESC LIMIT 10')
        rows = cursor.fetchall()
        history = "\\n".join([f"Human: {row[0]}\\nAI: {row[1]}" for row in reversed(rows)])
        return {"history": history }

# Usage
memory = SQLiteMemory('conversation_history.db')

print(memory.load_memory_variables({}))

3 优化内存使用和性能

为了确保您的应用程序保持响应,请考虑以下优化策略:

  • 高效数据结构:使用双端队列等结构来管理固定大小的缓冲区。
  • 缓存策略:通过对经常访问的数据实施缓存来减少数据库查询。
  • 数据修剪:定期修剪或汇总旧数据以保持可管理的内存大小。

这是具有基本缓存的内存类的示例:

import time

class CachedSQLiteMemory(SQLiteMemory):
    def __init__(self, db_path, cache_ttl=60):
        super().__init__(db_path)
        self.cache = None
        self.cache_time = 0
        self.cache_ttl = cache_ttl

    def load_memory_variables(self, inputs):
        current_time = time.time()
        if self.cache is None or (current_time - self.cache_time) > self.cache_ttl:
            var = self.cache
            self.cache = super().load_memory_variables(inputs)
            self.cache_time = current_time
            return self.cache

memory = CachedSQLiteMemory('conversation_history.db', cache_ttl=30)

此实现会缓存指定时间的数据库查询结果,减少数据库的负载并提高频繁访问内存数据的应用程序的性能。

结论

有效的内存管理是构建智能、上下文感知的对话式人工智能应用程序的基石。 LangChain提供了灵活而强大的内存管理框架,允许开发人员根据特定用例定制内存类型,实现持久存储解决方案,并优化大规模应用程序的性能。

通过选择正确的内存类型、集成持久存储并利用自定义内存类别和缓存策略等先进技术,您可以构建复杂的 AI 系统,即使在规模和复杂性方面也能维护上下文、改善用户体验并高效运行互动增加。

有了这些可用的工具和技术,您就可以充分利用 LangChain 的全部潜力来创建响应式、智能和情境感知的人工智能应用程序。无论您是开发客户支持机器人、虚拟助理还是复杂的对话系统,掌握 LangChain 中的内存和存储都将是您成功的关键因素。

如果您想支持我的写作或给我买啤酒:
https://buymeacoffee.com/bmours

版本声明 本文转载于:https://dev.to/jamesbmour/langchain-part-4-leveraging-memory-and-storage-in-langchain-a-comprehensive-guide-h4m?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • eval()vs. ast.literal_eval():对于用户输入,哪个Python函数更安全?
    eval()vs. ast.literal_eval():对于用户输入,哪个Python函数更安全?
    称量()和ast.literal_eval()中的Python Security 在使用用户输入时,必须优先确保安全性。强大的Python功能Eval()通常是作为潜在解决方案而出现的,但担心其潜在风险。 This article delves into the differences betwee...
    编程 发布于2025-06-22
  • 为什么在我的Linux服务器上安装Archive_Zip后,我找不到“ class \” class \'ziparchive \'错误?
    为什么在我的Linux服务器上安装Archive_Zip后,我找不到“ class \” class \'ziparchive \'错误?
    Class 'ZipArchive' Not Found Error While Installing Archive_Zip on Linux ServerSymptom:When attempting to run a script that utilizes the ZipAr...
    编程 发布于2025-06-22
  • 如何从Google API中检索最新的jQuery库?
    如何从Google API中检索最新的jQuery库?
    从Google APIS 问题中提供的jQuery URL是版本1.2.6。对于检索最新版本,以前有一种使用特定版本编号的替代方法,它是使用以下语法:获取最新版本:未压缩)While these legacy URLs still remain in use, it is recommended ...
    编程 发布于2025-06-22
  • 如何有效地选择熊猫数据框中的列?
    如何有效地选择熊猫数据框中的列?
    在处理数据操作任务时,在Pandas DataFrames 中选择列时,选择特定列的必要条件是必要的。在Pandas中,选择列的各种选项。选项1:使用列名 如果已知列索引,请使用ILOC函数选择它们。请注意,python索引基于零。 df1 = df.iloc [:,0:2]#使用索引0和1 c...
    编程 发布于2025-06-22
  • 如何干净地删除匿名JavaScript事件处理程序?
    如何干净地删除匿名JavaScript事件处理程序?
    删除匿名事件侦听器将匿名事件侦听器添加到元素中会提供灵活性和简单性,但是当要删除它们时,可以构成挑战,而无需替换元素本身就可以替换一个问题。 element? element.addeventlistener(event,function(){/在这里工作/},false); 要解决此问题,请考虑...
    编程 发布于2025-06-22
  • PHP与C++函数重载处理的区别
    PHP与C++函数重载处理的区别
    作为经验丰富的C开发人员脱离谜题,您可能会遇到功能超载的概念。这个概念虽然在C中普遍,但在PHP中构成了独特的挑战。让我们深入研究PHP功能过载的复杂性,并探索其提供的可能性。在PHP中理解php的方法在PHP中,函数超载的概念(如C等语言)不存在。函数签名仅由其名称定义,而与他们的参数列表无关。...
    编程 发布于2025-06-22
  • 人脸检测失败原因及解决方案:Error -215
    人脸检测失败原因及解决方案:Error -215
    错误处理:解决“ error:((-215)!empty()in Function Multultiscale中的“ openCV 要解决此问题,必须确保提供给HAAR CASCADE XML文件的路径有效。在提供的代码片段中,级联分类器装有硬编码路径,这可能对您的系统不准确。相反,OPENCV提...
    编程 发布于2025-06-22
  • `console.log`显示修改后对象值异常的原因
    `console.log`显示修改后对象值异常的原因
    foo = [{id:1},{id:2},{id:3},{id:4},{id:id:5},],]; console.log('foo1',foo,foo.length); foo.splice(2,1); console.log('foo2', foo, foo....
    编程 发布于2025-06-22
  • 如何修复\“常规错误:2006 MySQL Server在插入数据时已经消失\”?
    如何修复\“常规错误:2006 MySQL Server在插入数据时已经消失\”?
    How to Resolve "General error: 2006 MySQL server has gone away" While Inserting RecordsIntroduction:Inserting data into a MySQL database can...
    编程 发布于2025-06-22
  • 如何简化PHP中的JSON解析以获取多维阵列?
    如何简化PHP中的JSON解析以获取多维阵列?
    php 试图在PHP中解析JSON数据的JSON可能具有挑战性,尤其是在处理多维数组时。 To simplify the process, it's recommended to parse the JSON as an array rather than an object.To do...
    编程 发布于2025-06-22
  • 为什么Microsoft Visual C ++无法正确实现两台模板的实例?
    为什么Microsoft Visual C ++无法正确实现两台模板的实例?
    在Microsoft Visual C 中,Microsoft consions用户strate strate strate strate strate strate strate strate strate strate strate strate strate strate strate st...
    编程 发布于2025-06-22
  • 解决Spring Security 4.1及以上版本CORS问题指南
    解决Spring Security 4.1及以上版本CORS问题指南
    弹簧安全性cors filter:故障排除常见问题 在将Spring Security集成到现有项目中时,您可能会遇到与CORS相关的错误,如果像“访问Control-allo-allow-Origin”之类的标头,则无法设置在响应中。为了解决此问题,您可以实现自定义过滤器,例如代码段中的MyFi...
    编程 发布于2025-06-22
  • PHP未来:适应与创新
    PHP未来:适应与创新
    PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。 引言在编程世界中,PHP一直是网页开发的中流砥柱。作为一个从1994年就开始发展...
    编程 发布于2025-06-22
  • 左连接为何在右表WHERE子句过滤时像内连接?
    左连接为何在右表WHERE子句过滤时像内连接?
    左JOIN CONUNDRUM:WITCHING小时在数据库Wizard的领域中变成内在的加入很有趣,当将c.foobar条件放置在上面的Where子句中时,据说左联接似乎会转换为内部连接。仅当满足A.Foo和C.Foobar标准时,才会返回结果。为什么要变形?关键在于其中的子句。当左联接的右侧值...
    编程 发布于2025-06-22

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3