Agent Wranglers

WEB-250Optional

Search by meaning when the collection is small

When a site has 5,000 items or fewer, rank search results by meaning as well as by words.

How to do it

Embed each item's title, summary and aliases at build time, and store the vectors with the site. Embed the query, rank by similarity, and merge with word matches, as Search by meaning and by words says. A few thousand vectors fit in memory, so no vector database is needed. Record the embedding model with the vectors, as Record the embedding model with every vector says. Measure it as Measure retrieval on its own says.

Examples

JavaScript example

const cosine = (a, b) => a.reduce((sum, x, i) => sum + x * b[i], 0);
export const byMeaning = (queryVector, items) => items.map(item => ({ item, score: cosine(queryVector, item.vector) })).sort((a, b) => b.score - a.score);

Python example

import numpy as np

def by_meaning(query_vector: np.ndarray, vectors: np.ndarray, items: list[dict]) -> list[tuple[dict, float]]:
    scores = vectors @ query_vector
    return [(items[i], float(scores[i])) for i in np.argsort(-scores)]

How to check

A set of 10 paraphrased questions, none sharing a title word with its answer, returns the right item in the top three for each.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/ready-for-ai-agents/small-semantic-search">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.