Skip to main content

An intelligent memory layer for AI applications with RAG capabilities

Project description

Smriti Memory

An intelligent memory layer for AI applications with RAG (Retrieval-Augmented Generation) capabilities. Smriti Memory provides sophisticated memory management that can store, retrieve, and update contextual information using vector databases and LLM-powered decision making.

🚀 Prerequisites

Before using Smriti Memory, you'll need to obtain API keys from the following services:

Required API Keys

  1. Pinecone API Key - For vector database storage
  2. Groq API Key - For LLM operations (memory decisions and chat)
  3. Gemini API Key - For additional LLM capabilities

Quick Setup

  1. Set Environment Variables (recommended):
export PINECONE_API_KEY="your-pinecone-api-key"
export GROQ_API_KEY="your-groq-api-key"
export GEMINI_KEY="your-gemini-api-key"
  1. Or Pass Keys Directly:
from smriti import MemoryConfig, MemoryManager

config = MemoryConfig(
    pinecone_api_key="your-pinecone-key",
    groq_api_key="your-groq-key", 
    gemini_api_key="your-gemini-key"
)
memory_manager = MemoryManager(config)

📖 Need help getting your API keys? Check out our detailed Setup Guide with step-by-step instructions!

Features

  • 🧠 Intelligent Memory Management: Uses LLM to decide what information to store, update, or ignore
  • 🔍 Semantic Search: Find relevant memories using vector similarity search
  • 🔄 Memory Updates: Automatically detect and update existing memories with new information
  • 📊 Memory Statistics: Track and analyze memory usage patterns
  • 🚀 Easy Integration: Simple API for adding to any AI application
  • 🛠️ CLI Interface: Command-line tools for memory operations
  • ⚙️ Configurable: Flexible configuration for different use cases

Installation

pip install smriti-memory

Development Installation

git clone https://github.com/amanyadav721/smriti.git
cd smriti-memory
pip install -e .

Quick Start

Basic Usage

from smriti import MemoryManager, MemoryConfig

# Initialize with default configuration
memory_manager = MemoryManager()

# Add memory from a chat interaction
chat_thread = [
    {"user": "I like pizza and reading sci-fi books", "ai": "That's great! What's your favorite sci-fi book?"}
]

result = memory_manager.add_memory("user123", chat_thread)
print(result)
# Output: {'success': True, 'memory': [...], 'action': 'added', 'count': 1}

# Search for relevant memories
search_result = memory_manager.search_memories("user123", "pizza")
print(search_result)
# Output: {'success': True, 'results': [...], 'count': 1}

# Search with custom parameters (top_k and namespace)
search_result = memory_manager.search_memories("user123", "pizza", top_k=5, namespace="user_understanding")
print(f"Found {len(search_result['results'])} results")

# Chat with memory context
chat_result = memory_manager.chat_with_memory("user123", "What do I like?")
print(chat_result["response"])
# Output: "Based on our previous conversation, you like pizza and reading sci-fi books..."

CLI Usage

# Add memory
smriti add-memory user123 --chat-thread '[{"user": "I like pizza"}]'

# Search memories
smriti search user123 --query "pizza" --verbose

# Chat with memory
smriti chat user123 --query "What do I like?"

# Get user statistics
smriti stats user123

# Delete all memories
smriti delete user123

Configuration

Environment Variables

Set these environment variables for API access:

export PINECONE_API_KEY="your-pinecone-api-key"
export GROQ_API_KEY="your-groq-api-key"
export GEMINI_KEY="your-gemini-api-key"

Custom Configuration

from smriti import MemoryConfig, MemoryManager

config = MemoryConfig(
    pinecone_api_key="your-key",
    groq_api_key="your-key",
    gemini_api_key="your-key",
    llm_model="llama-3.1-8b-instant",
    llm_temperature=0.3,
    default_namespace="user_understanding",
    max_memory_length=1000,
    similarity_threshold=0.7,
    max_search_results=10
)

memory_manager = MemoryManager(config)

API Reference

MemoryManager

The main class for managing memories.

add_memory(user_id: str, chat_thread: List[Dict[str, Any]]) -> Dict[str, Any]

Add memory based on a chat thread.

Parameters:

  • user_id: Unique identifier for the user
  • chat_thread: List of dictionaries with "user" and/or "ai" keys

Returns:

{
    "success": bool,
    "memory": List[Dict],
    "action": str,  # "added" or "ignored"
    "namespace": str,
    "count": int,
    "storage_result": Dict
}

search_memories(user_id: str, query: str, namespace: Optional[str] = None, top_k: Optional[int] = None) -> Dict[str, Any]

Search for relevant memories.

Parameters:

  • user_id: User identifier
  • query: Search query
  • namespace: Optional namespace to search in
  • top_k: Number of results to return

Returns:

{
    "success": bool,
    "results": List[Dict],
    "query": str,
    "namespace": str,
    "count": int
}

chat_with_memory(user_id: str, query: str, add_to_memory: bool = True) -> Dict[str, Any]

Generate a chat response using memory context.

Parameters:

  • user_id: User identifier
  • query: User's query
  • add_to_memory: Whether to add the interaction to memory

Returns:

{
    "success": bool,
    "response": str,
    "memory_context": Dict,
    "memory_result": Dict
}

delete_user_memories(user_id: str) -> Dict[str, Any]

Delete all memories for a user.

