Skip to main content

Official Python SDK for MemoryModel API

Project description

memory-model

Official Python SDK for MemoryModel.

Installation

pip install memory-model

Quick Start

from memory_model import MemoryClient

client = MemoryClient(
    api_key="sk_live_...",
    default_end_user_id="user_123"
)

# Add a memory
client.add("Project deadline is Friday.")

# Search memories
results = client.search("When is the deadline?")
for memory in results:
    print(f"[{memory.similarity:.2f}] {memory.content}")

API Reference

Constructor

MemoryClient(
    api_key: str,                      # Required
    base_url: str = "https://api.memorymodel.dev",
    default_end_user_id: str = None,
    timeout: int = 30,                 # Seconds
    max_retries: int = 3,              # Retries for 5xx/429
    api_version: str = "v1"            # API version prefix
)

Methods

add(content, *, user_context=None, end_user_id=None)

response = client.add("Meeting moved to 3pm.", user_context="Calendar update")
print(response.job_id)

add_image(image_data, *, user_context=None, end_user_id=None)

import base64
with open("screenshot.png", "rb") as f:
    img_b64 = base64.b64encode(f.read()).decode()

response = client.add_image(img_b64, user_context="Error screenshot")

search(query, *, limit=5, strategy="centroid_aware", end_user_id=None)

results = client.search("What time is the meeting?", limit=3)

list(*, limit=50, memory_type=None, end_user_id=None)

memories = client.list(limit=10)

delete(memory_id, *, end_user_id=None)

client.delete("mem_abc123")

search_image(image_data, *, limit=5, end_user_id=None) — Visual Similarity Search

import base64

# Load and encode image
with open("query_image.jpg", "rb") as f:
    img_b64 = base64.b64encode(f.read()).decode()

# Find visually similar memories
results = client.search_image(img_b64, limit=5)
for memory in results:
    print(f"Found: {memory.id}")
    if hasattr(memory, 'source_doc'):
        print(f"  → From: {memory.source_doc}, Page: {memory.page_number}")

Note: Image search finds memories visually similar to the input image. Only memories ingested via add_image() will be matched.

add_document(storage_path, *, end_user_id=None) — Ingest PDF Document

# First, upload the PDF to your storage bucket
# Then trigger server-side processing

client.add_document("projects/my-project/docs/contract.pdf")

This method triggers server-side PDF processing that:

  1. Extracts text from each page
  2. Creates separate memories with source_doc and page_number metadata
  3. Allows you to trace which page a memory came from

Error Handling

from memory_model import MemoryClient, MemoryClientError

try:
    client.search("test")
except MemoryClientError as e:
    print(e.message)       # Human-readable error
    print(e.status)        # HTTP status (401, 429, 500...)
    print(e.code)          # Error code from API
    print(e.is_retryable)  # True for 5xx/429

Automatic Retry: The SDK retries on 5xx/429 with exponential backoff (1s → 2s → 4s).


Agent Integration

LangChain / CrewAI Example

from memory_model import MemoryClient
from langchain_openai import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage

memory = MemoryClient(api_key="sk_live_...", default_end_user_id="user_123")
llm = ChatOpenAI(model="gpt-4o")

def agent_respond(user_message: str) -> str:
    # 1. Store user message
    memory.add(user_message, user_context="User question")
    
    # 2. Retrieve context
    context = memory.search(user_message, limit=3)
    context_str = "\n".join([f"- {m.content}" for m in context])
    
    # 3. Generate response
    messages = [
        SystemMessage(content=f"Context:\n{context_str}"),
        HumanMessage(content=user_message)
    ]
    response = llm.invoke(messages)
    
    return response.content

Image-Based Search Example

import base64
from memory_model import MemoryClient

memory = MemoryClient(api_key="sk_live_...", default_end_user_id="user_123")

def find_similar_images(image_path: str):
    # 1. Load and encode query image
    with open(image_path, "rb") as f:
        img_b64 = base64.b64encode(f.read()).decode()
    
    # 2. Search for visually similar memories
    results = memory.search_image(img_b64, limit=5)
    
    # 3. Results may include source document info
    for mem in results:
        print(f"Found: {mem.id}")
        if hasattr(mem, 'source_doc'):
            print(f"  → From: {mem.source_doc}, Page: {mem.page_number}")
    
    return results

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

memory_model-0.0.4.tar.gz (5.6 MB view details)

Uploaded Source

Built Distribution

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

memory_model-0.0.4-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file memory_model-0.0.4.tar.gz.

File metadata

  • Download URL: memory_model-0.0.4.tar.gz
  • Upload date:
  • Size: 5.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for memory_model-0.0.4.tar.gz
Algorithm Hash digest
SHA256 e2c0c042651e1adc291a4e3a7f38bf523462ee323779ead13565aa2ead5388e9
MD5 41b334a7b6f32a77cce2f8191c22a867
BLAKE2b-256 b10aee865d7177b84445ff1c140c94b8f0b8ab17a46d7fa39432376a595292d8

See more details on using hashes here.

File details

Details for the file memory_model-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: memory_model-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for memory_model-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 436da41d00b469fdc8d1a1ae50c978083cff02d81cb3e8ddf87d542cde6cbe87
MD5 58ddd4bbbf7b83a57bac152e68845dcf
BLAKE2b-256 1424f481652484e8db3858bcc23f99368692d399ef9e49f318342036b7137b4c

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