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 memory_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 memory_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 memory_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 memory_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.0.tar.gz (43.8 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.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mem_llm-1.0.0.tar.gz
Algorithm Hash digest
SHA256 84ac9ef695b6a8ebe1954e1d977717c14a3f9475207f43bb9e5ce04c4fd9ef9d
MD5 798f7fe325584b646dd1dadc9bb2d26f
BLAKE2b-256 9749a5788272238d324a6f56ddedbc3d8dc4d7aba53be5ad1a69a4608c860637

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mem_llm-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 583facfa3d60d5402b43686d79e908865468e1361197f328b69a9eab07ac7304
MD5 0a82ffecefd1c4c26d70b548f3b0f411
BLAKE2b-256 fff011f88184d98d20a8eb687619e52fdaf41cdf59a9c2de908b5fb875058526

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