Skip to content

ffch/memsix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License JDK 17+ Maven Central

Memsix项目简介

Memsix 参考reme实现的长期记忆(Long-Term Memory)SDK,基于 Java 17 实现。 它通过文件存储和向量检索技术,为智能体提供跨会话的持久化记忆能力,让智能体能够记住用户偏好、历史对话事实等关键信息, 从而在对话中表现出更连贯、个性化的行为。

Memsix 实现了 AgentScope 的 LongTermMemory 接口,可以无缝集成到 AgentScope 的智能体工作流中。

版本说明

0.0.2

  • 优化文件存储模式,解决记忆重复问题。
  • 增加向量搜索功能,提升检索效率和准确性。
  • 支持es向量库,提供更高效的向量存储和检索能力。
  • 优化提示词,解决部分记忆膨胀问题。
  • 筹备独立部署版本和docker镜像.

0.0.1

  • 初始版本,支持文件存储模式的长期记忆功能,并提供了 AgentScope 集成的实现类 MemSixForAgentScopeLongTermMemory。
  • 支持文件系统和mysql存储两种模式,向量搜索功能处于测试阶段,暂未正式发布。
  • 存在记忆重复问题。

主要功能

  • 文件存储模式:将记忆数据持久化到本地文件系统,支持自定义工作目录和工作空间(workspace),方便多用户隔离管理。
  • 向量搜索支持:可选配置嵌入模型,将文本转化为向量并存储,实现基于语义的相似记忆检索,提升记忆召回的准确性。
  • 集成 AgentScope:提供 MemSixForAgentScopeLongTermMemory 类,直接实现 AgentScope 的 LongTermMemory 接口,可作为智能体的长期记忆组件使用。
  • 向量库的支持:筹备中,计划支持多种向量数据库(如 ES、PGVector 等),提供更高效的向量存储和检索能力。
  • 独立部署及docker部署:筹备中,计划提供独立部署的服务版本,并提供docker镜像,方便用户在不同环境中使用。

使用说明

jar包已经上传到maven中央仓库。 https://search.maven.org/search?q=memsix ,groupId为cn.pomit。

使用文档地址

maven依赖

<dependency>
	<groupId>cn.pomit</groupId>
	<artifactId>memsix</artifactId>
	<version>0.0.1</version>
</dependency>

整合进AgentScope中

配置MemSixConfig,交给MemSixForAgentScopeLongTermMemoryMemSixForAgentScopeLongTermMemory实现了AgentScope的LongTermMemory接口:

String llmModelName = "qwen3-max-preview";
String llmBaseUrl = "https://xxx";
String llmApiKey = "xxxx";
String embeddingModelName = "Qwen3-Embedding-0.6B";
String embeddingBaseUrl = "https://xxx";
String embeddingApiKey = "xxx";
MemSixConfig memSixConfig = MemSixConfig.fileMode()
        .workDirectory("./memsix")
        .workspaceId("meimei_sunday")
        .chatModelConfig(ChatModelConfig.builder().apiKey(llmApiKey)
                .baseUrl(llmBaseUrl)
                .modelName(llmModelName)
                .build())
        .enableVectorSearch(true)
        .embedModelConfig(EmbedModelConfig.builder()
                .modelName(embeddingModelName)
                .baseUrl(embeddingBaseUrl)
                .apiKey(embeddingApiKey)
                .build())
        .build();
MemSixForAgentScopeLongTermMemory longTermMemory = new MemSixForAgentScopeLongTermMemory(memSixConfig);

以上配置中

  • fileMode()会在当前目录下创建一个memsix文件夹(可配置),作为工作目录。支持bm25 + 向量 混合检索。
  • mysqlMode()使用jdbcTemplate操作mysql数据库进行全文检索。支持全文检索+ 向量 混合检索。
  • workspaceId指定了工作空间id(可用以区分用户)
  • 聊天模型配置必填。
  • 向量搜索配置可选,如果需要使用向量搜索,必须提供向量搜索模型的配置。

mysql模式需要表支持,默认表结构:

CREATE TABLE `agent_user_memory` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `content` longtext COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '记忆内容',
  `workspace_id` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '用户标识',
  `tag` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '分类标识',
  `embeddings` text COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '向量',
  `create_time` datetime NOT NULL DEFAULT current_timestamp() COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `agent_user_memory_workspace_id_IDX` (`workspace_id`,`tag`) USING BTREE,
  FULLTEXT KEY `agent_user_memory_content_IDX` (`content`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

注意:

  1. 需要有唯一索引进行表的更新和插入操作,workspace_idtag的组合作为唯一索引,确保同一工作空间下相同标签的记忆只能有一条记录。
  2. 可以通过自定义summarySqlTemplate和dailySqlTemplate修改为自定义SQL.
  3. useOneTableMode为true时,表示summary和daily用同一个表。daily无需额外配置。

调用方法

直接放到AgentScope的longTermMemory调用链中:

.longTermMemory(longTermMemory)

示例:

// 注册数据库更新工具
ReActAgent agent = ReActAgent.builder()
        .name(memSixConfig.getWorkspaceId() + "的长期记忆助理")
        .sysPrompt("你是有帮助的AI助理。")
        .model(OpenAIChatModel.builder()
        .apiKey(llmApiKey)
        .baseUrl(llmBaseUrl)
        .generateOptions(GenerateOptions.builder()
        .temperature(0.7)
        .build())
        .stream(true)
        .modelName(llmModelName)
        .build())
        .maxIters(10)
        .longTermMemory(longTermMemory)
        .build();

版权声明

memsix使用 Apache License 2.0 协议.

作者信息

个人网站:https://www.pomit.cn

作者邮箱: pomitcn@163.com/916881512@qq.com

License

Apache License V2

About

A long-term memory project based on file systems and vector search.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages