Skip to main content

Lightweight Cognis, local-first memory for LLM agents

Project description

Cognis — Lightweight memory for AI agents

Lightweight memory for AI agents

PyPI Python License Deps


Features

  • Hybrid search — Two-stage Matryoshka vector search (256D shortlist, 768D rerank) + BM25 keyword matching via SQLite FTS5, fused with RRF (70/30 split, tuned from ablation studies)
  • Zero infrastructure — Everything runs in-process. Qdrant local mode (file-backed) + SQLite. No Docker, no servers, just pip install
  • Smart extraction — LLM-powered fact extraction with 13 auto-tagged categories, memory versioning (ADD/UPDATE/DELETE), and name-aware facts
  • Session managementowner_id + agent_id + session_id scoping that matches the hosted Cognis platform. Memories are global, messages are session-scoped
  • Fast retrieval — ~500ms search latency (embedding API bottleneck), ~4ms with cache hits

Quick Start

1. Install

pip install lyzr-memory-lite

2. Set your API keys

export GEMINI_API_KEY="your-gemini-key"    # For embeddings
export OPENAI_API_KEY="your-openai-key"    # For extraction (gpt-4.1-mini)

3. Use it

from cognis import Cognis

m = Cognis(owner_id="user_1")

# Add conversation messages — facts are extracted automatically
m.add([
    {"role": "user", "content": "My name is Alice and I work at Google as a data scientist."},
    {"role": "user", "content": "I love hiking and I'm a huge fan of Taylor Swift."},
])

# Search memories
resp = m.search("Where does Alice work?")
for r in resp["results"]:
    print(f"  {r['content']}  (score: {r['score']})")

# Get context for your LLM (short-term messages + long-term memories)
ctx = m.get_context([{"role": "user", "content": "Tell me about myself"}])
print(ctx["context_string"])

# List all extracted memories
for mem in m.get_all()["memories"]:
    cat = mem["metadata"]["category"]
    print(f"  [{cat}] {mem['content']}")

m.close()

Architecture

Cognis search pipeline

API Reference

Cognis(owner_id, agent_id, session_id, data_dir, config)

Initialize a memory instance. At least one of owner_id, agent_id, or session_id is required.

Parameter Type Default Description
gemini_api_key str $GEMINI_API_KEY Gemini API key for embeddings
owner_id str Memory owner identifier
agent_id str None Agent identifier
session_id str auto-generated Session identifier
data_dir str ~/.cognis Local storage directory
config CognisConfig defaults Configuration overrides

Methods

All methods accept optional owner_id, agent_id, session_id overrides per call.

Method Returns Description
add(messages) {"success", "memories", "session_message_count"} Add messages and extract memories
search(query, limit) {"success", "results", "count", "query"} Hybrid RRF search
get(memory_id) {"success", "memory"} Get single memory by ID
get_all(limit, offset) {"success", "memories", "total", "limit", "offset"} List all memories
delete(memory_id) {"success", "message"} Delete a memory
get_context(messages) {"short_term", "long_term", "context_string"} Get LLM-ready context
clear() {"success", "message"} Clear all memories
count() int Count current memories

Session Management

m.new_session()         # Generate new session ID
m.set_session("ses_x")  # Switch session
m.set_owner("user_2")   # Switch owner
m.set_agent("agent_2")  # Switch agent

Scoping rules:

  • Extracted memories are global to (owner_id, agent_id) — persist across sessions
  • Raw messages are scoped to (owner_id, agent_id, session_id) — session-local
  • Search returns global memories + current session messages

Configuration

from cognis import Cognis, CognisConfig

config = CognisConfig(
    embedding_model="gemini/gemini-embedding-2-preview",
    embedding_full_dim=768,
    embedding_small_dim=256,
    vector_weight=0.70,       # RRF: 70% vector
    bm25_weight=0.30,         # RRF: 30% BM25
    rrf_k=10,                 # RRF constant
    similarity_threshold=0.3,
    llm_model="gpt-4.1-mini", # For fact extraction
)

m = Cognis(config=config, owner_id="user_1", data_dir="./my_data")

Memory Categories

Extracted facts are auto-categorized into 13 categories:

identity relationships work_career learning wellness lifestyle interests preferences plans_goals experiences opinions context misc

Dependencies

Only 3 core dependencies:

Package Size Purpose
qdrant-client 3 MB Vector store (local mode, no server)
litellm 55 MB LLM + embedding provider abstraction
pydantic 7 MB Config validation

SQLite is Python stdlib. Total install: ~156 MB. Wheel size: 30 KB.

Development

uv venv --python 3.12 .venv
uv pip install -e ".[dev]" python-dotenv openai
uv run pytest tests/ -v

License

MIT


Built by Lyzr

Project details


Download files

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

Source Distribution

lyzr_cognis-0.1.0.tar.gz (35.0 kB view details)

Uploaded Source

Built Distribution

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

lyzr_cognis-0.1.0-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lyzr_cognis-0.1.0.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lyzr_cognis-0.1.0.tar.gz
Algorithm Hash digest
SHA256 050e2101228c9d683c5b42941680905322bdab674338013ed5dca181701695c3
MD5 ffac2df5d677154b46800554287a28d0
BLAKE2b-256 6c15ba748bd9823506c823ff12919fe574dc7d50da6afe92e24d782b1f284ca2

See more details on using hashes here.

Provenance

The following attestation bundles were made for lyzr_cognis-0.1.0.tar.gz:

Publisher: publish.yml on Lyzr-Cognis/cognis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: lyzr_cognis-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 33.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lyzr_cognis-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 395907413f3cac321b45d20d7b72415b321a1a97f755444b89ba5917b714f957
MD5 172650a1d2c9ea149d286e35841332a6
BLAKE2b-256 e6eb45f250ad0d21eec71325bdbf59b688bf643164c5bece76896dfa44fa711b

See more details on using hashes here.

Provenance

The following attestation bundles were made for lyzr_cognis-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Lyzr-Cognis/cognis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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