簡介
在大模型應用里,將文本數據分塊嵌入存儲在向量數據庫已經是標準做法。然而,傳統向量數據庫雖然功能強大,但其高昂的RAM和存儲需求,以及復雜的部署運維,常常讓開發者望而卻步。今天,介紹一個名為?Memvid?的開源項目,它提出了一個有趣的思路將文本數據巧妙地“存入”視頻文件,完成信息的高效檢索。
Memvid是一種革命性的視頻化AI記憶庫,它通過將文本數據壓縮成MP4格式的視頻,實現在數百萬文本塊中進行極速語義搜索。傳統的向量數據庫需要大量的RAM和存儲空間,而Memvid則通過視頻文件高效壓縮數據,極大地降低了存儲需求,同時保留了秒級檢索速度。它將AI的記憶管理從傳統的數據庫模式轉變為視頻化存儲,改變了大規模AI記憶管理的方式。這意味著,你不再需要依賴龐大而昂貴的數據庫服務器,只需管理普通的視頻文件即可,并且這也讓離線部署更輕量化。
它一次性解決傳統文本存儲的三大痛點:存儲冗余、檢索延遲和網絡依賴,具備以下的顯著優勢:
- 極致存儲效率?:利用視頻壓縮技術,存儲空間可比傳統方案節省高達10倍。
- 閃電語義搜索?:歸功于預計算的語義索引和視頻幀的快速跳轉能力。
- 零基建,離線優先?:知識庫就是視頻和索引文件,易于分發,生成后即可完全離線運行。
📦 安裝
安裝需先安裝 zbar 依賴,提供了編碼、聊天、檢索等功能,性能方面隨數據集增大,編碼時間增長,搜索時間和存儲占用合理,還支持自定義嵌入、視頻優化等高級配置。
快速安裝
pip install memvid
對于 PDF 支持
pip install memvid PyPDF2
推薦設置(虛擬環境)
# Create a new project directory
mkdir my-memvid-project
cd my-memvid-project# Create virtual environment
python -m venv venv# Activate it
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activate# Install memvid
pip install memvid# For PDF support:
pip install PyPDF2
🎯 快速開始
基本用法
from memvid import MemvidEncoder, MemvidChat# Create video memory from text chunks
chunks = ["Important fact 1", "Important fact 2", "Historical event details"]
encoder = MemvidEncoder()
encoder.add_chunks(chunks)
encoder.build_video("memory.mp4", "memory_index.json")# Chat with your memory
chat = MemvidChat("memory.mp4", "memory_index.json")
chat.start_session()
response = chat.chat("What do you know about historical events?")
print(response)
從文檔構建內存
from memvid import MemvidEncoder
import os# Load documents
encoder = MemvidEncoder(chunk_size=512, overlap=50)# Add text files
for file in os.listdir("documents"):with open(f"documents/{file}", "r") as f:encoder.add_text(f.read(), metadata={"source": file})# Build optimized video
encoder.build_video("knowledge_base.mp4","knowledge_index.json",fps=30, # Higher FPS = more chunks per secondframe_size=512 # Larger frames = more data per frame
)
高級搜索和檢索
from memvid import MemvidRetriever# Initialize retriever
retriever = MemvidRetriever("knowledge_base.mp4", "knowledge_index.json")# Semantic search
results = retriever.search("machine learning algorithms", top_k=5)
for chunk, score in results:print(f"Score: {score:.3f} | {chunk[:100]}...")# Get context window
context = retriever.get_context("explain neural networks", max_tokens=2000)
print(context)
交互式聊天界面
from memvid import MemvidInteractive# Launch interactive chat UI
interactive = MemvidInteractive("knowledge_base.mp4", "knowledge_index.json")
interactive.run() # Opens web interface at http://localhost:7860
使用 file_chat.py 進行測試
該腳本提供了一種使用您自己的文檔測試 Memvid 的全面方法:examples/file_chat.py
# Process a directory of documents
python examples/file_chat.py --input-dir /path/to/documents --provider google# Process specific files
python examples/file_chat.py --files doc1.txt doc2.pdf --provider openai# Use H.265 compression (requires Docker)
python examples/file_chat.py --input-dir docs/ --codec h265 --provider google# Custom chunking for large documents
python examples/file_chat.py --files large.pdf --chunk-size 2048 --overlap 32 --provider google# Load existing memory
python examples/file_chat.py --load-existing output/my_memory --provider google
完整示例:與 PDF 書籍聊天
# 1. Create a new directory and set up environment
mkdir book-chat-demo
cd book-chat-demo
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate# 2. Install dependencies
pip install memvid PyPDF2# 3. Create book_chat.py
cat > book_chat.py << 'EOF'
from memvid import MemvidEncoder, chat_with_memory
import os# Your PDF file
book_pdf = "book.pdf" # Replace with your PDF path# Build video memory
encoder = MemvidEncoder()
encoder.add_pdf(book_pdf)
encoder.build_video("book_memory.mp4", "book_index.json")# Chat with the book
api_key = os.getenv("OPENAI_API_KEY") # Optional: for AI responses
chat_with_memory("book_memory.mp4", "book_index.json", api_key=api_key)
EOF# 4. Run it
export OPENAI_API_KEY="your-api-key" # Optional
python book_chat.py
🛠? 高級配置
自定義嵌入
from sentence_transformers import SentenceTransformer# Use custom embedding model
custom_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
encoder = MemvidEncoder(embedding_model=custom_model)
視頻優化
# For maximum compression
encoder.build_video("compressed.mp4","index.json",fps=60, # More frames per secondframe_size=256, # Smaller framesvideo_codec='h265', # Better compressioncrf=28 # Compression quality (lower = better quality)
)
分布式處理
# Process large datasets in parallel
encoder = MemvidEncoder(n_workers=8)
encoder.add_chunks_parallel(massive_chunk_list)
🐛 故障 排除
常見問題
ModuleNotFoundError:沒有名為 memvid
的模塊
# Make sure you're using the right Python
which python # Should show your virtual environment path
# If not, activate your virtual environment:
source venv/bin/activate # On Windows: venv\Scripts\activate
ImportError: PDF 支持需要 PyPDF2
pip install PyPDF2
LLM API 關鍵問題
# Set your API key (get one at https://platform.openai.com)
export GOOGLE_API_KEY="AIzaSyB1-..." # macOS/Linux
# Or on Windows:
set GOOGLE_API_KEY=AIzaSyB1-...
大型 PDF 處理
# For very large PDFs, use smaller chunk sizes
encoder = MemvidEncoder()
encoder.add_pdf("large_book.pdf", chunk_size=400, overlap=50)
參考文章
Olow304/memvid: 基于視頻的 AI 內存庫。通過閃電般的快速語義搜索將數百萬個文本塊存儲在 MP4 文件中。無需數據庫。
Memvid - Video-Based AI Memory ,使用視頻代替數據庫,Memvid讓AI記憶更高效、存儲更節省
Memvid:把你的文檔庫變成一個小巧的視頻,還能瞬間搜出你想要的! - 文章 - 開發者社區 - 火山引擎
GitHub 1.5k Star!視頻秒變數據庫:你的AI視頻記憶庫,知識不再丟失! - 知乎