Table of Contents

Interface IFewShotExampleSelector<T>

Namespace
AiDotNet.Interfaces
Assembly
AiDotNet.dll

Defines the contract for selecting few-shot examples to include in prompts.

public interface IFewShotExampleSelector<T>

Type Parameters

T

The type of numeric data used for similarity scoring.

Remarks

A few-shot example selector chooses which examples to include in a prompt to guide the language model's behavior. Different selection strategies (random, semantic similarity, diversity, MMR) optimize for different goals.

For Beginners: An example selector picks which examples to show the language model.

Think of it like a teacher choosing practice problems:

  • You have 100 practice problems in the textbook
  • You can only show 3-5 examples in class
  • Which ones do you choose?

Different strategies:

  • Random: Pick any 3 problems
  • Similar to homework: Pick problems like tonight's homework
  • Diverse: Pick problems covering different concepts
  • Best of both: Pick relevant problems that are also diverse

The examples you choose significantly affect how well students (or the LLM) learn!

Example - Sentiment classification: Available examples: 1,000 labeled movie reviews Current query: "This movie was fantastic, loved every minute!"

Selector's job:

  1. Look at the query
  2. Choose 3-5 most helpful examples
  3. Return them to include in the prompt

Good examples → Better LLM performance

Properties

ExampleCount

int ExampleCount { get; }

Property Value

int

Methods

AddExample(FewShotExample)

Adds an example to the selector's pool of available examples.

void AddExample(FewShotExample example)

Parameters

example FewShotExample

The example to add.

Remarks

Adds a new example to the pool that the selector can choose from. Examples typically consist of an input and corresponding output.

For Beginners: This adds a new example to the pool of available examples.

Example - Building a translation example pool:

var selector = new SemanticSimilaritySelector();

// Add examples
selector.AddExample(new FewShotExample
{
    Input = "Hello",
    Output = "Hola"
});

selector.AddExample(new FewShotExample
{
    Input = "Goodbye",
    Output = "Adiós"
});

// Now selector can choose from these examples

Over time, you build up a comprehensive example library.

GetAllExamples()

Gets all examples currently in the selector's pool.

IReadOnlyList<FewShotExample> GetAllExamples()

Returns

IReadOnlyList<FewShotExample>

Remarks

Returns all examples that the selector can choose from. Useful for inspection, debugging, and understanding the selector's behavior.

For Beginners: This shows all available examples in the pool.

Use this to:

  • See what examples are available
  • Debug selection issues
  • Export examples for documentation
  • Analyze example coverage

Example:

var allExamples = selector.GetAllExamples();
Console.WriteLine($"Pool has {allExamples.Count} examples");

foreach (var ex in allExamples)
{
    Console.WriteLine($"Input: {ex.Input} → Output: {ex.Output}");
}

RemoveExample(FewShotExample)

Removes an example from the selector's pool.

bool RemoveExample(FewShotExample example)

Parameters

example FewShotExample

The example to remove.

Returns

bool

True if the example was removed; false if it wasn't found.

Remarks

Removes an example from the pool. Useful for removing outdated or incorrect examples.

For Beginners: This removes an example from the pool.

Use this when:

  • An example is incorrect
  • An example is outdated
  • You're refining your example set
  • An example causes problems

Example:

// Remove a bad example
var badExample = new FewShotExample
{
    Input = "Test",
    Output = "Wrong answer"
};

bool removed = selector.RemoveExample(badExample);
// removed = true if it was in the pool, false otherwise

SelectExamples(string, int)

Selects the most appropriate examples for the given query.

IReadOnlyList<FewShotExample> SelectExamples(string query, int count)

Parameters

query string

The input query to select examples for.

count int

The number of examples to select.

Returns

IReadOnlyList<FewShotExample>

A list of selected examples.

Remarks

Chooses 'count' examples from the available pool that are most appropriate for the given query. The selection strategy depends on the implementation (random, semantic similarity, diversity, etc.).

For Beginners: This picks the best examples to show for a specific query.

Example - Code generation: Query: "Write a function to sort a list of numbers"

Available examples:

  1. "Sort a list of strings" → def sort_strings(lst): return sorted(lst)
  2. "Reverse a list" → def reverse(lst): return lst[::-1]
  3. "Filter even numbers" → def evens(lst): return [x for x in lst if x % 2 == 0]
  4. "Find max in list" → def find_max(lst): return max(lst)
  5. "Sort numbers descending" → def sort_desc(lst): return sorted(lst, reverse=True)

SelectExamples(query, count=2) might return:

  • Example 1: "Sort a list of strings" (very similar operation)
  • Example 5: "Sort numbers descending" (exact same task, different order)

These are most relevant for teaching the model how to sort numbers.

The selection adapts to each query:

  • Different query → Different examples selected
  • More relevant examples → Better results