Skip to main content

Lightweight and extensible memory layer for LLMs

Project description

memx - memory layer


Lightweight and extensible memory layer for LLMs.



Important Disclaimer: This library is intended to be production-ready, but currently is in active development. Fix the package version and run your own tests :)

🔥 Key Features

  • Framework agnostic: Use your preferred AI agent framework.
  • No vendor lock-in: Use your preferred cloud provider or infrastructure. No third-party api keys; your data, your rules.
  • Multiple backends: Seamlessly move from your local POC to production deployment (SQLite, MongoDB, PostgreSQL).
  • Sync and async api: Highly compatible with modern and legacy frameworks.
  • No forced schema: As long it is a list of json serializable objects.
  • Resumable memory: Perfect for chat applications and REST APIs.
  • Robust: Get production-ready code with minimal effort.
  • No esoteric patching: WYSIWYG, extensible as a sidecar. It doesn´t touch your libraries under the hood.

⚙️ Installation

From pypi:

pip install memx-ai

🚀 Quickstart

OpenAI

Simple conversation using OpenAI Python library

# https://platform.openai.com/docs/guides/conversation-state?api-mode=responses
# tested on openai==2.6.1

from openai import OpenAI
from memx.engine.sqlite import SQLiteEngine

sqlite_uri = "sqlite+aiosqlite:///message-storage.db"
engine = SQLiteEngine(sqlite_uri, "memx-messages", start_up=True)
m1 = engine.create_session()  # create a new session

client = OpenAI()

m1.sync.add([{"role": "user", "content": "tell me a good joke about programmers"}])

first_response = client.responses.create(
    model="gpt-4o-mini", input=m1.sync.get(), store=False
)

print(first_response.output_text)

m1.sync.add(
    [{"role": r.role, "content": r.content[0].text} for r in first_response.output]
)

m1.sync.add([{"role": "user", "content": "tell me another"}])

second_response = client.responses.create(
    model="gpt-4o-mini", input=m1.sync.get(), store=False
)

m1.sync.add(
    [{"role": r.role, "content": r.content[0].text} for r in second_response.output]
)

print(f"\n\n{second_response.output_text}")

print(m1.sync.get())

Pydantic AI

Message history with async Pydantic AI + OpenAI

# Reference: https://ai.pydantic.dev/message-history/

import asyncio

import orjson
from pydantic_ai import Agent, ModelMessagesTypeAdapter

from memx.engine.sqlite import SQLiteEngine

agent = Agent("openai:gpt-4o-mini")


async def main():
    sqlite_uri = "sqlite+aiosqlite:///message_store.db"
    engine = SQLiteEngine(sqlite_uri, "memx-messages", start_up=True)
    m1 = engine.create_session()  # create a new session

    result1 = await agent.run('Where does "hello world" come from?')

    # it is your responsibility to add the messages as a list[dict]
    messages = orjson.loads(result1.new_messages_json())

    await m1.add(messages)  # messages: list[dict] must be json serializable

    session_id = m1.get_id()
    print("Messages added with session_id: ", session_id)

    # resume the conversation from 'another' memory
    m2 = await engine.get_session(session_id)
    old_messages = ModelMessagesTypeAdapter.validate_python(await m2.get())

    print("Past messages:\n", old_messages)

    result2 = await agent.run(
        "Could you tell me more about the authors?", message_history=old_messages
    )
    print("\n\nContext aware result:\n", result2.output)


if __name__ == "__main__":
    asyncio.run(main())

You can change the memory backend with minimal modifications. Same api to add and get messages.

from memx.engine.mongodb import MongoDBEngine
from memx.engine.postgres import PostgresEngine
from memx.engine.sqlite import SQLiteEngine

# SQLite backend
sqlite_uri = "sqlite+aiosqlite:///message_store.db"
e1 = SQLiteEngine(sqlite_uri, "memx-messages", start_up=True)
m1 = e1.create_session() # memory session ready to go

# PostgreSQL backend
pg_uri = "postgresql+psycopg://admin:1234@localhost:5433/test-database"
e2 = PostgresEngine(pg_uri, "memx-messages", start_up=True)
m2 = e2.create_session()

# MongoDB backend
mongodb_uri = "mongodb://admin:1234@localhost:27017"
e3 = MongoDBEngine(mongodb_uri, "memx-test", "memx-messages")
m3 = e3.create_session()

More examples...

Tests

pytest tests -vs

Tasks

  • Add mongodb backend
  • Add SQLite backend
  • Add Postgres backend
  • Add redis backend
  • Add tests
  • Publish on pypi
  • Add full sync support
  • Add docstrings

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

memx_ai-0.1.9.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

memx_ai-0.1.9-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file memx_ai-0.1.9.tar.gz.

File metadata

  • Download URL: memx_ai-0.1.9.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for memx_ai-0.1.9.tar.gz
Algorithm Hash digest
SHA256 4961bf37d21051e6426618231a75d7d82b484768d63c0ba78a2162931197d5ff
MD5 c976ebb30a1d688faad2f25c711c6f23
BLAKE2b-256 5f77be8aeab1c959617360ee9e7133d258b558ca6be032fdaf04c84515ff5acd

See more details on using hashes here.

File details

Details for the file memx_ai-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: memx_ai-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for memx_ai-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 a8630ee20f78f2dc2df2e6a2183a8cce3ef34890df88aab6bcef2ac236eec2c0
MD5 c70d5a99822c7b47f263caafea94dc55
BLAKE2b-256 67686fba54c79ae394f4a22d3553c0bfaff4f5d6406d174c16f3119a1dd78389

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