Python SDK for the Mémoire memory engine
Project description
Mémoire
Mémoire is a production-grade, service-oriented memory engine for AI agents. It provides persistent, long-term memory on top of PostgreSQL + pgvector, accessible via a high-performance Python SDK or a clean, language-agnostic HTTP API.
Designed as a "Sidecar Brain," it runs alongside your main application, handling memory ingestion, storage, optimization, and retrieval. It allows you to build "Stateful LLMs" that remember user details, preferences, and past context across sessions.
🚀 Key Features
🧠 The Intelligence Engine
-
SQL + Vectors: PostgreSQL 15 +
pgvectorfor portable, queryable, and semantically searchable memories. -
Dual Memory System:
-
Conscious / Working Memory: "Essential" facts (identity, long‑term prefs) available immediately at session start.
-
Long‑Term Recall: Query‑aware retrieval based on semantic relevance.
-
-
Intelligent Extraction Pipeline:
-
Uses pluggable LLM providers (OpenAI, Anthropic, Gemini, OpenRouter) for structured fact extraction.
-
Slot‑Based Supersession: Intelligent updates where new facts override old ones (e.g., "moved to Dallas" supersedes "lives in SF").
-
Lifecycle Management: Tracks
superseded_by,last_refreshed_at, and applies periodic decay to stale facts.
-
🔌 Integration & SDK
-
Sidecar Memory Service: Runs as a standalone Docker service. Your app stays lightweight; Mémoire handles the heavy lifting.
-
Python SDK: A "Fail-Open" wrapper for your LLM client. If the memory backend is down, your app continues functioning (just without memory).
-
Streaming Support: Fully compatible with streaming LLM responses.
-
Observability: Prometheus metrics (request counts, latency) and structured Pydantic validation.
-
Secure Access: Per-user API keys and built-in rate limiting.
🏗️ Architecture
The system is built on a robust, async-first stack:
-
API Layer: FastAPI (Python 3.11+)
-
Database: PostgreSQL 15 +
pgvector -
Queue/Cache: Redis
-
Worker: ARQ (Async Task Queue)
-
SDK: Python client wrapping standard LLM libraries
graph TD
Client[Your App / SDK] -->|POST /v1/ingest| API[Mémoire API]
Client -->|GET /v1/conscious| API
Client -->|POST /v1/recall| API
API --> DB[(PostgreSQL)]
API --> Redis[(Redis)]
Worker[ARQ Worker] -->|Poll| Redis
Worker -->|Extract & Optimize| OpenAI[LLM Provider]
Worker -->|Save Facts| DB
⚡ Quick Start
Prerequisites
-
Docker & Docker Compose
-
An LLM provider key (OpenAI, OpenRouter, Anthropic, or Gemini)
1. Backend Setup
# Clone the repository
git clone https://github.com/rajat1299/ai-memory-engine.git
cd ai-memory-engine
# Configure environment
cp .env.example .env
Edit .env and set your LLM credentials:
DATABASE_URL=postgresql+asyncpg://memori:memori@db:5432/memori
REDIS_URL=redis://redis:6379
LLM_PROVIDER=openrouter # or: openai|anthropic|gemini
OPENROUTER_API_KEY=sk-or-...
2. Run Services
# Start Postgres + Redis
docker-compose up -d db redis
# Apply migrations
docker-compose run api alembic upgrade head
# Start API + worker
docker-compose up api worker
The API is now active at http://localhost:8000.
💻 Usage: Python SDK (Recommended)
The easiest way to integrate Mémoire is via the Python SDK, which wraps your existing LLM client to automate memory injection and extraction.
1. Install
pip install -e ".[openai]"
2. Auto-Memory Integration
from memoire import Memoire
import openai
# Initialize the SDK with your Mémoire API Key
memoire = Memoire(api_key="memori_...")
# Wrap your OpenAI client
client = memoire.wrap(openai.OpenAI())
# 1. First interaction: Fact is ingested automatically
response = client.chat.completions.create(
model="gpt-4o",
user="user-123", # Required for memory tracking
messages=[{"role": "user", "content": "I live in Austin, Texas now."}]
)
# ... Time passes ...
# 2. Second interaction: Memory is automatically recalled and injected
response = client.chat.completions.create(
model="gpt-4o",
user="user-123",
messages=[{"role": "user", "content": "Where do I live?"}]
)
# Output: "You live in Austin, Texas."
🔌 Usage: REST API
If you are not using Python, or need fine-grained control, use the HTTP API directly.
1. Create User & Key
curl -X POST http://localhost:8000/v1/users -d '{}'
# Returns: {"id": "...", "api_key": "memori_..."}
2. Create Session
curl -X POST http://localhost:8000/v1/sessions \
-H "X-API-Key: memori_..." \
-d '{ "user_id": "<USER_ID>" }'
3. Ingest Chat (Write)
Triggers the extraction pipeline to parse facts from the conversation.
curl -X POST http://localhost:8000/v1/ingest \
-H "X-API-Key: memori_..." \
-d '{
"user_id": "<USER_ID>",
"session_id": "<SESSION_ID>",
"role": "user",
"content": "I live in Austin now and love hiking."
}'
4. Recall (Read)
Retrieve relevant facts using hybrid vector + fuzzy search.
curl -X POST http://localhost:8000/v1/recall \
-H "X-API-Key: memori_..." \
-d '{
"user_id": "<USER_ID>",
"query": "where do I live?",
"current_view_only": true
}'
🧠 Background Intelligence
Mémoire runs asynchronous tasks to ensure memory remains relevant and clean:
-
Immediate Extraction: Triggered on
/ingest. Extracts atomic facts from the last few messages, deduplicates them, and stores embeddings. -
Periodic Optimization: Runs every 6 hours. Analyzes the user's memory bank to identify "Essential" facts (setting
is_essential=true), which are promoted to the/consciouspayload for fast loading. -
Decay: Periodically reduces confidence scores for stale facts based on
last_refreshed_at, allowing unused information to fade naturally.
🛠️ Development
# Run tests
docker-compose run api pytest
# Lint code
docker-compose run api ruff check .
📄 License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file memoire-0.1.0.tar.gz.
File metadata
- Download URL: memoire-0.1.0.tar.gz
- Upload date:
- Size: 52.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0d85d03f57ea61da5531a945c65eff5b2f3546509760a80648f909b939a4ec2
|
|
| MD5 |
20073bfd93eb8ad79e96676fadc4c539
|
|
| BLAKE2b-256 |
e773dd3d3f3a8d8666eafd9eccea4b095798e6c3e994a0dd74cf93e0b22623fa
|
File details
Details for the file memoire-0.1.0-py3-none-any.whl.
File metadata
- Download URL: memoire-0.1.0-py3-none-any.whl
- Upload date:
- Size: 62.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1efe8bd37a7b20f5cd516561249c689ce3c6410cef0f0ce0ecab6a95358f0f8
|
|
| MD5 |
8747613a1c65b1546fbed0408664eea2
|
|
| BLAKE2b-256 |
7a8a488e9b3fe31e938c626b92164dbd6b7c7786e7876566c60ff920f92a024d
|