Skip to main content

Python SDK for DeltaMemory - Smart memory database for AI agents

Project description

DeltaMemory Python SDK

Features

  • Cognitive Processing: Automatic fact, concept, profile, and event extraction
  • Hybrid Search: Combines semantic similarity, recency, and salience scoring
  • User Profiles: Structured facts organized by topic (basic_info, work, interests, etc.)
  • Event Timeline: Track activities, plans, milestones, and preferences with timestamps
  • Knowledge Graph: Multi-hop reasoning across concepts and relationships
  • Pre-formatted Context: Ready-to-use context strings for LLM consumption
  • Async/Await: Full async support with httpx

Installation

pip install deltamemory
# or
poetry add deltamemory
# or
uv add deltamemory

Quick Start

import asyncio
from deltamemory import DeltaMemory, IngestOptions, RecallOptions, RecallWeights

async def main():
    # Create client
    db = DeltaMemory(
        base_url='http://localhost:6969',
        default_collection='my-app'
    )

    async with db:
        # Ingest content with cognitive processing
        result = await db.ingest(
            'User prefers dark mode and TypeScript',
            IngestOptions(
                datetime='2024-01-15T10:30:00Z',
                speaker='user'
            )
        )
        print('Extracted facts:', result.facts)
        print('Extracted concepts:', result.concepts)

        # Recall relevant memories with profiles and events
        recall = await db.recall('What are the user preferences?')

        # Access structured user profiles
        print('Profiles:', recall.profiles)
        # [UserProfile(topic='interest', sub_topic='language', content='TypeScript')]

        # Access timeline events
        print('Events:', recall.events)
        # [UserEvent(gist='prefers dark mode', event_type='preference')]

        # Use pre-formatted context for LLM
        print('Context:', recall.context)

asyncio.run(main())

API Reference

Constructor

db = DeltaMemory(
    base_url='http://localhost:6969',  # Default
    default_collection='default',       # Default
    timeout=30.0,                        # Default (seconds)
    headers={'X-Custom': 'header'},      # Optional
)

# Or from config
from deltamemory import DeltaMemoryConfig
config = DeltaMemoryConfig(base_url='http://localhost:6969')
db = DeltaMemory.from_config(config)

Methods

ingest(content, options?)

Ingest content with full cognitive processing.

from deltamemory import IngestOptions

result = await db.ingest(
    'I started learning TypeScript last week',
    IngestOptions(
        collection='preferences',
        metadata={'source': 'chat'},
        datetime='2024-01-15T10:30:00Z',
        speaker='user'
    )
)
# Returns: IngestResponse(memory_ids, facts, concepts)

recall(query, options?)

Recall memories with profiles, events, and pre-formatted context.

from deltamemory import RecallOptions, RecallWeights, MemoryType

result = await db.recall(
    'user preferences',
    RecallOptions(
        limit=10,
        weights=RecallWeights(similarity=0.5, recency=0.3, salience=0.2),
        memory_types=[MemoryType.FACT, MemoryType.INSIGHT]
    )
)

# Access results
print(result.results)      # List[MemoryResult]
print(result.concepts)     # List[ConceptResult]
print(result.profiles)     # List[UserProfile]
print(result.events)       # List[UserEvent]
print(result.context)      # str - Pre-formatted for LLM

store(content, options?)

Store a memory without cognitive processing.

from deltamemory import StoreOptions, MemoryType

response = await db.store(
    'Important fact',
    StoreOptions(
        memory_type=MemoryType.FACT,
        metadata={'importance': 'high'}
    )
)
print(response.id)

get(id, collection?)

Get a specific memory by ID.

memory = await db.get('memory-id')

delete(id, collection?)

Delete a memory by ID.

await db.delete('memory-id')

decay(rate?, collection?)

Apply salience decay to memories.

result = await db.decay(rate=0.1)
print(result.affected_count)

consolidate(threshold?, collection?)

Consolidate similar memories using LLM.

result = await db.consolidate(threshold=0.8)
print(result.consolidated_count)

reflect(window_size?, collection?)

Generate insights from recent memories.

result = await db.reflect(window_size=10)
print(result.reflection)

stats(collection?)

Get collection statistics.

stats = await db.stats()
print(stats.memory_count)
print(stats.profile_count)
print(stats.event_count)

graph(collection?)

Get knowledge graph for visualization.

graph = await db.graph()
print(graph.nodes)
print(graph.edges)

purge(collection?)

Delete all memories in a collection.

result = await db.purge()
print(result.deleted_count)

health()

Check server health.

health = await db.health()
print(health.healthy, health.version)

Types

from deltamemory import (
    MemoryType,      # Enum: CONVERSATION, FACT, INSIGHT, SUMMARY
    RecallWeights,   # similarity, recency, salience weights
    Memory,          # Memory object
    MemoryResult,    # Memory with cognitive scores
    UserProfile,     # Structured user fact
    UserEvent,       # Timeline entry
    RecallResponse,  # Full recall response
    StatsResponse,   # Collection statistics
    GraphNode,       # Knowledge graph node
    GraphEdge,       # Knowledge graph edge
)

Error Handling

from deltamemory import (
    DeltaMemoryError,
    MemoryNotFoundError,
    CollectionNotFoundError,
    InvalidRequestError,
    ServerUnavailableError,
    ConnectionError,
)

try:
    await db.get('non-existent-id')
except MemoryNotFoundError:
    print('Memory not found')
except ConnectionError:
    print('Server unavailable')
except DeltaMemoryError as e:
    print(f'Error: {e.message} (code: {e.code})')

Context Manager

The client supports async context manager for automatic cleanup:

async with DeltaMemory() as db:
    await db.ingest('Hello world')
    # Client is automatically closed when exiting the context

Migration from CognitiveDB

If you're migrating from the old cognitivedb package:

# Old
from cognitivedb import CognitiveDB
db = CognitiveDB(...)

# New
from deltamemory import DeltaMemory
db = DeltaMemory(...)

# Or use the legacy alias for gradual migration
from deltamemory import CognitiveDB
db = CognitiveDB(...)

License

Apache-2.0

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

deltamemory-0.1.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

deltamemory-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: deltamemory-0.1.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for deltamemory-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0c3e50450e9fb270b54ea53e4761e2dda1d0b2f517573cb9abd2dc1c55e44881
MD5 4a24721165b30c26055b2423693b9b03
BLAKE2b-256 2db79f523aef9f56f9cd407fc79bc8b49a2a7f996aeb7feee340c443bda7b25c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: deltamemory-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for deltamemory-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d3b698785ab63acd12a3a8efd7a89880ce06f01d84008489691ee172562629a
MD5 197ae60bc381786a282d6d970f6593fe
BLAKE2b-256 7d311994b75c887bbc041564541e12d4784a7a555be05847d775e75299c47829

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