Skip to content

Commit c820099

Browse files
committed
Some tweaks
1 parent 0567107 commit c820099

File tree

16 files changed

+965
-790
lines changed

16 files changed

+965
-790
lines changed

README.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@
22

33
Agent Memory Server is a high-performance and flexible server for managing
44
short-term and long-term memory for agents using Redis. It provides both REST
5-
API endpoints and an MCP (Managed Control Plane) server interface for robust
5+
API endpoints and an MCP (Model Context Protocol) server interface for robust
66
memory operations in AI applications.
77

88
## Features
99

1010
- **Short-Term Memory**
11-
- Configurable window size for recent messages
12-
- Automatic conversation summarization using LLMs
13-
- Token limit management based on model capabilities
11+
- Storage for messages, token count, context, and metadata for a session
12+
- Automatically and recursively summarizes conversations
13+
- Token limit management based on specific model capabilities
1414

1515
- **Long-Term Memory**
16-
- Semantic search over messages
17-
- Automatic message indexing
18-
- Topic modeling with BERTopic
19-
- Named Entity Recognition using BERT
16+
- Storage for long-term memories across sessions
17+
- Semantic search to retrieve memories, with filters
18+
- Automatic topic modeling for stored memories with BERTopic
19+
- Automatic Entity Recognition using BERT
2020

21-
- **Advanced Features**
21+
- **Other Features**
2222
- Support for OpenAI and Anthropic model providers
2323
- Namespace support for session isolation
24+
- Both a REST interface and MCP server
2425

26+
## Roadmap
27+
- Long-term memory deduplication
28+
- More options for moving session memory to long-term memory
29+
- Auth hooks
30+
- Use a background task system instead of `BackgroundTask`
2531

2632
## REST API Endpoints
2733

@@ -42,7 +48,8 @@ The following endpoints are available:
4248
- `namespace` (string, optional): Filter sessions by namespace.
4349

4450
- **GET /sessions/{session_id}/memory**
45-
Retrieves conversation memory for a session, including messages and context.
51+
Retrieves conversation memory for a session, including messages and
52+
summarized older messages.
4653

4754
- **POST /sessions/{session_id}/memory**
4855
Adds messages (and optional context) to a session's memory.
@@ -52,8 +59,7 @@ The following endpoints are available:
5259
"messages": [
5360
{"role": "user", "content": "Hello"},
5461
{"role": "assistant", "content": "Hi there"}
55-
],
56-
"context": "Optional context"
62+
]
5763
}
5864
```
5965

@@ -70,14 +76,11 @@ The following endpoints are available:
7076
```
7177

7278
## MCP Server Interface
73-
Agent Memory Server also offers an MCP (Model Context Protocol) server interface powered by FastMCP, providing tool-based memory operations:
74-
75-
- **list_sessions**: Retrieve available memory sessions with optional pagination.
76-
- **get_session_memory**: Fetch memory (messages and context) for a specific session.
77-
- **add_memory**: Add messages and context to a session's memory.
78-
- **delete_session_memory**: Remove all memory data for a session.
79-
- **search_memory**: Perform semantic search across session messages.
80-
- **memory_prompt**: Generate prompts enriched with memory context and long-term memories.
79+
Agent Memory Server offers an MCP (Model Context Protocol) server interface powered by FastMCP, providing tool-based long-term memory management:
80+
81+
- **create_long_term_memories**: Store long-term memories.
82+
- **search_memory**: Perform semantic search across long-term memories.
83+
- **memory_prompt**: Generate prompts enriched with session context and long-term memories.
8184

8285
## Getting Started
8386

redis_memory_server/api.py

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
22

3-
from redis_memory_server import messages
3+
from redis_memory_server import long_term_memory, messages
44
from redis_memory_server.config import settings
55
from redis_memory_server.logging import get_logger
66
from redis_memory_server.models import (
77
AckResponse,
8+
CreateLongTermMemoryPayload,
89
GetSessionsQuery,
10+
LongTermMemoryResultsResponse,
911
SearchPayload,
10-
SearchResults,
12+
SessionListResponse,
1113
SessionMemory,
1214
SessionMemoryResponse,
1315
)
14-
from redis_memory_server.utils import (
15-
get_openai_client,
16-
get_redis_conn,
17-
)
16+
from redis_memory_server.utils import get_redis_conn
1817

