Skip to content

Similarity Scoring

Compute cosine similarity between a source sentence and a list of candidate sentences.

This example assumes using HuggingFace; is in scope and apiKey contains your HuggingFace API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
using var client = new HuggingFaceEmbeddingClient(apiKey);

var scores = await client.SimilarityAsync(
    inputs: new SimilarityInput
    {
        SourceSentence = "What is Deep Learning?",
        Sentences =
        [
            "Deep Learning is a subset of Machine Learning.",
            "The weather is sunny today.",
            "Neural networks are inspired by the human brain.",
        ],
    });

for (var i = 0; i < scores.Count; i++)
{
    Console.WriteLine($"[{i}] similarity={scores[i]:F4}");
}