A library for storing and retrieving embeddings with PGVector (with Chunking)
Project description
Mem Vault
A Python library for efficient vector-based memory storage using PostgreSQL and pgvector. Perfect for LLM applications that need to maintain conversation context and retrieve relevant information.
Features
- Vector similarity search using pgvector
- Easy PostgreSQL integration
- Conversation-based memory management
- Metadata support for rich memory context
- Bulk operations support
- Automatic cleanup of old conversations
Installation
pip install mem-vault
Prerequisites
- PostgreSQL 11 or later
- pgvector extension
Install pgvector on your PostgreSQL instance:
CREATE EXTENSION vector;
Quick Start
from mem_vault import VectorMemory
# Initialize connection
memory = VectorMemory.initialize(
host="localhost",
port=5432,
database="your_db",
user="your_user",
password="your_password",
openai_api_key="your-openai-key"
)
# Add a memory
memory.add_memory(
conversation_id="user_123",
content="The user prefers dark mode in applications."
)
# Retrieve relevant memories
relevant_memories = memory.get_relevant_memories(
conversation_id="user_123",
query="What are the user's UI preferences?",
limit=5
)
# Print retrieved memories
for memory in relevant_memories:
print(memory['content'])
Advanced Usage
Adding Memories with Metadata
# Add memory with metadata
memory.add_memory(
conversation_id="user_123",
content="User selected blue theme",
metadata={
"category": "preferences",
"timestamp": "2024-01-18T10:30:00",
"source": "settings_page"
}
)
Bulk Operations
# Add multiple memories at once
memory.add_memories(
conversation_id="user_123",
contents=[
"User opened dashboard",
"User viewed reports",
"User downloaded PDF"
],
metadata_list=[
{"action": "view", "page": "dashboard"},
{"action": "view", "page": "reports"},
{"action": "download", "type": "pdf"}
]
)
Memory Management
# Delete a conversation
memory.delete_conversation("user_123")
# Clean up old conversations
memory.cleanup_old_conversations(days=30)
# Get conversation statistics
stats = memory.get_conversation_stats("user_123")
Configuration
memory = VectorMemory.initialize(
host="localhost",
port=5432,
database="your_db",
user="your_user",
password="your_password",
openai_api_key="your-openai-key",
# Optional settings
pool_size=5, # Connection pool size
max_overflow=10, # Max additional connections
max_tokens=8191, # Maximum tokens per content
embedding_model="text-embedding-ada-002" # OpenAI embedding model
)
Error Handling
from mem_vault.exceptions import StorageError
try:
memory.add_memory(
conversation_id="user_123",
content="Some content"
)
except StorageError as e:
print(f"Failed to store memory: {e}")
Best Practices
- Use descriptive conversation IDs
- Include relevant metadata with timestamps
- Implement regular cleanup of old conversations
- Use appropriate similarity thresholds for retrieval
- Use bulk operations for multiple memories
- Enable connection pooling for high-traffic applications
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mem_vault-0.1.1.tar.gz.
File metadata
- Download URL: mem_vault-0.1.1.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2111be93516eee269711f2fbdfee14933ffe31767518b95b9a184f2225ae7907
|
|
| MD5 |
04aa3fa529c109869cc32e13410919f4
|
|
| BLAKE2b-256 |
dd20f204cefe27babb954f128445fac44509783d559e680bb0265407aa224964
|
File details
Details for the file mem_vault-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mem_vault-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c07ad8a558a444f9aadbcf96b5c5e50748cd4d8a321f24b8b53f5ea46351a7dd
|
|
| MD5 |
ed48292d70f2d3f76abeda140c099538
|
|
| BLAKE2b-256 |
2e157f4fb6651d9e3ca004d839b59b990f97f13f24f882a297e2831acbb96030
|