Table of Contents

Class VectorSearch

Namespace
LMKit.Retrieval
Assembly
LM-Kit.NET.dll

Provides methods for searching partitions across one or more data sources by comparing vector embeddings for similarity.

public static class VectorSearch
Inheritance
VectorSearch
Inherited Members

Examples

Example: Search with pre-computed embeddings

using LMKit.Model;
using LMKit.Retrieval;
using LMKit.Embeddings;
using System;

// Load embedding model and create embedder
LM embeddingModel = LM.LoadFromModelID("embeddinggemma-300m");
Embedder embedder = new Embedder(embeddingModel);

// Create and populate a data source (typically done via RagEngine)
RagEngine rag = new RagEngine(embeddingModel);
rag.ImportText("Machine learning enables computers to learn from data.", "docs", "ml");
rag.ImportText("Neural networks are computational models inspired by the brain.", "docs", "nn");

// Get the data source
var dataSources = rag.DataSources;

// Generate query embedding manually
float[] queryVector = embedder.GetEmbeddings("How do computers learn?");

// Search using VectorSearch directly
var results = VectorSearch.FindMatchingPartitions(
    dataSources,
    queryVector,
    topK: 3,
    minScore: 0.3f);

foreach (var result in results)
{
    Console.WriteLine($"[{result.Score:F3}] {result.Payload}");
}

Remarks

The VectorSearch class provides low-level vector similarity search functionality that can be used independently of RagEngine. It performs cosine similarity comparisons between a query vector and stored partition embeddings.

Key Features

  • Search single or multiple data sources
  • Configurable top-K and minimum score thresholds
  • Optional filtering by data source or section
  • Support for forcing unique data source or section results
  • Sync and async methods available

Usage Note
For most use cases, consider using FindMatchingPartitions(string, int, float, bool, bool, CancellationToken) which handles embedding generation automatically. Use VectorSearch when you need direct control over the embedding vectors or are integrating with custom embedding pipelines.

Methods

FindMatchingPartitions(DataSource, float[], int, float, bool, bool, DataFilter, CancellationToken)

Synchronously finds the top matching partitions in the given data source based on cosine similarity to the provided query vector.

FindMatchingPartitions(IEnumerable<DataSource>, float[], int, float, bool, bool, DataFilter, CancellationToken)

Synchronously finds the top matching partitions from the given data sources based on cosine similarity to the provided query vector.

FindMatchingPartitionsAsync(DataSource, float[], int, float, bool, bool, DataFilter, CancellationToken)

Asynchronously finds the top matching partitions in the given data source based on cosine similarity to the provided query vector.

FindMatchingPartitionsAsync(IEnumerable<DataSource>, float[], int, float, bool, bool, DataFilter, CancellationToken)

Asynchronously finds the top matching partitions from the given data sources based on cosine similarity to the provided query vector.