Skip to main content

Python SDK for SharedMemory.ai — the persistent memory layer for AI agents

Project description

SharedMemory Python SDK

The persistent memory layer for AI agents. Add, search, and manage long-term memories with knowledge graph, entity scoping, session lifecycle, and structured extraction.

Installation

pip install sharedmemory

Quick Start

from sharedmemory import SharedMemory

memory = SharedMemory(
    api_key="sm_live_...",
    volume_id="your-volume-uuid",
)

# Add a memory
result = memory.add("User prefers dark mode and compact layout")
print(result["status"])  # "approved"

# Search memories
results = memory.search("what are the user's UI preferences?")
for source in results["sources"]:
    print(source["content"], source["score"])

# Batch add
memory.add_many([
    {"content": "User's name is Alice"},
    {"content": "Alice works at Acme Corp"},
    {"content": "Alice prefers Python over JavaScript"},
])

Entity Scoping

Scope memories to specific users, agents, or sessions:

memory = SharedMemory(
    api_key="sm_live_...",
    volume_id="vol-uuid",
    user_id="user-123",
    agent_id="chatbot-1",
)

# All operations are automatically scoped
memory.add("User asked about pricing", session_id="sess-abc")
results = memory.search("pricing", session_id="sess-abc")

Sessions

Track conversation sessions with automatic summarization:

# Start a session
memory.start_session("conv-001", user_id="user-123")

# Add memories during the session
memory.add("User asked about pricing", session_id="conv-001")
memory.add("User is interested in enterprise plan", session_id="conv-001")

# End session — automatically summarizes into long-term memory
summary = memory.end_session("conv-001", auto_summarize=True)

Advanced Search

# With reranking
results = memory.search(
    "project deadlines",
    rerank=True,
    rerank_method="llm",
    include_context=True,
)

# With metadata filters
results = memory.search("preferences", filters={
    "AND": [
        {"field": "memory_class", "op": "eq", "value": "preference"},
        {"field": "score", "op": "gte", "value": 0.5},
    ]
})

Knowledge Graph

# Get entity details
entity = memory.get_entity("Alice")
print(entity["summary"])
print(entity["facts"])

# Get full graph
graph = memory.get_graph()

Feedback

memory.feedback("memory-uuid", "POSITIVE", reason="Highly relevant")
memory.feedback("memory-uuid", "NEGATIVE", reason="Outdated information")

Context Assembly

Get an optimized context block for LLM prompting:

context = memory.assemble_context(template_id="conversational")
print(context["blocks"])  # Ready for system prompt injection

Structured Extraction

data = memory.extract(
    text="Alice is 28, works at Acme Corp as a Senior Engineer since 2022",
    schema_id="contact-info",
)
print(data)  # {"name": "Alice", "age": 28, "company": "Acme Corp", ...}

Async Client

from sharedmemory.async_client import AsyncSharedMemory

async with AsyncSharedMemory(api_key="sm_live_...") as memory:
    await memory.add("async memory")
    results = await memory.search("query")

API Reference

Method Description
add(content) Add a memory
search(query) Semantic search
get(memory_id) Get single memory
update(memory_id, content) Update memory
delete(memory_id) Soft-delete
add_many(memories) Batch write (up to 100)
feedback(memory_id, feedback) Quality feedback
history(memory_id) Audit trail
get_entity(name) Knowledge graph entity
get_graph() Full knowledge graph
assemble_context() LLM context block
start_session(id) Start session
end_session(id) End + summarize session
export_memories() Export all memories
import_memories(data) Bulk import
extract(text, schema_id) Structured extraction

License

MIT

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

sharedmemory_ai-0.3.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

sharedmemory_ai-0.3.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file sharedmemory_ai-0.3.0.tar.gz.

File metadata

  • Download URL: sharedmemory_ai-0.3.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for sharedmemory_ai-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c811504fb11ac3e8d79bd8a4e1a9e463d5a14f367ec143373052daac00179af6
MD5 aa784cda579ec374bc13e269f28864c0
BLAKE2b-256 7a2d27fdb003cca512ca041f4b5ed15ad1ead174b4c3cb3b32a4c46a1e5a7a03

See more details on using hashes here.

File details

Details for the file sharedmemory_ai-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sharedmemory_ai-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 260246a0c485628a709e85aea1efbde520a1f6ef7f75280a568ffa7a16eb1ebb
MD5 792acc4cba61ab17b9f7db3be6474e75
BLAKE2b-256 3215fb113a9f9debb8030ec111a8fced50f5706604de8407923ceabf4170fe7a

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