Skip to main content

Memory-enabled AI assistant with local LLM support

Project description

๐Ÿง  Mem-Agent: Memory-Enabled Mini Assistant

Python License Ollama

A local AI assistant that remembers user interactions and responds with context awareness using a lightweight 4-billion parameter LLM.

Quick Start โ€ข Features โ€ข Documentation โ€ข Examples


๐ŸŽฏ Why Mem-Agent?

Most Large Language Models (LLMs) treat every conversation as "new" and don't remember past interactions. Mem-Agent uses a small locally-running model to:

  • โœ… Remember user history - Separate memory for each customer/user
  • โœ… Context awareness - Responds based on previous conversations
  • โœ… Fully local - No internet connection required
  • โœ… Lightweight & fast - Only 2.5 GB model size
  • โœ… Easy integration - Get started with 3 lines of code

๐Ÿš€ Quick Start

1. Install Ollama

# Windows/Mac/Linux: https://ollama.ai/download
curl https://ollama.ai/install.sh | sh

# Start the service
ollama serve

2. Download Model

ollama pull granite4:tiny-h

3. Use Mem-Agent

from mem_llm import MemAgent

# Create agent
agent = MemAgent(model="granite4:tiny-h")

# System check
status = agent.check_setup()
if status['status'] == 'ready':
    print("โœ… System ready!")
else:
    print("โŒ Error:", status)

# Set user
agent.set_user("user123")

# First conversation
response = agent.chat("Hello, my name is Ali")
print(response)

# Second conversation - It remembers me!
response = agent.chat("Do you remember my name?")
print(response)

๐Ÿ“š Example Scripts

1. Simple Test

python examples/example_simple.py

2. Customer Service Simulation

python examples/example_customer_service.py

๐Ÿ—๏ธ Project Structure

Memory LLM/
โ”œโ”€โ”€ memory_llm/              # Main package
โ”‚   โ”œโ”€โ”€ __init__.py          # Package initialization
โ”‚   โ”œโ”€โ”€ mem_agent.py         # Main assistant class
โ”‚   โ”œโ”€โ”€ memory_manager.py    # Memory management
โ”‚   โ”œโ”€โ”€ memory_db.py         # SQL database support
โ”‚   โ”œโ”€โ”€ llm_client.py        # Ollama integration
โ”‚   โ”œโ”€โ”€ memory_tools.py      # User tools
โ”‚   โ”œโ”€โ”€ knowledge_loader.py  # Knowledge base loader
โ”‚   โ”œโ”€โ”€ prompt_templates.py  # Prompt templates
โ”‚   โ””โ”€โ”€ config_manager.py    # Configuration manager
โ”œโ”€โ”€ examples/                # Example scripts
โ”œโ”€โ”€ tests/                   # Test files
โ”œโ”€โ”€ setup.py                 # Installation script
โ”œโ”€โ”€ requirements.txt         # Dependencies
โ””โ”€โ”€ README.md               # This file

๐Ÿ”ง API Usage

MemAgent Class

from mem_llm import MemAgent

agent = MemAgent(
    model="granite4:tiny-h",           # Ollama model name
    memory_dir="memories",             # Memory directory
    ollama_url="http://localhost:11434" # Ollama API URL
)

Basic Methods

# Set user
agent.set_user("user_id")

# Chat
response = agent.chat(
    message="Hello",
    user_id="optional_user_id",  # If set_user not used
    metadata={"key": "value"}     # Additional information
)

# Get memory summary
summary = agent.memory_manager.get_summary("user_id")

# Search in history
results = agent.search_user_history("keyword", "user_id")

# Update profile
agent.update_user_info({
    "name": "Ali",
    "preferences": {"language": "en"}
})

# Get statistics
stats = agent.get_statistics()

# Export memory
json_data = agent.export_memory("user_id")

# Clear memory (WARNING!)
agent.clear_user_memory("user_id", confirm=True)

MemoryManager Class

from mem_llm import MemoryManager

memory = MemoryManager(memory_dir="memories")

# Load memory
data = memory.load_memory("user_id")

