Skip to main content

cortex-memory

An LLM-powered long-term memory layer backed by a Neo4j knowledge graph — packaged as a reusable Python library you can drop into any project.

Python 3.10+


What it does

  1. Chat — queries the LLM with retrieved memory context injected automatically.
  2. Retrieve — embeds your query with all-MiniLM-L6-v2, runs a Neo4j vector-index search, then re-ranks with:
    • importance_weight (very_high → low)
    • stability_weight (permanent → transient)
    • tag_matches between the query and stored memory tags
  3. Ingest — asks the LLM to decide if an input is worth storing. If so, it extracts type, label, content, stability, importance, and related_entities, chunks the text, embeds each chunk, and upserts it into Neo4j as a Memory node linked to Entity nodes via RELATED_TO.
  4. Reinforce — memories recalled during chat have their last_accessed refreshed; transient memories are promoted to stable.

Project structure

cortex-ai-memory/
├── src/
│   └── cortex_memory/          ← installable package
│       ├── __init__.py         ← public API
│       ├── __main__.py         ← CLI entrypoint
│       ├── agent.py            ← CortexMemory orchestrator
│       ├── config.py           ← CortexConfig dataclass
│       ├── graph/
│       │   └── memory_graph.py ← Neo4j upsert (MemoryGraph)
│       ├── retrieval/
│       │   └── retriever.py    ← WeightedMemoryRetriever
│       ├── ingestion/
│       │   └── ingestor.py     ← ingestion pipeline
│       ├── llm/
│       │   └── chat.py         ← LLM client + chat_with_assistant
│       └── prompts/
│           └── templates.py    ← all prompt strings
├── examples/
│   ├── basic_chat.py
│   └── ingest_document.py
├── tests/
│   ├── test_retriever.py
│   └── test_ingestor.py
├── setups/
│   └── neo4j/                  ← Docker / docker-compose setup
├── pyproject.toml
├── requirements-dev.txt
└── .env.example

Prerequisites


Installation

As a dependency in another project

pip install git+https://github.com/your-org/cortex-ai-memory.git

For local development

git clone https://github.com/your-org/cortex-ai-memory.git
cd cortex-ai-memory
pip install -e ".[dev]"

Configuration

Copy .env.example to .env and fill in your values:

NEO4J_URI=bolt://localhost:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=your_neo4j_password_here
GROQ_API_KEY=your_groq_api_key_here

# Optional overrides
# CORTEX_MODEL=openai/gpt-oss-20b
# CORTEX_EMBEDDER=all-MiniLM-L6-v2
# CORTEX_TOP_K=5
# CORTEX_SCORE_THRESHOLD=0.65
# CORTEX_CHUNK_MAX_CHARS=400

Usage

Python API

from cortex_memory import CortexMemory

# Context manager — closes Neo4j connections automatically
with CortexMemory.from_env() as memory:
    # Retrieve + chat + ingest + reinforce — all in one call
    response = memory.chat("What do I know about Japan?")
    print(response)

    # Ingest standalone text
    memory.ingest("User booked flights to Tokyo for March.", source="upload")

    # Raw retrieval without chat
    results = memory.retrieve("Japan travel plans", top_k=5)

CLI

# Chat
cortex-memory chat "What do I know about Japan?"

# Ingest
cortex-memory ingest "User loves hiking in the Alps and dislikes crowded cities."

# Override top-k and disable auto-ingestion
cortex-memory chat "Remind me about my diet goals." --top-k 3 --no-ingest

Running Neo4j with Docker

docker run -d \
  --name memory-graph-neo4j \
  -p 7474:7474 \
  -p 7687:7687 \
  -e NEO4J_AUTH=neo4j/your_neo4j_password_here \
  -v neo4j_data:/data \
  neo4j:5.15

Or using docker-compose (see setups/neo4j/):

docker-compose -f setups/neo4j/docker-compose.yml up -d

Verify at http://localhost:7474.


Running tests

pytest tests/

Memory schema

Field Description
id UUID
type context, event, fact, …
label Short title
content Chunked text
source upload, assistant_chat, cli, …
created_at / last_accessed ISO timestamps
stability transientstablepermanent
status active
tags Keyword list
embedding 384-dim vector (all-MiniLM-L6-v2)
importance very_high, high, medium, low

Related entities are linked as (:Memory)-[:RELATED_TO]->(:Entity).


Using in another project

# my_project/memory_layer.py
from cortex_memory import CortexMemory, CortexConfig

# Explicit config — no .env needed
config = CortexConfig(
    neo4j_uri="bolt://localhost:7687",
    neo4j_username="neo4j",
    neo4j_password="secret",
    groq_api_key="gsk_...",
    top_k=10,
    score_threshold=0.70,
)
memory = CortexMemory(config)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cortex_vault-0.1.0.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cortex_vault-0.1.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file cortex_vault-0.1.0.tar.gz.

File metadata

  • Download URL: cortex_vault-0.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for cortex_vault-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b210107bc3247be4884ea378cae57cea4b697af9abf7ce7795508b8796f65aee
MD5 a0622de79c96751ff09d5c2f784a06d7
BLAKE2b-256 f1a8400663f36adb2b21a990b6c94b3d93863b1d21bad17605ae6ee9174a4888

See more details on using hashes here.

File details

Details for the file cortex_vault-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cortex_vault-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for cortex_vault-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44d01bcfbe6d51f91c3c7642b5ddbf768ea49f568210168685b4ba840fe72a30
MD5 f06aed255576158a1b365031115cc282
BLAKE2b-256 7d255d22e27a94ceec4597cb861db6b65b26b5fe3c0fac78ffda11ff87981f5a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page