To integrate memvid with Ollama, you'll need to modify the chat functionality to use Ollama as the LLM backend. Here's how you can do it:
from memvid import MemvidEncoder
import requests
import json
class OllamaMemvidChat:
def __init__(self, video_path, index_path, model="llama2", ollama_url="http://localhost:11434"):
self.video_path = video_path
self.index_path = index_path
self.model = model
self.ollama_url = ollama_url
# Load the memory index
with open(index_path, 'r') as f:
self.memory_index = json.load(f)
def retrieve_relevant_chunks(self, query, top_k=3):
# This is a simplified retrieval - memvid likely has better methods
# You'd want to use the actual memvid retrieval logic here
relevant_chunks = []
# Implement your retrieval logic based on the memory_index
# For now, returning all chunks as an example
if 'chunks' in self.memory_index:
relevant_chunks = self.memory_index['chunks'][:top_k]
return relevant_chunks
def chat(self, query):
# Retrieve relevant context from memory
context_chunks = self.retrieve_relevant_chunks(query)
context = "\n".join(context_chunks)
# Create prompt with context
prompt = f"""Based on the following context from memory:
{context}
Answer the following question: {query}
Answer:"""
# Call Ollama API
response = requests.post(
f"{self.ollama_url}/api/generate",
json={
"model": self.model,
"prompt": prompt,
"stream": False
}
)
if response.status_code == 200:
return response.json()['response']
else:
return f"Error: {response.status_code} - {response.text}"
# Usage example
if __name__ == "__main__":
# Create video memory
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 using Ollama
chat = OllamaMemvidChat("memory.mp4", "memory_index.json", model="llama2")
response = chat.chat("What do you know about historical events?")
print(response)
For a more complete integration, you might want to extend the original MemvidChat class:
from memvid import MemvidChat
import requests
class OllamaMemvidChat(MemvidChat):
def __init__(self, video_path, index_path, model="llama2", ollama_url="http://localhost:11434"):
super().__init__(video_path, index_path)
self.model = model
self.ollama_url = ollama_url
def generate_response(self, query, context):
"""Override the default LLM generation with Ollama"""
prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"
response = requests.post(
f"{self.ollama_url}/api/generate",
json={
"model": self.model,
"prompt": prompt,
"stream": False
}
)
if response.status_code == 200:
return response.json()['response']
else:
raise Exception(f"Ollama error: {response.status_code}")
To use streaming responses from Ollama:
def chat_stream(self, query):
context_chunks = self.retrieve_relevant_chunks(query)
context = "\n".join(context_chunks)
prompt = f"Context: {context}\n\nQuestion: {query}\n\nAnswer:"
response = requests.post(
f"{self.ollama_url}/api/generate",
json={
"model": self.model,
"prompt": prompt,
"stream": True
},
stream=True
)
for line in response.iter_lines():
if line:
data = json.loads(line)
if 'response' in data:
yield data['response']
Make sure you have:
- Ollama installed and running (
ollama serve)
- A model pulled (e.g.,
ollama pull llama2)
- The memvid library installed
This integration allows you to use Ollama's local LLMs with memvid's video memory system, giving you private, offline-capable memory-augmented chat functionality.