# Add interaction
memory.add_interaction(
    user_id="user_id",
    user_message="Hello",
    bot_response="Hello! How can I help you?",
    metadata={"timestamp": "2025-01-13"}
)

# Get recent conversations
recent = memory.get_recent_conversations("user_id", limit=5)

# Search
results = memory.search_memory("user_id", "order")

OllamaClient Class

from mem_llm import OllamaClient

client = OllamaClient(model="granite4:tiny-h")

# Simple generation
response = client.generate("Hello world!")

# Chat format
response = client.chat([
    {"role": "system", "content": "You are a helpful assistant"},
    {"role": "user", "content": "Hello"}
])

# Connection check
is_ready = client.check_connection()

# Model list
models = client.list_models()

๐Ÿ’ก Usage Scenarios

1. Customer Service Bot

  • Remembers customer history
  • Knows previous issues
  • Makes personalized recommendations

2. Personal Assistant

  • Tracks daily activities
  • Learns preferences
  • Makes reminders

3. Education Assistant

  • Tracks student progress
  • Adjusts difficulty level
  • Remembers past mistakes

4. Support Ticket System

  • Stores ticket history
  • Finds related old tickets
  • Provides solution suggestions

๐Ÿ“Š Memory Format

Memories are stored in JSON format:

{
  "conversations": [
    {
      "timestamp": "2025-01-13T10:30:00",
      "user_message": "Hello",
      "bot_response": "Hello! How can I help you?",
      "metadata": {
        "topic": "greeting"
      }
    }
  ],
  "profile": {
    "user_id": "user123",
    "first_seen": "2025-01-13T10:30:00",
    "preferences": {},
    "summary": {}
  },
  "last_updated": "2025-01-13T10:35:00"
}

๐Ÿ”’ Privacy and Security

  • โœ… Works completely locally (no internet connection required)
  • โœ… Data stored on your computer
  • โœ… No data sent to third-party services
  • โœ… Memories in JSON format, easily deletable

๐Ÿ› ๏ธ Development

Test Mode

# Simple chat without memory (for testing)
response = agent.simple_chat("Test message")

Using Your Own Model

# Different Ollama model
agent = MemAgent(model="llama2:7b")

# Or another LLM API
# Customize llm_client.py file

๐Ÿ› Troubleshooting

Ollama Connection Error

# Start Ollama service
ollama serve

# Port check
netstat -an | findstr "11434"

Model Not Found

# Check model list
ollama list

# Download model
ollama pull granite4:tiny-h

Memory Issues

# Check memory directory
import os
os.path.exists("memories")

# List memory files
os.listdir("memories")

๐Ÿ“ˆ Performance

  • Model Size: ~2.5 GB
  • Response Time: ~1-3 seconds (depends on CPU)
  • Memory Usage: ~4-6 GB RAM
  • Disk Usage: ~10-50 KB per user

๐Ÿค Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'feat: Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open Pull Request

๐Ÿ“ License

MIT License - See LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ“ž Contact

You can open an issue for your questions.


Note: This project is for educational and research purposes. Please perform comprehensive testing before using in production environment.

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

mem_llm-1.0.3.tar.gz (46.4 kB view details)

Uploaded Source

Built Distribution

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

mem_llm-1.0.3-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file mem_llm-1.0.3.tar.gz.

File metadata

  • Download URL: mem_llm-1.0.3.tar.gz
  • Upload date:
  • Size: 46.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mem_llm-1.0.3.tar.gz
Algorithm Hash digest
SHA256 54b6cb64cc80b4703fa57b9c033f3229bf516e962b2b81902549a28730227535
MD5 c25147710bd62499b25b4e4f8231ffd5
BLAKE2b-256 5591b0270537772fb47c18d0578589ecc34c96f81e38f81343c2fdced8d161cc

See more details on using hashes here.

File details

Details for the file mem_llm-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: mem_llm-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 28.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mem_llm-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 21d59381ed56d1338093b52775bea5ad83d2fa5d5fcbbddec9e927c05b9300c5
MD5 4f52c94bbf844fa1590806279cb4ed73
BLAKE2b-256 4d310e60366c763f3ccef0aabd9bc28bc5dece61b3c2fe47267128c5ab0042ed

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