Search in AI: retrieval, metrics and planning

Search in AI: retrieval, metrics and planning

Learning materials

Retrieval: candidates → ranking → evaluation

Vector similarity measures geometric proximity. Relevance means a document is useful for a particular query. A nearby vector need not represent a useful or reliable document.

Search in AI: retrieval, metrics and planning: mechanism diagram with an example

Vectors and search cost

  • Dot product: x·y = Σx_i y_i.
  • L2 norm: ||x|| = √Σx_i².
  • Cosine similarity: (x·y) / (||x|| ||y||). For [2, 0] and [1, 1], this gives 2 / (2√2) = 1/√2. The formula is undefined for a zero vector.
  • For unit vectors, cosine = dot product, and squared Euclidean distance = 2 − 2 × dot.
  • Exhaustively calculating dot products against N vectors of dimension d requires order N × d operations.

Generating candidates

MethodMechanism and limitation
BM25 (Best Match 25)Term matching, frequency saturation and length normalization. Each additional occurrence adds less score. b = 0 disables length normalization.
Bi-encoderEncodes queries and documents separately; document vectors can be precomputed.
ANN / HNSW (Hierarchical Navigable Small World)ANN (approximate nearest neighbours) is approximate nearest-neighbor search; HNSW uses a vector graph. It can miss some exact neighbors.
Cross-encoderProcesses a query–document pair jointly, so it usually reranks a short candidate list.

In HNSW, higher search ef usually improves recall at a latency cost. Higher M increases graph connectivity and index memory. These control different things: traversal of an existing index and the index structure.

Combining retrievers and HyDE

BM25 and cosine scores use different scales. A plain sum can give one retriever disproportionate influence. RRF (Reciprocal Rank Fusion) combines ranks: score = Σ1/(c + rank).

A document at ranks 2 and 4 with c = 8 gets 1/10 + 1/12 ≈ 0.1833. Calculate the two contributions separately; do not add ranks inside one denominator.

HyDE (Hypothetical Document Embeddings) creates a hypothetical document and retrieves using its embedding. An incorrect assumption can steer retrieval toward the wrong topic. Measure its benefit against the original query on the same labeled set.

Precision, recall and F1

CountMeaning
TP (True Positives)Correctly predicted positive cases.
FP (False Positives)Cases incorrectly predicted positive.
FN (False Negatives)Missed positive cases.

With TP = 6, FP = 2 and FN = 4:

  • Precision = TP/(TP+FP) = 6/8 = 0.75: the correct fraction of predicted positives.
  • Recall = TP/(TP+FN) = 6/10 = 0.6: the found fraction of actual positives.
  • F1 = 2PR/(P+R) ≈ 0.667: the harmonic mean of precision and recall.

Macro-F1 averages per-class F1 with equal weight. Micro-F1 sums TP/FP/FN across classes first, then calculates F1. Macro-F1 is not the F1 of averaged precision and recall.

Rank and confidence

MetricWhat it checks
ANN recall@kOverlap with the k exact neighbors. It does not establish relevance to users.
MRR (Mean Reciprocal Rank)Mean 1/rank of the first relevant result; an absent result contributes 0. For ranks 2 and 4: (1/2 + 1/4)/2 = 0.375.
NDCG (Normalized Discounted Cumulative Gain)DCG (Discounted Cumulative Gain)/IDCG (Ideal Discounted Cumulative Gain): ranking quality using relevance and position, relative to the ideal ordering. 3/4 gives 0.75.
CalibrationAgreement between probabilities and event frequencies. If predictions of 0.7 occur only 40% of the time, confidence in that range is too high.

Path and plan search

AlgorithmSelection ruleCondition
BFS (Breadth-First Search)Next layer by edge count.Fewest actions; lowest cost only when edges have equal positive cost.
DijkstraLowest accumulated cost g.Nonnegative edge costs.
A*Lowest f = g + h; h estimates the remaining path.Guarantees depend on the heuristic and handling of repeated states.

S→G costs 10, while S→A→G costs 2 + 2 = 4. BFS favors one edge; cost-based search favors the two cheaper edges.

  • Admissibility: h never overestimates the true remainder. An estimate of 9 is inadmissible if the best remaining path costs 8.
  • Consistency: h(u) ≤ cost(u,v) + h(v). With g(v) = g(u) + cost(u,v), this implies f(v) ≥ f(u).
  • For (g,h) = (3,4), f = 7; for (5,1), f = 6. A* considers the smaller f first.
  • A state must include everything affecting future actions: location and remaining budget if the budget constrains transitions.
  • Topological sorting orders acyclic dependencies. If A depends on B and B depends on A, the cycle prevents a valid order.

A plan is valid relative to its world model. Actual state, action availability and permissions must be checked before execution.