Skip to main content

A lightweight, local-first semantic memory buffer for AI agents with hybrid time-decay scoring.

Project description

semantic-buffer 🧠

PyPI version Python versions License: MIT

A lightweight, local-first semantic memory buffer for AI agents. It implements a hybrid scoring system that prioritizes memories based on semantic relevance, importance, and exponential time-decay (recency).

🌟 Features

  • Local-first Vector Storage: Simple SQLite-based vector storage with zero cloud dependencies or bulky database installations.
  • Hybrid Scoring: Combines cosine similarity, recency (exponential time decay), and importance weighting to retrieve the most contextual memories.
  • Pluggable Embeddings: Run lightweight model locally (sentence-transformers) or plug in any API (Gemini, OpenAI, Cohere).
  • Agent Decorators: @remember decorator automatically tracks function inputs, returns, and metadata in the background.

🏗️ Architecture

  [ Agent Action ] ──► ( @remember Decorator ) ──► [ SemanticBuffer ]
                                                           │
                                                  ( Generate Embeddings )
                                                           │
          [ LLM Prompt ] ◄── [ Ranked Top N Memories ] ◄── [ SQLite DB ]

🚀 Installation & Import Namespace

[!IMPORTANT] Install Name vs. Import Name Namespace

  • Pip Install Name: semantic-buffer (with hyphen)
  • Python Import Name: semanticbuffer (no hyphens or underscores, e.g. import semanticbuffer)

1. Minimal Installation (API Embedders)

If you only plan to use external API models (Gemini, OpenAI, etc.) and want to keep dependencies lightweight:

pip install semantic-buffer

2. Local-First Installation (Offline CPU Models)

If you want to use the default offline embeddings (sentence-transformers running locally on your CPU):

pip install "semantic-buffer[local]"

[!WARNING] If you instantiate SemanticBuffer() without passing an embedder, it defaults to using the local offline model. If you did not install the [local] extra, it will throw an ImportError requesting sentence-transformers.

⚡ Quickstart

from semanticbuffer import SemanticBuffer

# 1. Initialize local memory buffer
buffer = SemanticBuffer(db_path="my_memory.db")

# 2. Add memories with importance scores
buffer.add("User's favorite programming language is Python.", importance=0.9)
buffer.add("It rained today in Paris.", importance=0.4)

# 3. Retrieve relevant context dynamically
memories = buffer.search("What coding preferences does the user have?", limit=1)
print(memories[0]["content"])
# Output: "User's favorite programming language is Python."

Auto-Capture Agent Interactions with Decorators

buffer = SemanticBuffer()

@buffer.remember(importance=0.7)
def run_web_search(query: str):
    # Mocking search execution
    return f"Results for {query}: Python is a versatile language."

# Automatically logged to database
run_web_search("Python info")

⚙️ Custom Embedders (OpenAI Example)

You can easily swap out the local model for your preferred embedding API:

from semanticbuffer import SemanticBuffer, BaseEmbedder

class OpenAIEmbedder(BaseEmbedder):
    def __init__(self, api_key):
        from openai import OpenAI
        self.client = OpenAI(api_key=api_key)

    def embed_query(self, text: str):
        res = self.client.embeddings.create(input=[text], model="text-embedding-3-small")
        return res.data[0].embedding

    def embed_documents(self, texts: list[str]):
        return [self.embed_query(t) for t in texts]

# Instantiate with custom embedder
buffer = SemanticBuffer(embedder=OpenAIEmbedder(api_key="your-key"))

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

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

semantic_buffer-0.1.2.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

semantic_buffer-0.1.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file semantic_buffer-0.1.2.tar.gz.

File metadata

  • Download URL: semantic_buffer-0.1.2.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for semantic_buffer-0.1.2.tar.gz
Algorithm Hash digest
SHA256 498a1aec1c11eebf3f02b9da1e8f52e9f51eb9271a987692621b5d8197ff46f0
MD5 ef439902d46b1192b0e863ec08504d49
BLAKE2b-256 a3132430afa94444e1a4da079207977ff011b398cf7d8e141e42d36b497976bd

See more details on using hashes here.

File details

Details for the file semantic_buffer-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for semantic_buffer-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1c7368fe629fb40c2e1dd1e15d6353b7b0631be988f6604b033096f6c41cfca3
MD5 aef3b7da09ffc736f42c8b72a2695f0d
BLAKE2b-256 574eebd3fb9f6181fd562bf7b0b58755833dd41a3fb0695f7bdd36f032ae4ff7

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