Author: Ayman Taha
License: MIT
Version: 1.1.0
β οΈ Note: This project provides a foundational memory layer,
not a fully autonomous cognitive memory system.
- Overview
- Architecture
- Features
- Installation
- Configuration
- API Documentation
- Deployment
- Environment Variables
- Development
- Contributing
MEMORAI is a persistent AI memory server that acts as middleware between client applications and LLM providers. It mitigates stateless LLM limitations by storing user profile data and conversation memory, then injecting relevant context during chat generation.
Large Language Models are powerful, but commonly face:
| Challenge | MEMORAI Solution |
|---|---|
| Stateless Nature | Persistent user identity and conversation history |
| Limited Context Window | Memory retrieval and contextual injection |
| No Personalization by Default | User preferences and custom instructions |
| Context Drift Over Time | Memory pruning and relevance filtering |
graph TB
subgraph "Client Applications"
A["Web Apps"]
B["Mobile Apps"]
C["CLI Tools"]
end
subgraph "MEMORAI Core"
D["API Gateway"]
E["API Key Authentication"]
F["User Manager"]
G["Memory Engine"]
H["Context Builder"]
I["Provider Router"]
J["Response Processor"]
end
subgraph "Storage Layer"
K["SQLite/PostgreSQL"]
M["Memory Index (SQL)"]
end
subgraph "LLM Providers"
N["OpenAI"]
O["Qwen"]
P["DeepSeek"]
end
A --> D
B --> D
C --> D
D --> E
E --> F
F --> G
G --> H
H --> I
I --> N
I --> O
I --> P
N --> J
O --> J
P --> J
G --> K
H --> M
J --> D
| Component | Function | Benefit |
|---|---|---|
| Memory Engine | Stores and retrieves user memories | Long-term context retention |
| Context Builder | Constructs prompts with relevant memories | Better response continuity |
| Provider Abstraction | Supports multiple provider adapters | Extendable integration model |
| User Manager | Handles user profiles and preferences | Personalized behavior |
| Pruning System | Manages memory lifecycle | Controlled storage growth |
- Conversation memory persistence
- Relevance-based memory retrieval
- Memory pruning endpoint with retention window
- Context augmentation during chat generation
- Persistent user identity (
user_id) - Language and tone preferences
- Custom instruction storage
- Last-active tracking and message count
- OpenAI integration
- Qwen adapter
- DeepSeek adapter
- Central provider factory for deterministic routing
- API key protection for
/api/v1/*routes - Structured JSON error responses
- Restricted CORS origin policy
- Python 3.10+
- Git
- Docker (optional)
- Provider API key (required for real LLM calls)
# Clone the repository
git clone https://github.com/aymantaha3345/memorai.git
cd memorai
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env and set INTERNAL_API_KEY + provider keys
# Launch the server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000# Build and run with Docker Compose
docker compose up --build| Variable | Required | Default | Purpose |
|---|---|---|---|
INTERNAL_API_KEY |
Yes | change-me-in-production |
API auth key for protected endpoints |
OPENAI_API_KEY |
For OpenAI | "" |
OpenAI API authentication |
QWEN_API_KEY |
For Qwen | "" |
Qwen API authentication |
DEEPSEEK_API_KEY |
For DeepSeek | "" |
DeepSeek API authentication |
DATABASE_URL |
No | sqlite:///./data/memorai.db |
Database connection |
DEFAULT_PROVIDER |
No | openai |
Default provider |
MAX_CONTEXT_TOKENS |
No | 8000 |
Context token limit setting |
MEMORY_RETENTION_DAYS |
No | 30 |
Memory retention period |
LOG_LEVEL |
No | INFO |
Logging verbosity |
ALLOWED_ORIGINS |
No | http://localhost:3000,http://127.0.0.1:3000 |
CORS allow-list |
# App
APP_ENV=development
APP_NAME=MEMORAI - Persistent AI Memory Server
APP_VERSION=1.1.0
LOG_LEVEL=INFO
# Security
INTERNAL_API_KEY=your-internal-api-key
ALLOWED_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
# Database
DATABASE_URL=sqlite:///./data/memorai.db
# Providers
DEFAULT_PROVIDER=openai
OPENAI_API_KEY=sk-your-openai-key-here
QWEN_API_KEY=your-qwen-key-here
DEEPSEEK_API_KEY=your-deepseek-key-herehttp://localhost:8000
Include API key using one of:
X-API-Key: your-internal-api-keyor
Authorization: Bearer your-internal-api-keyPOST /api/v1/chatRequest Body:
{
"user_id": "user-uuid-string",
"message": "How can you help me today?",
"provider": "openai",
"model": "gpt-4o-mini",
"temperature": 0.7,
"max_tokens": 1000
}Response:
{
"id": "response-id",
"user_id": "user-uuid-string",
"message": "Generated assistant reply",
"timestamp": "2026-01-15T10:30:00Z",
"tokens_used": 85,
"memory_injected": true,
"provider_used": "openai"
}GET /api/v1/user/{user_id}PUT /api/v1/user/{user_id}/preferencesRequest Body:
{
"name": "Jane Smith",
"language_preference": "en",
"tone_style_preference": "friendly",
"custom_instructions": "Respond concisely with examples when useful."
}POST /api/v1/memory/pruneRequest Body:
{
"user_id": "user-uuid-string",
"retention_days": 30
}GET /healthResponse:
{
"status": "healthy",
"version": "1.1.0",
"provider_default": "openai",
"checks": {
"database": {
"ok": true,
"error": null
}
},
"timestamp": "2026-01-15T10:30:00Z"
}# Development mode with auto-reload
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Production-like mode
uvicorn app.main:app --host 0.0.0.0 --port 8000version: '3.8'
services:
memorai:
build: .
ports:
- "8000:8000"
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
- INTERNAL_API_KEY=${INTERNAL_API_KEY}
- DATABASE_URL=postgresql://memorai:memorai@db:5432/memorai
- DEFAULT_PROVIDER=openai
- LOG_LEVEL=INFO
- ALLOWED_ORIGINS=http://localhost:3000
depends_on:
- db
restart: unless-stopped
db:
image: postgres:15-alpine
environment:
- POSTGRES_DB=memorai
- POSTGRES_USER=memorai
- POSTGRES_PASSWORD=memorai
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
postgres_data:memorai/
βββ app/
β βββ __init__.py
β βββ main.py
β βββ api/
β β βββ __init__.py
β β βββ v1/
β β βββ __init__.py
β β βββ chat.py
β β βββ user.py
β β βββ memory.py
β βββ core/
β β βββ __init__.py
β β βββ config.py
β β βββ database.py
β β βββ security.py
β β βββ errors.py
β βββ memory/
β β βββ __init__.py
β β βββ engine.py
β β βββ manager.py
β β βββ storage.py
β βββ models/
β β βββ __init__.py
β β βββ user.py
β β βββ memory.py
β β βββ chat.py
β β βββ schemas.py
β βββ providers/
β β βββ __init__.py
β β βββ base.py
β β βββ openai.py
β β βββ qwen.py
β β βββ deepseek.py
β β βββ factory.py
β βββ utils/
β βββ __init__.py
β βββ helpers.py
βββ tests/
β βββ conftest.py
β βββ test_api.py
βββ Dockerfile
βββ docker-compose.yml
βββ requirements.txt
βββ .env.example
βββ readme.md
βββ LICENSE
# Run all tests
pytest tests/ -v
# Run specific test file
pytest tests/test_api.py -v- Fork the repository
- Clone your fork:
git clone https://github.com/yourusername/memorai.git - Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Test thoroughly
- Commit your changes:
git commit -m 'Add amazing feature' - Push to your branch:
git push origin feature/amazing-feature - Open a Pull Request
- Follow PEP 8 coding standards
- Write clear docstrings for non-trivial logic
- Include tests for new features
- Update documentation when behavior changes
- Keep commits atomic and descriptive
MIT License - See LICENSE file for details.
- Documentation: API Reference
- Issues: GitHub Issues
- Email: aymantaha89pp@gmail.com
π Built with β€οΈ by Ayman Taha
Making AI Conversations Truly Conversational
