RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /OpenCLaw: Building a Personal AI Agent
  6. /Ch. 6
OpenCLaw: Building a Personal AI Agent

06. Vector Store for Long-Term

Chapter 6 of 24 · 15 min
KEY INSIGHT

Vector embeddings enable semantic similarity search across large memory stores, allowing the agent to retrieve relevant past information without exact keyword matches. Long-term memory must support complex queries over accumulated knowledge. A user asking about "the Python project we worked on last month" expects the agent to identify relevant conversations without explicit project naming. Vector similarity search solves this problem by representing memories as points in a high-dimensional space. Embedding Generation Each memory entry receives a vector embedding that captures its semantic meaning. The embedding model converts text into a fixed-length numeric vector. Similar concepts produce vectors with small angular distances. This property enables retrieval through cosine similarity or dot product comparisons. ```python import numpy as np from openclaw.embeddings import get_embedding_model class VectorStore: def __init__(self, dimension: int, metric: str = "cosine"): self.dimension = dimension self.metric = metric self.vectors: dict[str, np.ndarray] = {} self.embedding_model = get_embedding_model() def add(self, memory_id: str, text: str, metadata: dict) -> None: """Add a memory with its embedding.""" embedding = self.embedding_model.encode(text) self.vectors[memory_id] = embedding # Store metadata and text in SQLite for retrieval self._store_record(memory_id, text, metadata) def search(self, query: str, top_k: int = 10) -> list[dict]: """Find the most similar memories to the query.""" query_embedding = self.embedding_model.encode(query) similarities = [] for memory_id, vector in self.vectors.items(): similarity = self._compute_similarity(query_embedding, vector) similarities.append((memory_id, similarity)) # Return top-k results similarities.sort(key=lambda x: x[1], reverse=True) return [ {**self._get_record(memory_id), "score": score} for memory_id, score in similarities[:top_k] ] def _compute_similarity( self, a: np.ndarray, b: np.ndarray ) -> float: if self.metric == "cosine": return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) return -np.sum((a - b) ** 2) # Euclidean distance ``` Indexing for Scale Naive vector comparison scales poorly with memory size. Production systems use approximate nearest neighbor (ANN) indexes that sacrifice some accuracy for dramatically improved performance. Popular options include FAISS, Annoy, and HNSW. The index must be rebuilt when memories are added or deleted. Incremental updates avoid full rebuilds when possible. Memory constraints may require index partitioning across multiple files.

EXERCISE

Design a hybrid retrieval system that combines vector similarity with keyword matching. Explain how you would weight each component and handle cases where they disagree on relevance.

← Chapter 5
SQLite for Short-Term
Chapter 7 →
Memory Consolidation