1918

2019
logger = get_logger(__name__)
2120

2221
router = APIRouter()
2322

2423

25-
@router.get("/sessions/", response_model=list[str])
24+
@router.get("/sessions/", response_model=SessionListResponse)
2625
async def list_sessions(
2726
options: GetSessionsQuery = Depends(),
2827
):
@@ -35,19 +34,20 @@ async def list_sessions(
3534
Returns:
3635
List of session IDs
3736
"""
38-
# TODO: Pydantic should validate this
39-
if options.page > 100:
40-
raise HTTPException(status_code=400, detail="Page must not exceed 100")
41-
4237
redis = get_redis_conn()
4338

44-
return await messages.list_sessions(
39+
total, session_ids = await messages.list_sessions(
4540
redis=redis,
46-
page=options.page,
47-
size=options.size,
41+
limit=options.limit,
42+
offset=options.offset,
4843
namespace=options.namespace,
4944
)
5045

46+
return SessionListResponse(
47+
sessions=session_ids,
48+
total=total,
49+
)
50+
5151

5252
@router.get("/sessions/{session_id}/memory", response_model=SessionMemoryResponse)
5353
async def get_session_memory(
@@ -133,12 +133,36 @@ async def delete_session_memory(
133133
return AckResponse(status="ok")
134134

135135

136-
@router.post("/messages/search", response_model=SearchResults)
137-
async def messages_search(payload: SearchPayload):
136+
@router.post("/long-term-memory", response_model=AckResponse)
137+
async def create_long_term_memory(payload: CreateLongTermMemoryPayload):
138+
"""
139+
Create a long-term memory
140+
141+
Args:
142+
payload: Long-term memory payload
143+
144+
Returns:
145+
Acknowledgement response
146+
"""
147+
redis = get_redis_conn()
148+
149+
if not settings.long_term_memory:
150+
raise HTTPException(status_code=400, detail="Long-term memory is disabled")
151+
152+
await long_term_memory.index_long_term_memories(
153+
redis=redis,
154+
memories=payload.memories,
155+
)
156+
return AckResponse(status="ok")
157+
158+
159+
@router.post("/long-term-memory/search", response_model=LongTermMemoryResultsResponse)
160+
async def search_long_term_memory(payload: SearchPayload):
138161
"""
139-
Run a semantic search on messages
162+
Run a semantic search on long-term memory
140163
141-
TODO: Infer topics for `text`
164+
TODO: Infer topics, entities for `text` and attempt to use them
165+
as boosts or filters in the search.
142166
143167
Args:
144168
payload: Search payload
@@ -149,13 +173,9 @@ async def messages_search(payload: SearchPayload):
149173
redis = get_redis_conn()
150174

151175
if not settings.long_term_memory:
152-
raise HTTPException(status_code=400, detail="Long term memory is disabled")
176+
raise HTTPException(status_code=400, detail="Long-term memory is disabled")
153177

154-
# For embeddings, we always use OpenAI models since Anthropic doesn't support embeddings
155-
client = await get_openai_client()
156-
157-
return await messages.search_messages(
158-
client=client,
159-
redis_conn=redis,
178+
return await long_term_memory.search_long_term_memories(
179+
redis=redis,
160180
**payload.model_dump(exclude_none=True),
161181
)

redis_memory_server/extraction.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def extract_entities(text: str) -> list[str]:
5050
"""
5151
Extract named entities from text using the NER model.
5252
53+
TODO: Cache this output.
54+
5355
Args:
5456
text: The text to extract entities from
5557
@@ -89,6 +91,8 @@ def extract_topics(text: str, num_topics: int | None = None) -> list[str]:
8991
"""
9092
Extract topics from text using the BERTopic model.
9193
94+
TODO: Cache this output.
95+
9296
Args:
9397
text: The text to extract topics from
9498

0 commit comments

Comments
 (0)