Mem4j provides a REST API for managing AI memory operations. The API is built on Spring Boot and provides endpoints for adding, searching, updating, and deleting memories.
http://localhost:8080/api/v1
Currently, the API does not require authentication. In production, you should implement proper authentication and authorization.
POST /memory/add
Add memories from a conversation.
Request Body:
{
"messages": [
{
"role": "user",
"content": "I like pizza and coffee"
},
{
"role": "assistant",
"content": "I'll remember that you like pizza and coffee."
}
],
"userId": "user123",
"metadata": {
"session_id": "session456",
"agent_id": "agent789"
},
"infer": true,
"memoryType": "factual"
}Response:
{
"status": "success",
"message": "Memories added successfully"
}GET /memory/search
Search for relevant memories based on a query.
Parameters:
query(required): Search queryuserId(required): User IDlimit(optional): Maximum number of results (default: 10)threshold(optional): Similarity threshold (default: 0.7)filters(optional): Additional filters
Example:
GET /memory/search?query=What do I like?&userId=user123&limit=5
Response:
{
"status": "success",
"results": [
{
"id": "memory123",
"content": "User likes pizza and coffee",
"memoryType": "factual",
"userId": "user123",
"score": 0.85,
"createdAt": "2024-01-01T12:00:00Z",
"updatedAt": "2024-01-01T12:00:00Z"
}
],
"count": 1
}GET /memory/all
Get all memories for a user.
Parameters:
userId(required): User IDlimit(optional): Maximum number of results (default: 100)filters(optional): Additional filters
Example:
GET /memory/all?userId=user123&limit=50
GET /memory/{memoryId}
Get a specific memory by its ID.
Example:
GET /memory/memory123
Response:
{
"status": "success",
"memory": {
"id": "memory123",
"content": "User likes pizza and coffee",
"memoryType": "factual",
"userId": "user123",
"createdAt": "2024-01-01T12:00:00Z",
"updatedAt": "2024-01-01T12:00:00Z"
}
}PUT /memory/{memoryId}
Update a specific memory.
Request Body:
{
"content": "Updated memory content",
"metadata": {
"updated": true
}
}Response:
{
"status": "success",
"message": "Memory updated successfully"
}DELETE /memory/{memoryId}
Delete a specific memory.
Response:
{
"status": "success",
"message": "Memory deleted successfully"
}DELETE /memory/user/{userId}
Delete all memories for a specific user.
Response:
{
"status": "success",
"message": "All memories deleted successfully"
}POST /memory/reset
Reset all memories (for testing purposes).
Response:
{
"status": "success",
"message": "All memories reset successfully"
}{
"role": "user|assistant|system",
"content": "Message content",
"name": "Optional name",
"functionCall": {
"name": "Function name",
"arguments": "Function arguments"
},
"toolCalls": [
{
"id": "tool123",
"type": "function",
"function": {
"name": "Function name",
"arguments": "Function arguments"
}
}
],
"metadata": {
"key": "value"
}
}{
"id": "memory123",
"content": "Memory content",
"memoryType": "factual|episodic|semantic|procedural|working",
"userId": "user123",
"agentId": "agent456",
"runId": "run789",
"actorId": "actor123",
"metadata": {
"key": "value"
},
"score": 0.85,
"createdAt": "2024-01-01T12:00:00Z",
"updatedAt": "2024-01-01T12:00:00Z",
"embedding": [0.1, 0.2, 0.3, ...]
}All endpoints return error responses in the following format:
{
"status": "error",
"message": "Error description"
}Common HTTP status codes:
200: Success400: Bad Request404: Not Found500: Internal Server Error
Add memories:
curl -X POST http://localhost:8080/api/v1/memory/add \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "I like pizza"},
{"role": "assistant", "content": "I'll remember that!"}
],
"userId": "user123"
}'Search memories:
curl "http://localhost:8080/api/v1/memory/search?query=pizza&userId=user123&limit=5"Delete memory:
curl -X DELETE http://localhost:8080/api/v1/memory/memory123Add memories:
const response = await fetch("http://localhost:8080/api/v1/memory/add", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [
{ role: "user", content: "I like pizza" },
{ role: "assistant", content: "I'll remember that!" },
],
userId: "user123",
}),
});
const result = await response.json();
console.log(result);Search memories:
const response = await fetch(
"http://localhost:8080/api/v1/memory/search?query=pizza&userId=user123"
);
const result = await response.json();
console.log(result.results);Currently, the API does not implement rate limiting. In production, you should implement appropriate rate limiting to prevent abuse.
The application exposes health check endpoints:
GET /actuator/health: Application healthGET /actuator/info: Application informationGET /actuator/metrics: Application metrics
The API behavior can be configured through application properties:
github:
mem4j:
vector-store:
type: inmemory
similarity-threshold: 0.7
llm:
type: openai
model: gpt-4o-mini
embeddings:
type: openai
model: text-embedding-3-small