FewShotPromptTemplate<T>
Models & Types · AiDotNet.PromptEngineering.Templates
Prompt template that includes few-shot examples to guide model behavior.
Inheritance: PromptTemplateBase → FewShotPromptTemplate<T>
For Beginners
A template that shows examples to teach the model what you want.
Example:
// Create base template
var baseTemplate = "Classify the sentiment of the following text.\n\n{examples}\n\nText: {text}\nSentiment:";
// Labelled examples the template can draw from
var exampleSelector = new FixedExampleSelector<double>();
exampleSelector.AddExample(new FewShotExample { Input = "I loved this product!", Output = "Positive" });
exampleSelector.AddExample(new FewShotExample { Input = "Terrible experience.", Output = "Negative" });
exampleSelector.AddExample(new FewShotExample { Input = "It's okay, nothing special.", Output = "Neutral" });
// Create few-shot template with example selector
var template = new FewShotPromptTemplate<double>(
baseTemplate,
exampleSelector,
exampleCount: 3,
exampleFormat: "Text: {input}\nSentiment: {output}"
);
var prompt = template.Format(new Dictionary<string, string>
{
["text"] = "This movie was amazing!"
});
// Result includes 3 examples + the query:
// "Classify the sentiment of the following text.
//
// Text: I loved this product!
// Sentiment: Positive
//
// Text: Terrible experience.
// Sentiment: Negative
//
// Text: It's okay, nothing special.
// Sentiment: Neutral
//
// Text: This movie was amazing!
// Sentiment:"
How It Works
This template combines a base template with examples selected from a few-shot example selector. The examples are formatted and included in the prompt to demonstrate the desired input-output pattern.
Constructors
| Constructor | Summary |
|---|---|
FewShotPromptTemplate(string template, IFewShotExampleSelector<T> exampleSelector, int exampleCount, string exampleFormat, string exampleSeparator) |
Initializes a new instance of the FewShotPromptTemplate class. |
Methods
| Method | Summary |
|---|---|
bool Validate(Dictionary<string, string> variables) |
Validates that the provided variables match the template’s requirements. |