Metadata-Version: 2.4
Name: llm-long-term-memory
Version: 0.1.0
Summary: Give your AI agents persistent, searchable long-term memory with pluggable storage backends
Project-URL: Homepage, https://github.com/Devparihar5/llm-long-term-memory
Project-URL: Documentation, https://github.com/Devparihar5/llm-long-term-memory#readme
Project-URL: Repository, https://github.com/Devparihar5/llm-long-term-memory.git
Project-URL: Issues, https://github.com/Devparihar5/llm-long-term-memory/issues
Project-URL: Changelog, https://github.com/Devparihar5/llm-long-term-memory/releases
Author-email: Devendra Parihar <devendraparihar340@gmail.com>
Maintainer-email: Devendra Parihar <devendraparihar340@gmail.com>
License: MIT
License-File: LICENSE
Keywords: ai-agents,chatbot,faiss,langchain,llm,long-term-memory,memory,openai,rag,vector-search
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Requires-Dist: faiss-cpu>=1.7.4
Requires-Dist: langchain-openai>=0.0.5
Requires-Dist: langchain>=0.1.0
Requires-Dist: numpy>=1.24.0
Requires-Dist: openai>=1.0.0
Requires-Dist: pydantic>=2.0.0
Provides-Extra: all
Requires-Dist: psycopg2-binary>=2.9.0; extra == 'all'
Requires-Dist: pymongo>=4.0.0; extra == 'all'
Requires-Dist: redis>=4.0.0; extra == 'all'
Provides-Extra: dev
Requires-Dist: black>=23.0.0; extra == 'dev'
Requires-Dist: isort>=5.0.0; extra == 'dev'
Requires-Dist: mypy>=1.0.0; extra == 'dev'
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
Requires-Dist: pytest>=7.0.0; extra == 'dev'
Provides-Extra: mongodb
Requires-Dist: pymongo>=4.0.0; extra == 'mongodb'
Provides-Extra: postgresql
Requires-Dist: psycopg2-binary>=2.9.0; extra == 'postgresql'
Provides-Extra: redis
Requires-Dist: redis>=4.0.0; extra == 'redis'
Provides-Extra: streamlit
Requires-Dist: streamlit>=1.30.0; extra == 'streamlit'
Description-Content-Type: text/markdown

# 🧠 LLM Long-Term Memory

