README.md

tangled-discover-embeddings #

Network-hostable store of the embeddings that power Tangled's Discover recommendation engine. Each repo README and each open issue in the Tangled network is embedded once (Gemini gemini-embedding-001, 1536-d, L2-normalized); this repo publishes those vectors as plain files so the recommendation index is rebuildable by anyone and no single database is the source of truth.

The source data (repos, issues) already lives on ATProto in PDSes. Embeddings were the one derived artifact that previously lived only in a central Postgres. This repo moves them into the network: clone it, load the matrices into memory, and you can serve recommendations with no database at all.

Layout #

manifest.json          # model, dim, metric, per-section counts + sha256
data/
  repos.f32.npy        # (N_repos, 1536) float32, L2-normalized — one matrix, all repos
  repos.jsonl          # one metadata line per row, aligned to the matrix (row i ↔ V[i])
  issues.f32.npy       # (N_issues, 1536) float32 — all open issues
  issues.jsonl         # one metadata line per row
scripts/
  export_embeddings.py # (re)generate data/ from the DB — READ-ONLY
  load.py              # load + brute-force cosine kNN (numpy only)
  search_example.py    # demo: nearest repos to a given repo, files-only

It is not one file per embedding and not CSV: a single dense .npy matrix per section (loads in one call, ready for a matmul) plus a human-readable .jsonl sidecar.

Format #

  • Vectors: 1536-d float32, L2-normalized, so cosine similarity = dot product and distance = 1 - score (identical to pgvector's <=>). metric: cosine.
  • Alignment: matrix row i corresponds to line i of the sidecar (row field).
  • repos.jsonl: row, subject_uri, repo_did, repo_name, owner_handle, description, topics, created_at, content_len, content_sha500, embedding_model, embedded_at.
    • content_len → minimum-README quality gate; content_sha500 (md5(content[:500])) → fork dedup. (Full README text is intentionally not shipped.)
  • issues.jsonl: row, subject_uri (issue uri), repo_did, rkey, repo_uri, author_did, title, body, owner_handle, repo_name, repo_description, created_at, embedding_model.
    • Only issues whose identity fully resolves are included (the set the engine can emit).

The data is complete and unfiltered — quality gates (min-README, test-issue filter, own-repo exclusion) are policy applied by the consumer at query time, not baked in here.

Consuming (load once at boot, then in-memory) #

from scripts.load import load_section, knn          # numpy only — no DB, no network
V, meta, by_did = load_section(".", "repos")         # once at startup (~28 MB)
q = V[by_did[seed_repo_did]]                          # a seed repo's vector
hits = knn(V, meta, q, k=40, exclude_dids={seed_repo_did})
# hits: [(meta_row, distance), ...] nearest first — same shape the rec engine's
# db.knn_repos() returns, so merge/dedup/rank/quality downstream are unchanged.

Issue recommendations: load the issues section and matmul the user's seed README vector (from V) against the issues matrix. No per-request I/O; the only cost is the one-time load at boot.

Updating #

Re-run the export against a synced DB, commit, and push to the knot:

DB_CONNECTION_STRING=... python scripts/export_embeddings.py
git add -A && git commit -m "refresh embeddings" && git push

A push to the hosting knot emits a sh.tangled.git.refUpdate record on the ATProto firehose — consumers can subscribe to that as a "re-pull + reload" signal (or just refresh on a timer). Between refreshes consumers serve a slightly stale snapshot, which is fine for recommendations.

Trust & reproducibility #

Owned by the publishing DID (whoever controls the hosting knot repo); consumers trust that DID's embeddings, the same way they'd trust any ATProto record's author. Vectors are reproducible: gemini-embedding-001, taskType=RETRIEVAL_DOCUMENT, output dimensionality 1536, L2-normalized.

Scale note #

At a few thousand vectors, brute-force kNN (one matmul) is sub-10 ms — no ANN index is needed. If the corpus grows past ~1M, add a prebuilt hnswlib/FAISS index file and shard the .npy/consider git-LFS.