Skip to main content

Persistent semantic memory for AI agents — SQLite-backed, local-first, zero config

Project description

cartisien-engram

Persistent semantic memory for AI agents — Python SDK

from engram import Engram, EngramConfig

memory = Engram(EngramConfig(db_path="./memory.db"))

# Store
memory.remember("user_123", "User prefers TypeScript and dark mode", "user")

# Recall semantically — finds the right memory without exact keyword match
results = memory.recall("user_123", "what are the user's preferences?", limit=5)
# [MemoryEntry(content='User prefers TypeScript and dark mode', similarity=0.82, ...)]

Install

pip install cartisien-engram

Optional: LangChain integration

pip install "cartisien-engram[langchain]"

Optional: Local embeddings (recommended)

# Install Ollama: https://ollama.ai
ollama pull nomic-embed-text

Without Ollama, keyword search is used automatically.


Quick Start

from engram import Engram, EngramConfig

memory = Engram(EngramConfig(
    db_path="./agent.db",
    embedding_url="http://localhost:11434",  # Ollama default
))

# In your agent loop
def handle_message(session_id: str, user_input: str) -> str:
    # 1. Recall relevant context
    context = memory.recall(session_id, user_input, limit=5)
    context_str = "\n".join(f"[{e.role}]: {e.content}" for e in context)

    # 2. Build prompt + call your LLM
    response = llm.chat(f"Context:\n{context_str}\n\nUser: {user_input}")

    # 3. Store both sides
    memory.remember(session_id, user_input, "user")
    memory.remember(session_id, response, "assistant")

    return response

LangChain Integration

Drop-in replacement for ConversationBufferMemory:

from engram.langchain import EngramMemory
from langchain.chains import ConversationChain
from langchain.llms import OpenAI

memory = EngramMemory(
    session_id="user_abc",
    db_path="./memory.db",
    recall_limit=10
)

chain = ConversationChain(llm=OpenAI(), memory=memory)
chain.predict(input="My name is Jeff and I'm building GovScout")
# Start a new session — memory persists
chain.predict(input="What am I building?")  # Remembers "GovScout"

API

Engram(config?)

config = EngramConfig(
    db_path="./memory.db",       # SQLite path (default: ":memory:")
    max_context_length=4000,     # Max chars per entry
    embedding_url="http://localhost:11434",  # Ollama base URL
    embedding_model="nomic-embed-text",      # Embedding model
    semantic_search=True,        # Enable semantic search
)
memory = Engram(config)

remember(session_id, content, role="user", metadata=None)

entry = memory.remember("session_1", "User loves Thai food", "user")
# MemoryEntry(id=..., content=..., role='user', timestamp=...)

recall(session_id, query=None, limit=10, options=None)

Semantic search when Ollama available, keyword fallback otherwise.

results = memory.recall("session_1", "food preferences", limit=5)
# [MemoryEntry(..., similarity=0.84)]

history(session_id, limit=20)

Chronological conversation history.

chat = memory.history("session_1", limit=20)

forget(session_id, id=None, before=None)

memory.forget("session_1")                    # clear all
memory.forget("session_1", id="abc123")       # delete one
memory.forget("session_1", before=datetime.now())  # delete old

stats(session_id)

stats = memory.stats("session_1")
# {"total": 42, "by_role": {"user": 21, "assistant": 21}, "with_embeddings": 42}

Context manager

with Engram(EngramConfig(db_path="./memory.db")) as memory:
    memory.remember("s1", "test", "user")

Part of the Cartisien Memory Suite

Package Language Purpose
cartisien-engram Python This package
@cartisien/engram TypeScript/Node TS SDK
@cartisien/engram-mcp TypeScript MCP server
@cartisien/extensa TypeScript Vector infrastructure (soon)
@cartisien/cogito TypeScript Agent identity (soon)

MIT © Cartisien Interactive

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

cartisien_engram-0.8.0.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

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

cartisien_engram-0.8.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file cartisien_engram-0.8.0.tar.gz.

File metadata

  • Download URL: cartisien_engram-0.8.0.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for cartisien_engram-0.8.0.tar.gz
Algorithm Hash digest
SHA256 3c302da77bac0236f434feba7e817dc9794c4732831cbd9133ca7c2028c7c969
MD5 3cd54360506384bb7c0887782841dd20
BLAKE2b-256 197a3b2d08133efe79415eb9be0a594e686e3357faf9993be08e2fcfce354637

See more details on using hashes here.

File details

Details for the file cartisien_engram-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cartisien_engram-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d831b14eb1256dad895b8ac812579a2f7bccee5a3d6b67d3a5092bbca8d128ee
MD5 4d2223ac27031cc4071619113f0df8df
BLAKE2b-256 332c122d7a563b052e8b158a6f906a6d52437db9e17bb3c76923e198e1d68708

See more details on using hashes here.

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