的 Chat Store 集成)
LlamaIndex TablestoreChatStore把聊天历史持久化到阿里云表格存储Tablestore的 Chat Store 集成【免费下载链接】llama_indexLlamaIndex is the leading document agent and OCR platform项目地址: https://gitcode.com/GitHub_Trending/ll/llama_index本篇技术指南围绕 LlamaIndex 的TablestoreChatStore展开——这是llama-index-storage-chat-store-tablestore集成包提供的聊天历史存储实现将 LlamaIndex 对话记忆ChatMemoryBuffer的会话历史以 JSON 形式写入阿里云表格存储Tablestore/OTS。读完本篇你将掌握该包的完整安装方式、构造函数参数、底层表结构与数据模型、全部 API 方法的源码级实现细节以及如何将其接入聊天记忆流并用集成测试验证。安装与包信息该集成包位于 llama-index-integrations/storage/chat_store/llama-index-storage-chat-store-tablestore其 pyproject.toml 声明了如下关键事实包名llama-index-storage-chat-store-tablestore当前版本0.3.0MIT 协议Python 要求3.10,4.0依赖llama-index-core0.13.0,0.15与tablestore6.1.0阿里云表格存储官方 SDK。安装方式pip install llama-index-storage-chat-store-tablestore快速上手五步接入聊天记忆集成包 README.md 给出的标准用法如下from llama_index.storage.chat_store.tablestore import TablestoreChatStore from llama_index.core.memory import ChatMemoryBuffer # 1. create tablestore vector store chat_store TablestoreChatStore( endpointend_point, instance_nameinstance_name, access_key_idaccess_key_id, access_key_secretaccess_key_secret, ) # You need to create a table for the first use chat_store.create_table_if_not_exist() chat_memory ChatMemoryBuffer.from_defaults( token_limit3000, chat_storechat_store, chat_store_keyuser1, )流程分三步构造TablestoreChatStore传入 Tablestore 实例的 endpoint、实例名与阿里云 AccessKey首次使用前建表调用create_table_if_not_exist()幂等地创建存储表接入ChatMemoryBuffer通过chat_store注入存储后端chat_store_keyuser1指定会话主键token_limit3000限制送入 LLM 的历史 token 上限。此后ChatMemoryBuffer的每次追加、裁剪都会自动落盘无需手动持久化或加载聊天历史。API 参考页由 mkdocstrings 自动生成见 docs/api_reference/api_reference/storage/chat_store/tablestore.md其内容直接指向llama_index.storage.chat_store.tablestore模块中的TablestoreChatStore类。TablestoreChatStore 构造函数参数构造函数定义于 base.py 第 50-73 行。参数说明如下取自类 docstring 与源码签名参数类型默认值说明tablestore_clienttablestore.OTSClientNone外部已有的 OTS 客户端。若显式传入则 endpoint/instance_name/access_key_id/access_key_secret 全部被忽略endpointstrNoneTablestore 实例 endpointinstance_namestrNoneTablestore 实例名access_key_idstrNone阿里云 AccessKey IDaccess_key_secretstrNone阿里云 AccessKey Secrettable_namestrllama_index_chat_store_v1存储表名**kwargsAny—透传给tablestore.OTSClient的额外参数实现上未传入现成客户端时内部以retry_policytablestore.WriteRetryPolicy()构造OTSClientbase.py 第 63-71 行即写操作默认启用 Tablestore SDK 的写重试策略若传入tablestore_client则直接复用该客户端base.py 第 72-73 行这为复用已有连接池或自定义认证如 STS 临时凭证留出了口子。表结构与数据模型从源码结构看该存储采用一行一会话的极简模型base.py 第 45-48 行主键列_primary_key session_id类型为STRING即chat_store_key如user1属性列_history_column history存放整段会话历史。create_table_if_not_exist()的建表逻辑base.py 第 75-98 行table_meta tablestore.TableMeta( self.table_name, [(self._primary_key, STRING)] ) reserved_throughput tablestore.ReservedThroughput( tablestore.CapacityUnit(0, 0) ) self._tablestore_client.create_table( table_meta, tablestore.TableOptions(), reserved_throughput )要点先调用list_table()检查表名是否存在存在则仅打日志返回保证幂等预留读写吞吐为CapacityUnit(0, 0)即使用按量付费CU 按量模式建表。消息序列化发生在读写边界base.py 第 13-24 行写入时将List[ChatMessage]逐条转为 dict 后json.dumps(..., ensure_asciiFalse)整体序列化读取时用json.loads反序列化并逐条ChatMessage.model_validate(d)还原为 Pydantic 模型。ensure_asciiFalse保证中文等多字节内容以原文落盘——集成测试中专门用ChatMessage(contentTablestore 第三 message, ...)验证了这一点test_chat_store_tablestore_chat_store.py 第 54-68 行。API 方法逐一解析TablestoreChatStore继承自核心包的BaseChatStorellama-index-core/llama_index/core/storage/chat_store/base.py实现了其全部 7 个抽象方法并额外提供create_table_if_not_exist()与clear_store()两个 Tablestore 专属操作。set_messages整行覆盖写入base.py 第 110-131 行 使用put_row原子写入同一session_id下已有的历史会被整体覆盖primary_key [(self._primary_key, key)] attribute_columns [ ( self._history_column, json.dumps(_messages_to_dict(messages), ensure_asciiFalse), ), ] row tablestore.Row(primary_key, attribute_columns) self._tablestore_client.put_row(self.table_name, row)get_messages按主键单行读取base.py 第 133-156 行 调用get_row(table, primary_key, None, None, 1)max_version1只取最新版本遍历row.attribute_columns找到history列并反序列化。行不存在时返回空列表不抛异常。add_message读-改-写base.py 第 158-173 行 的逻辑是get_messages取当前历史 →append(message)→set_messages回写。这里可以推断由于底层是整行覆盖而非列追加高并发下对同一session_id的并发追加存在读改写竞争单会话串行对话场景不受影响。delete 系列整行删除、按索引删除、删除末条delete_messages(key)base.py 第 175-190 行先get_messages取回消息再delete_row删除整行并返回被删消息delete_message(key, idx)base.py 第 192-215 行读改写删除指定索引的消息索引越界时记录logger.error并返回Nonedelete_last_message(key)base.py 第 217-229 行委托为delete_message(key, -1)即删除最后一条——这是ChatMemoryBuffer在历史超出 token 上限时裁剪记忆所用的方法clear_store()base.py 第 100-104 行遍历get_keys()逐行delete_messages清空全表。get_keys基于 get_range 的分页扫描base.py 第 231-281 行 用 Tablestore 的范围读枚举全部会话键起止主键为[(session_id, INF_MIN)]到[(session_id, INF_MAX)]方向Direction.FORWARD每批limit5000、max_version1通过next_start_primary_key非空循环翻页直至扫完只读取主键值columns_to_get[]逐行取row.primary_key[0][1]收集为键列表。与 ChatMemoryBuffer 的协同关系ChatMemoryBuffer定义于 llama-index-core/llama_index/core/memory/chat_memory_buffer.py其from_defaults(token_limit..., chat_store..., chat_store_key...)将本集成作为持久化后端。核心包BaseChatStore同时提供aset_messages、aget_messages等异步方法base.py 第 52-78 行通过asyncio.to_thread将同步实现包装为异步因此TablestoreChatStore即使没有原生 async I/O也能被异步记忆流程调用。集成测试与本地验证端到端测试位于 tests/test_chat_store_tablestore_chat_store.py。要点测试依赖真实 Tablestore 实例凭据来自环境变量tablestore_end_point、tablestore_instance_name、tablestore_access_key_id、tablestore_access_key_secret任一缺失时pytest.skip跳过第 14-40 行每个用例先create_table_if_not_exist()再clear_store()保证干净状态覆盖用例包括test_add_message、test_set_and_retrieve_messages含中文内容断言、test_delete_messages、test_delete_specific_message、test_get_keys、test_delete_last_message、test_clear_store与上文 API 方法一一对应可直接作为回归基线。相关存储组件仓库中 Tablestore 的存储类集成不止 Chat Store同一存储家族还包括llama-index-vector-stores-tablestore向量存储llama-index-storage-kvstore-tablestoreKV 存储llama-index-storage-docstore-tablestore文档存储llama-index-storage-index-store-tablestore索引存储。这些集成在 mkdocs 配置中统一登记于 docs/api_reference/mkdocs.yml 第 511-570 行。若你的 RAG 系统希望将向量、文档、索引与聊天记忆统一落在 Tablestore 上可按本文模式逐一接入其中本文覆盖的TablestoreChatStore负责多轮对话状态的远程持久化。小结TablestoreChatStore以主键session_id JSON 列history的极简表模型把BaseChatStore的 7 个抽象方法完整映射到 Tablestore 的put_row/get_row/delete_row/get_range原语上并提供幂等建表能力。接入时只需四个连接参数加一次create_table_if_not_exist()即可让ChatMemoryBuffer的会话历史跨进程、跨重启持久化实现细节、参数默认值与行为边界均可在上述源码与测试文件中逐行核对。【免费下载链接】llama_indexLlamaIndex is the leading document agent and OCR platform项目地址: https://gitcode.com/GitHub_Trending/ll/llama_index创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考