search method

Future<RagSearchResult> search(
  1. String query, {
  2. int topK = 10,
  3. int tokenBudget = 2000,
  4. ContextStrategy strategy = ContextStrategy.relevanceFirst,
  5. int adjacentChunks = 0,
  6. bool singleSourceMode = false,
  7. List<int>? sourceIds,
  8. String? collectionId,
})

Search for relevant chunks and assemble context for LLM.

query - The search query text. topK - Number of top results to return (default: 10). tokenBudget - Maximum tokens for assembled context (default: 2000). strategy - Context assembly strategy (default: relevanceFirst). adjacentChunks - Include N chunks before/after matches (default: 0). singleSourceMode - Only include chunks from most relevant source. sourceIds - Only search these sources.

ChunkSearchResult.similarity is the cosine similarity of the chunk to the query, with or without sourceIds. Chunks added by adjacentChunks have similarity 0.

With sourceIds, every chunk of those sources is compared, so scoping to most of a large collection is slower than searching without sourceIds: 20.5 ms against 2.7 ms for 18,000 of 20,000 chunks (9.7 ms before this was cosine), median on an M-series Mac with fake embeddings, not measured on a device. Scoping to a few sources is fast.

Implementation

Future<RagSearchResult> search(
  String query, {
  int topK = 10,
  int tokenBudget = 2000,
  ContextStrategy strategy = ContextStrategy.relevanceFirst,
  int adjacentChunks = 0,
  bool singleSourceMode = false,
  List<int>? sourceIds,
  String? collectionId,
}) async {
  _ensureEmbeddingFingerprintUnlocked();
  final service = await _serviceForCollection(collectionId);
  await _flushIndex(
    collectionId: collectionId,
  ); // Ensure index is up-to-date before searching
  return service.search(
    query,
    topK: topK,
    tokenBudget: tokenBudget,
    strategy: strategy,
    adjacentChunks: adjacentChunks,
    singleSourceMode: singleSourceMode,
    sourceIds: sourceIds,
  );
}