Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xK3vin MegaMemory Semantic Search

From Leeroopedia
Knowledge Sources
Domains NLP, Embeddings, Information_Retrieval
Last Updated 2026-10-27 12:00 GMT

Overview

A retrieval technique that embeds a natural-language query and a corpus into the same vector space, then ranks corpus items by cosine similarity to the query.

Description

Semantic search (also called dense retrieval) replaces lexical matching (BM25, TF-IDF) with a learned embedding model that maps text to dense vectors. The query and each candidate are embedded by the same model; similarity is computed as the cosine of the angle between vectors. Because the model captures meaning rather than surface tokens, it retrieves relevant concepts even when the query uses different vocabulary than the stored text.

Problem solved: In a knowledge graph where concepts are described in natural language (e.g., "Two-pass graph merge with conflict tracking"), an agent asking "how does merging handle edges?" would fail a keyword search because the words differ. Semantic search bridges this vocabulary gap by operating in a shared meaning space.

Context: In MegaMemory, every concept node stores a 384-dimensional embedding of its "kind: name — summary" text. The understand tool embeds the agent's query and brute-force ranks all active nodes by cosine similarity, returning the top K with full context (children, edges, parent). This is the primary retrieval mechanism for pre-task context loading.

Usage

Use this principle when:

  • You store human-readable descriptions and need to retrieve them by meaning, not exact keywords
  • The query vocabulary may differ from the stored descriptions (paraphrase retrieval)
  • The corpus is small-to-medium (brute-force search is acceptable without an ANN index)
  • You need offline/local search with no API keys or network dependency

Theoretical Basis

Given an embedding function ϕ:text→ℝd, semantic search computes:

sim(q,ci)=cos⁡(ϕ(q),ϕ(ci))=ϕ(q)⋅ϕ(ci)‖ϕ(q)‖‖ϕ(ci)‖

and returns the top-K candidates by descending similarity.

Pseudo-code:

# Abstract algorithm (NOT real implementation)
query_vec = embed(query)          # 384-dim, L2-normalized
scores = []
for candidate in corpus:
    cand_vec = candidate.embedding  # precomputed, stored
    scores.append((candidate, cosine(query_vec, cand_vec)))
scores.sort(key=lambda x: -x[1])  # descending
return scores[:top_k]

When the embedding model L2-normalizes outputs (as all-MiniLM-L6-v2 does with normalize: true), the cosine similarity reduces to the dot product, and all vectors lie on the unit hypersphere.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment