Skip to main content

Agnostic Memory Management Library for Local AI (Ollama, LM Studio, vLLM, etc)

Project description

AILocalMemory 🧠

Enterprise-Grade Agnostic Memory Management Library for Local AI.

PyPI version License: MIT

AILocalMemory is a robust, lightweight Python library designed to handle chat history, state persistence, and context window optimization for any local AI endpoint (Ollama, LM Studio, vLLM, Llama.cpp, etc.).

Since most local AI instances are stateless, AILocalMemory handles the tedious parts of conversational AI: storing past messages, truncating them when they get too long (so your AI doesn't crash from OOM), and abstracting the API calls.

Features ✨

  • Plug-and-Play Adapters: Built-in adapters for Ollama and OpenAI-compatible endpoints.
  • Async & Streaming Support: Fully supports asynchronous operations (asyncio) and token streaming for responsive UIs.
  • Persistent Memory: Keep conversations across reboots using SQLite (Thread-safe).
  • Smart Context Optimization: Automatically prunes old messages to fit your model's maximum token limit.

Installation 📦

You can install AILocalMemory directly from PyPI:

pip install ailocalmemory

Quick Start (Using Adapters) 🚀

The easiest way to use the library is via built-in adapters.

from ailocalmemory import ChatSession, OllamaAdapter

# 1. Initialize a session (keeps messages in memory by default)
session = ChatSession(session_id="user_1")

# 2. Wrap it with an adapter (e.g., Ollama)
chat = OllamaAdapter(model="llama3", memory_session=session)

# 3. Chat! The adapter automatically handles context saving and retrieving.
response = chat.send("Hello, my name is Alice and my favorite color is blue.")
print("AI:", response)

response = chat.send("What is my name and favorite color?")
print("AI:", response) # It remembers!

Advanced: Streaming & Async ⚡

AILocalMemory is built for modern applications like FastAPI or Discord bots.

Streaming

# Stream the response token by token
response_stream = chat.send("Write a long story", stream=True)
for chunk in response_stream:
    print(chunk, end="", flush=True)

Async / Await

import asyncio
from ailocalmemory import ChatSession, OllamaAdapter

async def main():
    session = ChatSession(session_id="user_2", storage="sqlite")
    chat = OllamaAdapter(model="llama3", memory_session=session)
    
    # Async Streaming
    stream = await chat.send_async("Tell me a joke", stream=True)
    async for chunk in stream:
        print(chunk, end="", flush=True)

asyncio.run(main())

Storage Options 💾

  • memory (Default): Volatile, lost when script ends.
  • sqlite: Persistent, thread-safe, saves to a local ~/.ailocalmemory/ailocalmemory.db file automatically.

Optimizers ⚙️

By default, the session uses TokenLimitOptimizer(max_tokens=8192) to ensure your context doesn't explode. You can also use SlidingWindowOptimizer to just keep the last $K$ messages.

from ailocalmemory import ChatSession, SlidingWindowOptimizer

# Keeps the last 10 messages and ensures it stays under 8192 tokens
opt = SlidingWindowOptimizer(k=10, max_tokens=8192)
session = ChatSession(session_id="user", optimizer=opt)

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

ailocalmemory-0.1.1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

ailocalmemory-0.1.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file ailocalmemory-0.1.1.tar.gz.

File metadata

  • Download URL: ailocalmemory-0.1.1.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.3

File hashes

Hashes for ailocalmemory-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1d1c23a4e6d721815a90af41491494c08009ec6b533023aa7dcce74f90f7a9a1
MD5 1d264da33f7c369f69793ec752f2f824
BLAKE2b-256 49dfd2909ac770163f9578148f738eff29b41b82f479538129bf8f9c00f5856f

See more details on using hashes here.

File details

Details for the file ailocalmemory-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ailocalmemory-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.3

File hashes

Hashes for ailocalmemory-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 66000b40b276877f0b5361db0088f032b4a1128076c1c97e99629e0cacae25ad
MD5 354a7ba81146d77c0f26353c4fea714c
BLAKE2b-256 1f1ddd2387d042ce21d634dce595f98e3e0b1bf2660473439da1d0a72fbf4a20

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