This is the main Spring Boot application for Mem4j - a long-term memory system for AI agents. The application provides REST APIs for memory operations including adding, searching, updating, and deleting memories.
- Memory Management: Add, search, update, and delete memories
- Vector Search: Semantic search using embeddings
- Multiple Storage Types: Support for in-memory and external vector stores
- LLM Integration: Support for DashScope and OpenAI
- RESTful API: Complete REST API for memory operations
- H2 Database: Embedded database for metadata storage
- Java 17 or higher
- Maven 3.6 or higher
- DashScope API Key (optional, for LLM features)
-
Clone and navigate to the project:
git clone <repository-url> cd mem4j/mem4j-app
-
Set environment variables (optional):
export DASHSCOPE_API_KEY=your-api-key-here
-
Run the application:
mvn spring-boot:run
-
Verify the application is running: The application will start on
http://localhost:8080/api/v1
The application uses application.yml
for configuration. Key configuration sections:
spring:
datasource:
url: jdbc:h2:mem:mem4jdb
driver-class-name: org.h2.Driver
username: sa
password:
mem4j:
vector-store:
type: inmemory # Vector store type: inmemory, qdrant, elasticsearch
collection: memories # Collection name
options:
similarity-threshold: 0.7 # Similarity threshold for search
llm:
type: dashscope # LLM provider: dashscope, openai
api-key: ${DASHSCOPE_API_KEY} # API key from environment variable
model: qwen-turbo # Model name
options:
max-tokens: 1000
temperature: 0.7
embeddings:
type: dashscope # Embedding provider: dashscope, openai
model: text-embedding-v1 # Embedding model
options:
dimensions: 1536 # Embedding dimensions
The application provides the following REST endpoints:
POST /api/v1/memory/add
Content-Type: application/json
{
"messages": [
{
"role": "user",
"content": "I love pizza"
},
{
"role": "assistant",
"content": "That's great! Pizza is delicious."
}
],
"userId": "user123",
"metadata": {
"topic": "food_preferences"
},
"infer": true,
"memoryType": "factual"
}
GET /api/v1/memory/search?query=pizza&userId=user123&limit=10&threshold=0.7
GET /api/v1/memory/all?userId=user123&limit=100
GET /api/v1/memory/{memoryId}
PUT /api/v1/memory/{memoryId}
Content-Type: application/json
{
"content": "Updated content",
"metadata": {
"updated": true
}
}
DELETE /api/v1/memory/{memoryId}
DELETE /api/v1/memory/user/{userId}
POST /api/v1/memory/reset
All API responses follow this format:
Success Response:
{
"status": "success",
"message": "Operation completed successfully",
"results": [...],
"count": 10
}
Error Response:
{
"status": "error",
"message": "Error description"
}
The system supports different types of memories:
- factual: Stores facts and information
- episodic: Stores events and experiences
- semantic: Stores concepts and relationships
- procedural: Stores how-to information
- working: Temporary information for current task
You can test the API using curl, Postman, or any HTTP client:
curl -X POST http://localhost:8080/api/v1/memory/add \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "I work as a software engineer"},
{"role": "assistant", "content": "That sounds like an interesting career!"}
],
"userId": "user123",
"metadata": {"topic": "career"},
"infer": true,
"memoryType": "factual"
}'
curl "http://localhost:8080/api/v1/memory/search?query=software%20engineer&userId=user123&limit=5"
inmemory
: Simple in-memory vector store (default)qdrant
: Qdrant vector databaseelasticsearch
: Elasticsearch vector search
dashscope
: Alibaba Cloud DashScopeopenai
: OpenAI GPT models
dashscope
: Alibaba Cloud DashScope embeddingsopenai
: OpenAI embeddings
Variable | Description | Default |
---|---|---|
DASHSCOPE_API_KEY |
DashScope API key for LLM and embeddings | your-dashscope-api-key |
SPRING_PROFILES_ACTIVE |
Spring profile to activate | default |
mvn clean compile
mvn test
mvn clean package
java -jar target/mem4j-app-0.1.0.jar
mem4j-app/
├── src/
│ └── main/
│ ├── java/
│ │ └── io/github/mem4j/app/
│ │ ├── Mem4jApplication.java # Main application class
│ │ └── controllers/
│ │ └── MemoryController.java # REST API controller
│ └── resources/
│ └── application.yml # Configuration file
├── pom.xml # Maven dependencies
└── README.md # This file
You can override default configuration by:
- Environment variables:
DASHSCOPE_API_KEY
- System properties:
-Dmem4j.vector-store.type=qdrant
- Profile-specific configs:
application-dev.yml
1. DataSource Configuration Error
Failed to configure a DataSource: 'url' attribute is not specified
Solution: This should be resolved in the current version. The H2 database dependency is properly configured.
2. Memory Configuration Binding Error
Failed to bind properties under 'mem4j'
Solution: Ensure your configuration uses the correct mem4j:
prefix (not github.mem4j:
).
3. DashScope API Key Error
API key not configured
Solution: Set the DASHSCOPE_API_KEY
environment variable or update the configuration.
Enable debug logging for troubleshooting:
logging:
level:
io.github.mem4j: DEBUG
org.springframework: INFO
Check if the application is healthy:
curl http://localhost:8080/api/v1/actuator/health
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
For questions and support:
- Create an issue in the GitHub repository
- Check the documentation in the
docs/
directory - Review the API documentation at
docs/API.md