Few-shot examples steer the output

Showing the model a few input and answer pairs teaches it the format and the labels, often better than describing them.

Time O(1)Space O(k)Code C#
Watch it animatedStep through it, with the running line highlighted. Free with an account.

The idea

Few-shot prompting means putting a handful of worked examples in the prompt: an input followed by the answer you want. The model continues the pattern, so it copies the format, the label names and the style. Without examples (zero-shot) it may answer correctly in a chatty, unpredictable shape that your code cannot rely on.

Code

public record Example(string Text, string Label);

public static class TicketLabeler
{
    public static async Task<string> ClassifyAsync(ILlmClient llm, string query)
    {
        var sb = new StringBuilder("Label each message with one word.\n\n");
        var examples = new[]
        {
            new Example("app crashes on login", "BUG"),
            new Example("please add dark mode", "FEATURE"),
            new Example("reset my password?", "QUESTION"),
        };
        foreach (var e in examples)
            sb.Append($"Message: {e.Text}\nLabel: {e.Label}\n\n");
        sb.Append($"Message: {query}\nLabel:");
        string label = await llm.CompleteAsync(sb.ToString());
        return label.Trim();
    }
}

Tips

In the .NET library

Keep the examples in data, not scattered string literals, for example a list loaded from a file or database. Then you can add a new tricky case when a user finds one, and re-run your evals.