AS AgentScopeAPI 手册 / API Reference

Mem0LongTermMemory

public class Mem0LongTermMemory implements LongTermMemory
源码Source io/agentscope/core/memory/mem0/Mem0LongTermMemory.java:113–439

Mem 0 Long Term 记忆,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.core.memory.mem0 包,实现 LongTermMemory。核心成员包括 record、retrieve、builder。

Long-term memory implementation using Mem0 as the store.

This implementation integrates with Mem0, a memory layer for AI applications that provides persistent, searchable memory storage using vector embeddings and LLM-powered memory extraction.

Key Features:

  • Semantic memory search using vector embeddings

  • LLM-powered memory extraction and inference

  • Multi-tenant memory isolation (agent, user, run)

  • Custom metadata support for tagging and filtering memories

  • Automatic fallback mechanisms to ensure reliable memory storage

  • Reactive, non-blocking operations

Memory Isolation: Memories are organized using three levels of metadata:

  • agentId: Identifies the agent (optional)

  • userId: Identifies the user/workspace (optional)

  • runId: Identifies the session/run (optional)

  • metadata: Custom key-value pairs for additional filtering (optional)

At least one identifier is required. During retrieval, only memories with matching metadata are returned.

Usage Example:

`// Create memory instance with authentication
Mem0LongTermMemory memory = Mem0LongTermMemory.builder()
.agentName("Assistant")
.userId("user_123")
.apiBaseUrl("http://localhost:8000")
.apiKey(System.getenv("MEM0_API_KEY"))
.build();

// For local deployments without authentication
Mem0LongTermMemory localMemory = Mem0LongTermMemory.builder()
.agentName("Assistant")
.userId("user_123")
.apiBaseUrl("http://localhost:8000")
.build();

// For self-hosted Mem0
Mem0LongTermMemory selfHostedMemory = Mem0LongTermMemory.builder()
.agentName("Assistant")
.userId("user_123")
.apiBaseUrl("http://localhost:8000")
.apiType(Mem0ApiType.SELF_HOSTED) // Specify self-hosted API type
.build();

// With custom metadata for filtering
Map<String, Object> metadata = new HashMap<>();
metadata.put("category", "travel");
metadata.put("project_id", "proj_001");

Mem0LongTermMemory memoryWithMetadata = Mem0LongTermMemory.builder()
.agentName("Assistant")
.userId("user_123")
.apiBaseUrl("http://localhost:8000")
.metadata(metadata) // Custom metadata for storage and filtering
.build();

// Record a message (metadata will be stored with the memory)
Msg msg = Msg.builder()
.role(MsgRole.USER)
.content("I prefer homestays when traveling")
.build();

memory.record(List.of(msg)).block();

// Retrieve relevant memories (metadata will be used as filter)
Msg query = Msg.builder()
.role(MsgRole.USER)
.content("What are my travel preferences?")
.build();

String memories = memory.retrieve(query).block();
// Result: "User prefers homestays when traveling"
`

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
Builder类class
Mem0LongTermMemory的构建器。
Builder for Mem0LongTermMemory.

方法摘要Method Summary

方法Method说明Description
public Mono<Void> record(List<Msg> msgs)
设置记录。
Records messages to long-term memory.
public Mono<String> retrieve(Msg msg)
按查询检索相关文档。
Retrieves relevant memories based on the input message.
public static Builder builder()
static
创建新的构建器用于 Mem0LongTermMemory。
Creates a new builder for Mem0LongTermMemory.

方法详情Method Detail

record

public Mono<Void> record(List<Msg> msgs)

设置记录。

Records messages to long-term memory.

参数Parameters
名称Name类型Type说明Description
msgsList<Msg>列出的消息到。List of messages to record
返回Returns Mono<Void>
返回Mono。A Mono that completes when recording is finished
源码Source io/agentscope/core/memory/mem0/Mem0LongTermMemory.java:169–204

retrieve

public Mono<String> retrieve(Msg msg)

按查询检索相关文档。

Retrieves relevant memories based on the input message.

参数Parameters
名称Name类型Type说明Description
msgMsg消息到使用 as 搜索查询。The message to use as a search query
返回Returns Mono<String>
返回Mono。A Mono emitting the retrieved memory text (may be empty)
源码Source io/agentscope/core/memory/mem0/Mem0LongTermMemory.java:268–292

builder

public static Builder builder()
static

创建新的构建器用于 Mem0LongTermMemory。

Creates a new builder for Mem0LongTermMemory.

返回Returns Builder
一种新的构建器实例。A new builder instance
源码Source io/agentscope/core/memory/mem0/Mem0LongTermMemory.java:299–301

嵌套类详情Nested Class Detail

Builder

public static class Builder

Mem0LongTermMemory的构建器。位于 io.agentscope.core.memory.mem0 包。核心成员包括 agentName、userId、runName、apiBaseUrl、apiKey、timeout。

Builder for Mem0LongTermMemory.

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.