[![PyPI version](https://badge.fury.io/py/llm-long-term-memory.svg)](https://badge.fury.io/py/llm-long-term-memory)
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**Give your AI agents persistent, searchable long-term memory with pluggable storage backends.**

A sophisticated memory storage and retrieval system that provides LLMs with persistent, searchable long-term memory capabilities. This system can extract, store, update, and retrieve memories from conversations, enabling AI agents to maintain context across multiple sessions.

## ✨ Features

- 🧠 **Intelligent Memory Extraction** - Automatically extracts factual information from conversations using OpenAI GPT
- 🔍 **Semantic Search** - Vector-based similarity search using OpenAI embeddings and FAISS
- 💾 **Pluggable Storage Backends** - SQLite, PostgreSQL, MongoDB, and Redis support
- 🔄 **Memory Management** - Add, update, and delete memories with conflict resolution
- 📊 **Category Organization** - Automatic categorization of memories
- ⚡ **Importance Scoring** - Weighted importance system for memory prioritization
- 🔗 **LangChain Integration** - Built with LangChain for robust LLM interactions

## 📦 Installation

```bash
# Basic installation (SQLite backend)
pip install llm-long-term-memory

# With PostgreSQL support
pip install llm-long-term-memory[postgresql]

# With MongoDB support
pip install llm-long-term-memory[mongodb]

# With Redis support
pip install llm-long-term-memory[redis]

# With all backends
pip install llm-long-term-memory[all]

# With Streamlit UI
pip install llm-long-term-memory[streamlit]
```

## 🚀 Quick Start

```python
from llm_memory import LongTermMemorySystem

# Initialize with SQLite (default)
memory = LongTermMemorySystem(openai_api_key="your-api-key")

# Process a message and extract memories
result = memory.process_message(
    "I use VS Code for Python development and prefer dark mode", 
    user_id="user123"
)
print(f"Extracted {len(result['new_memories'])} memories")

# Query memories
answer = memory.answer_with_memory("What IDE do I use?")
print(answer)  # "You use VS Code for Python development"

# Get all memories
memories = memory.get_all_memories()
for mem in memories:
    print(f"- {mem.content} (importance: {mem.importance})")
```

## 💾 Storage Backends

### SQLite (Default)

```python
from llm_memory import LongTermMemorySystem

memory = LongTermMemorySystem(
    openai_api_key="...",
    storage_backend="sqlite",
    storage_config={"db_path": "my_memories.db"}
)
```

### PostgreSQL

```python
from llm_memory import LongTermMemorySystem

memory = LongTermMemorySystem(
    openai_api_key="...",
    storage_backend="postgresql",
    storage_config={
        "connection_string": "postgresql://user:password@localhost:5432/memory_db"
    }
)
```

### MongoDB

```python
from llm_memory import LongTermMemorySystem

memory = LongTermMemorySystem(
    openai_api_key="...",
    storage_backend="mongodb",
    storage_config={
        "connection_string": "mongodb://localhost:27017",
        "database": "memory_db",
        "collection": "memories"
    }
)
```

### Redis

```python
from llm_memory import LongTermMemorySystem

memory = LongTermMemorySystem(
    openai_api_key="...",
    storage_backend="redis",
    storage_config={
        "host": "localhost",
        "port": 6379,
        "password": "optional_password"
    }
)
```

### Custom Backend

```python
from llm_memory import StorageBackend, Memory, LongTermMemorySystem

class MyCustomBackend(StorageBackend):
    def init_storage(self) -> None:
        # Initialize your storage
        pass
    
    def save_memory(self, memory: Memory) -> None:
        # Save memory
        pass
    
    def get_memory(self, memory_id: str) -> Memory:
        # Get memory by ID
        pass
    
    def get_all_memories(self) -> list:
        # Get all memories
        pass
    
    def delete_memory(self, memory_id: str) -> bool:
        # Delete memory
        pass
    
    def search_memories(self, query: str, category: str = None) -> list:
        # Search memories
        pass
    
    def close(self) -> None:
        # Close connections
        pass

# Use your custom backend
memory = LongTermMemorySystem(
    openai_api_key="...",
    storage_backend=MyCustomBackend()
)
```

## 📊 Memory Structure

```python
from llm_memory import Memory

# Each memory contains:
memory = Memory(
    id="unique_id",
    content="User prefers dark mode",
    category="preferences",
    importance=0.8,
    timestamp="2024-01-15T10:30:00",
    embedding=[...],  # Vector embedding
    metadata={"user_id": "user123", "source": "chat"}
)
```

## 🔧 API Reference

### LongTermMemorySystem

```python
# Initialize
memory = LongTermMemorySystem(
    openai_api_key: str,              # Required: OpenAI API key
    storage_backend: str = "sqlite",   # Backend type or instance
    storage_config: dict = None,       # Backend-specific config
    embedding_model: str = "text-embedding-3-small",
    llm_model: str = "gpt-3.5-turbo",
)

# Methods
memory.process_message(message, user_id, context)  # Extract memories from message
memory.query_memories(query, k=5)                  # Semantic search for memories
memory.answer_with_memory(question, max_memories)  # Answer using memory context
memory.get_all_memories()                          # Get all stored memories
memory.delete_memory(memory_id)                    # Delete specific memory
memory.get_memory_stats()                          # Get statistics
memory.close()                                     # Close connections
```

### Context Manager Support

```python
from llm_memory import LongTermMemorySystem

with LongTermMemorySystem(openai_api_key="...") as memory:
    memory.process_message("Hello!", user_id="user123")
    # Connections automatically closed when done
```

## 🎯 Use Cases

1. **Personal AI Assistants** - Remember user preferences, habits, and information
2. **Customer Service Bots** - Maintain customer history and preferences  
3. **Educational AI** - Track learning progress and personalized content
4. **Productivity Tools** - Remember user workflows and tool preferences
5. **Healthcare AI** - Maintain patient information (with proper security)

## 🌐 Web Interface

If you installed with `[streamlit]`:

```bash
# Clone the repo for the app.py
git clone https://github.com/Devparihar5/llm-long-term-memory.git
cd llm-long-term-memory

# Run the Streamlit app
streamlit run app.py
```

## 🔒 Security Considerations

- Store API keys securely (use environment variables)
- Use secure connection strings for databases
- Consider encryption for sensitive memories
- Implement user authentication for multi-user scenarios

## 🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

## 📄 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## 🙏 Acknowledgments

- Built with [LangChain](https://github.com/langchain-ai/langchain)
- Vector search powered by [FAISS](https://github.com/facebookresearch/faiss)
- Embeddings by [OpenAI](https://openai.com)

---

**Made with ❤️ by [Devendra Parihar](https://github.com/Devparihar5)**

*Contributions by [Divya](https://github.com/piechartXdata) ✨*
