lookup method

Future<String?> lookup(
  1. String prompt
)

The cached response for a prompt similar to prompt, or null.

Implementation

Future<String?> lookup(String prompt) async {
  // `document`, matching [put]: this compares a prompt with stored prompts,
  // not a query with documents. An asymmetric embedder (Voyage and friends)
  // gives a text different vectors per purpose, so mixing them here would
  // put self-similarity below any sensible threshold and never hit.
  final vector = await embedder.embedOne(prompt);
  final found = await store.query(
    CollectionQuery(
      vector: vector,
      filter: _scope(),
      k: 1,
      includeText: false,
    ),
  );
  final best = found.isEmpty ? null : found.first;
  final similarity = best?.vectorScore;
  // Negated deliberately: a NaN similarity (a zero-norm embedding gives
  // one) must count as a miss, and `similarity < threshold` would not.
  if (best == null || similarity == null || !(similarity >= threshold)) {
    misses++;
    return null;
  }
  final response = best.metadata?['response'];
  if (response is! String || response.isEmpty) {
    misses++;
    return null;
  }
  hits++;
  return response;
}