retrieve method

Future<List<ScoredChunk>> retrieve(
  1. String query, {
  2. int topK = 5,
  3. double? minScore,
  4. bool where(
    1. Document document
    )?,
})

Embeds query and returns the most similar stored chunks, best first.

Returns an empty list without calling the embedder when the store is empty. See VectorStore.search for topK, minScore and where.

where restricts the search to documents for which it returns true, using each document's Document.metadata. It runs before scoring, so filtered documents cost no similarity computation. Use it to scope a query to one source, language, tenant, or any metadata field you set when adding text.

Implementation

Future<List<ScoredChunk>> retrieve(
  String query, {
  int topK = 5,
  double? minScore,
  bool Function(Document document)? where,
}) async {
  if (await store.count() == 0) return const [];
  final embeddings = await embedder([query]);
  if (embeddings.length != 1) {
    throw StateError(
      'Embedder returned ${embeddings.length} embeddings for 1 text.',
    );
  }
  return store.search(
    embeddings.first,
    topK: topK,
    minScore: minScore,
    where: where,
  );
}