get_user_stats(user_id: str) -> Dict[str, Any]

Get statistics about a user's memories.

MemoryConfig

Configuration class for customizing behavior.

Attributes:

  • pinecone_api_key: Pinecone API key
  • groq_api_key: Groq API key
  • gemini_api_key: Gemini API key
  • llm_model: LLM model name
  • llm_temperature: LLM temperature
  • default_namespace: Default namespace for memories
  • max_memory_length: Maximum length of memory text
  • similarity_threshold: Similarity threshold for search
  • max_search_results: Maximum number of search results

Advanced Usage

Custom Memory Types

# Add memory with custom namespace
chat_thread = [{"user": "I have a meeting on Friday"}]
result = memory_manager.add_memory("user123", chat_thread)

# Search in specific namespace
search_result = memory_manager.search_memories(
    "user123", 
    "meeting", 
    namespace="facts"
)

# Search with custom top_k parameter
search_result = memory_manager.search_memories(
    "user123", 
    "pizza", 
    top_k=3  # Get only top 3 results
)

# Search with both namespace and top_k
search_result = memory_manager.search_memories(
    "user123", 
    "work", 
    namespace="professional",
    top_k=10  # Get top 10 results from professional namespace
)

Batch Operations

# Add multiple memories
chat_threads = [
    [{"user": "I like pizza"}],
    [{"user": "I work at Google"}],
    [{"user": "I have a dog named Max"}]
]

for thread in chat_threads:
    memory_manager.add_memory("user123", thread)

Error Handling

from smriti import SmritiError, MemoryError

try:
    result = memory_manager.add_memory("user123", chat_thread)
    if not result["success"]:
        print(f"Error: {result['error']}")
except SmritiError as e:
    print(f"Smriti error: {e.message}")
    print(f"Details: {e.details}")

CLI Commands

smriti add-memory

Add memory from a chat thread.

smriti add-memory user123 --chat-thread '[{"user": "I like pizza"}]'

smriti search

Search for memories.

smriti search user123 --query "pizza" --namespace "user_understanding" --top-k 5

smriti chat

Chat with memory context.

smriti chat user123 --query "What do I like?" --no-memory

smriti delete

Delete all memories for a user.

smriti delete user123

smriti stats

Get user memory statistics.

smriti stats user123 --verbose

Examples

Chatbot Integration

from smriti import MemoryManager

class Chatbot:
    def __init__(self):
        self.memory_manager = MemoryManager()
    
    def respond(self, user_id: str, message: str) -> str:
        # Get response with memory context
        result = self.memory_manager.chat_with_memory(user_id, message)
        
        if result["success"]:
            return result["response"]
        else:
            return "I'm sorry, I encountered an error. Please try again."
    
    def add_memory(self, user_id: str, user_message: str, ai_response: str):
        chat_thread = [{"user": user_message, "ai": ai_response}]
        self.memory_manager.add_memory(user_id, chat_thread)

# Usage
chatbot = Chatbot()
response = chatbot.respond("user123", "What do I like?")
print(response)

Web Application Integration

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from smriti import MemoryManager

app = FastAPI()
memory_manager = MemoryManager()

class ChatRequest(BaseModel):
    user_id: str
    message: str

@app.post("/chat")
async def chat(request: ChatRequest):
    result = memory_manager.chat_with_memory(request.user_id, request.message)
    
    if result["success"]:
        return {"response": result["response"]}
    else:
        raise HTTPException(status_code=500, detail=result["error"])

@app.get("/memories/{user_id}")
async def get_memories(user_id: str, query: str):
    result = memory_manager.search_memories(user_id, query)
    return result

Contributing

  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

Development

Setup Development Environment

git clone https://github.com/amanyadav721/smriti.git
cd smriti-memory
pip install -e ".[dev]"

Running Tests

pytest

Code Formatting

black smriti/
flake8 smriti/
mypy smriti/

Building Documentation

pip install -e ".[docs]"
cd docs
make html

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built with Pinecone for vector storage
  • Powered by Groq for fast LLM inference
  • Uses Google Gemini for embeddings
  • Inspired by research in memory-augmented neural networks

Support

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

smriti_memory-0.2.0.tar.gz (79.3 kB view details)

Uploaded Source

Built Distribution

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

smriti_memory-0.2.0-py3-none-any.whl (77.7 kB view details)

Uploaded Python 3

File details

Details for the file smriti_memory-0.2.0.tar.gz.

File metadata

  • Download URL: smriti_memory-0.2.0.tar.gz
  • Upload date:
  • Size: 79.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for smriti_memory-0.2.0.tar.gz
Algorithm Hash digest
SHA256 349a6729e04003e8fd6d6abb146ad60471dcef2249a4b272e841297d09bacf5a
MD5 dfe33eb5e6d57a123074829255fbe7a0
BLAKE2b-256 80cad3228febc26dae1b8fccc7165699949a5ab39c15e25e481a3a3216c2c108

See more details on using hashes here.

File details

Details for the file smriti_memory-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: smriti_memory-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 77.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for smriti_memory-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a01cc47745f58dbaeeb27c0be34becdb0215cf898fcc27c403b96cbf6b10dbcb
MD5 72ef694fcb704588e9aaa64375d143b2
BLAKE2b-256 e4a6ff975ebd412611345d5bbcf7ba450f94772a268a91d2f0333db8879433fb

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