Skip to content

About

Knowledge storage and RAG service with PostgreSQL, vector search, and gRPC API

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ContextBrain

Python 3.13+ License

ContextBrain is the Knowledge Storage & RAG Service of the ContextUnity ecosystem. It owns vector search, knowledge graphs, and typed Conversation History on PostgreSQL or SQLite.


What is it for?

  • RAG backends — store and retrieve knowledge for LLM applications
  • Product catalogs — taxonomy, enrichment, and semantic search
  • Memory systems — typed Conversation History and BrainCell memory for AI agents
  • News aggregation — fact storage and deduplication

Quick Start

Database Setup

createdb brain
psql brain -c "CREATE EXTENSION IF NOT EXISTS vector;"
psql brain -c "CREATE EXTENSION IF NOT EXISTS ltree;"
uv run alembic upgrade head

Running the Service

export BRAIN_DATABASE_URL="postgres://user:pass@localhost:5432/brain"
export EMBEDDER_TYPE="openai"
export OPENAI_API_KEY="sk-..."
uv run python -m contextunity.brain

Running Tests

uv run --package contextunity-brain pytest

Architecture

src/contextunity/brain/
├── service/                    # gRPC service handlers & server
│   ├── handlers/               # Domain-specific: knowledge, memory, taxonomy, commerce, news, traces
│   └── interceptors.py         # BrainPermissionInterceptor (RLS enforcement)
├── storage/postgres/           # PostgreSQL + pgvector (primary store)
│   ├── store/                  # Modular mixins (search, graph, conversation history)
│   └── schema.py               # DDL definitions
├── ingestion/rag/              # RAG pipeline & NLP processors
├── payloads.py                 # Pydantic validation models
└── core/                       # Config, registry, interfaces

All modules follow the 400-Line Code Scale standard. The BrainService uses a mixin pattern for composable database operations.


Core Configuration

Variable Default Description
BRAIN_DATABASE_URL — PostgreSQL connection string (required)
BRAIN_PORT 50051 gRPC server port
BRAIN_TENANTS — Comma-separated allowed tenants
EMBEDDER_TYPE auto openai or local
PGVECTOR_DIM 1536 Vector dimensions (must match embedder)
OPENAI_API_KEY — Required for OpenAI embeddings
REDIS_URL — Embedding cache (optional, falls back to in-memory LRU)

Multi-Tenant Security

ContextBrain enforces multi-layer tenant isolation via PostgreSQL Row-Level Security (RLS). Every database operation sets session scope before query execution:

SET LOCAL app.current_tenant = '{tenant_id}';

Only the brain_admin role can bypass RLS (used by Brain Admin / ContextForge dashboard access).


Memory Architecture

Type Storage Description
Semantic cells (pgvector / sqlite-vec) Long-term BrainCells with vector embeddings
Conversation History conversation_records Typed bounded tenant/user/session history
BrainCells cells (Postgres) Durable facts, preferences, and documentation
Embedding Cache Redis (optional) Avoids redundant API calls; falls back to LRU (2048 entries)

Further Reading

About

Knowledge storage and RAG service with PostgreSQL, vector search, and gRPC API

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages