Skip to main content

Memfuse Python SDK

Project description

GitHub license


MemFuse Logo

MemFuse Python SDK
The official Python client for MemFuse, the open-source memory layer for LLMs.
Explore the Docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About MemFuse
  2. Installation
  3. Quick Start
  4. Examples
  5. Documentation
  6. Community & Support
  7. License

About MemFuse

Large language model applications are inherently stateless by design. When the context window reaches its limit, previous conversations, user preferences, and critical information simply disappear.

MemFuse bridges this gap by providing a persistent, queryable memory layer between your LLM and storage backend, enabling AI agents to:

  • Remember user preferences and context across sessions
  • Recall facts and events from thousands of interactions later
  • Optimize token usage by avoiding redundant chat history resending
  • Learn continuously and improve performance over time

This repository contains the official Python SDK for seamless integration with MemFuse servers. For comprehensive information about the MemFuse server architecture and advanced features, please visit the MemFuse Server repository.

Recent Updates

  • Enhanced Testing: Comprehensive E2E testing with semantic memory validation
  • Better Error Handling: Improved error messages and logging for easier debugging
  • Prompt Templates: Structured prompt management system for consistent LLM interactions
  • Performance Benchmarks: MSC dataset accuracy testing with 95% validation threshold

Installation

Note: This is the standalone Client SDK repository. If you need to install and run the MemFuse server, which is essential to use the SDK, please visit the MemFuse Server repository.

You can install the MemFuse Python SDK using one of the following methods:

Option 1: Install from PyPI (Recommended)

pip install memfuse

Option 2: Install from Source

git clone https://github.com/memfuse/memfuse-python.git
cd memfuse-python
pip install -e .

Quick Start

Here's a comprehensive example demonstrating how to use the MemFuse Python SDK with OpenAI:

from memfuse.llm import OpenAI
from memfuse import MemFuse
import os


memfuse_client = MemFuse(
  # api_key=os.getenv("MEMFUSE_API_KEY")
  # base_url=os.getenv("MEMFUSE_BASE_URL"),
)

memory = memfuse_client.init(
  user="alice",
  # agent="agent_default",
  # session=<randomly-generated-uuid>
)

# Initialize your LLM client with the memory scope
llm_client = OpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),  # Your OpenAI API key
    memory=memory
)

# Make a chat completion request
response = llm_client.chat.completions.create(
    model="gpt-4o", # Or any model supported by your LLM provider
    messages=[{"role": "user", "content": "I'm planning a trip to Mars. What is the gravity there?"}]
)

print(f"Response: {response.choices[0].message.content}")
# Example Output: Response: Mars has a gravity of about 3.721 m/s², which is about 38% of Earth's gravity.

Contextual Follow-up

Now, ask a follow-up question. MemFuse will automatically recall relevant context from the previous conversation:

# Ask a follow-up question. MemFuse automatically recalls relevant context.
followup_response = llm_client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What are some challenges of living on that planet?"}]
)

print(f"Follow-up: {followup_response.choices[0].message.content}")
# Example Output: Follow-up: Some challenges of living on Mars include its thin atmosphere, extreme temperatures, high radiation levels, and the lack of liquid water on the surface.

MemFuse automatically manages the retrieval of relevant information and storage of new memories from conversations within the specified memory scope.

Advanced Features

Memory Validation & Testing

The SDK includes comprehensive testing capabilities to validate memory accuracy:

  • E2E Memory Tests: Automated tests that verify conversational context retention
  • Semantic Similarity Validation: Uses RAGAS framework for intelligent response verification
  • Performance Benchmarks: MSC (Multi-Session Chat) dataset testing with accuracy metrics

Error Handling & Debugging

Enhanced error messages provide clear guidance:

  • Connection Issues: Helpful instructions for starting the MemFuse server
  • API Errors: Detailed error responses with actionable information
  • Logging: Comprehensive logging for troubleshooting and monitoring

Examples

Explore comprehensive examples in the examples/ directory of this repository, featuring:

  • Basic Operations: Fundamental usage patterns and asynchronous operations
  • Conversation Continuity: Maintaining context across multiple interactions
  • UI Integrations: Gradio-based chatbot implementations with streaming support

Documentation

  • Server Documentation: For detailed information about the MemFuse server architecture and advanced configuration, visit the MemFuse online documentation
  • SDK Documentation: Comprehensive API references and guides will be available soon

Community & Support

Join our growing community:

If MemFuse enhances your projects, please ⭐ star both the server repository and this SDK repository!

License

This MemFuse Python SDK is licensed under the Apache 2.0 License. See the LICENSE file for complete 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

memfuse-0.3.1rc1.tar.gz (40.0 kB view details)

Uploaded Source

Built Distribution

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

memfuse-0.3.1rc1-py3-none-any.whl (52.7 kB view details)

Uploaded Python 3

File details

Details for the file memfuse-0.3.1rc1.tar.gz.

File metadata

  • Download URL: memfuse-0.3.1rc1.tar.gz
  • Upload date:
  • Size: 40.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for memfuse-0.3.1rc1.tar.gz
Algorithm Hash digest
SHA256 42a54f635ba77f62c8eb04f133335bfc3823404332eb0fd79ae51d5cd1ac77b7
MD5 5a326513d5ce956daf77f849237b1fd0
BLAKE2b-256 3aa20c5ecfd6a7985f4a24ace730edced8acc92fddcf95ac9fb212d04df2327d

See more details on using hashes here.

Provenance

The following attestation bundles were made for memfuse-0.3.1rc1.tar.gz:

Publisher: release.yml on memfuse/memfuse-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file memfuse-0.3.1rc1-py3-none-any.whl.

File metadata

  • Download URL: memfuse-0.3.1rc1-py3-none-any.whl
  • Upload date:
  • Size: 52.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for memfuse-0.3.1rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 16e291afb1bc6c9dfff6c6012bbf696e107cfd8c5943f9fdc17e284eb4c1b7fc
MD5 1bb258cdd7ae38330ff96fbe2170f37f
BLAKE2b-256 abeff594a3a0b3a5563a56c7151fe91782843e7c055de943180c10937272124b

See more details on using hashes here.

Provenance

The following attestation bundles were made for memfuse-0.3.1rc1-py3-none-any.whl:

Publisher: release.yml on memfuse/memfuse-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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