FewShot
aisteer360.algorithms.input_control.few_shot
args
control
Few-shot learning control for prompt adaptation.
FewShot
Bases: InputControl
Implementation of few-shot learning control for prompt adaptation.
FewShot enables selective behavioral steering by prepending specific examples to user prompts, guiding model responses through demonstration.
The method operates in two modes:
-
Pool-based sampling: Maintains pools of positive and negative examples from which k examples are dynamically selected using configurable sampling strategies (random, semantic similarity, etc.).
-
Runtime injection: Accepts examples directly at inference time through runtime_kwargs, enabling context-specific demonstrations without predefined pools. Useful for dynamic or user-provided examples.
The selected examples are formatted into a system prompt with clear positive/negative labels and prepended to the user query using the model's chat template, allowing the model to learn the desired behavior pattern from the demonstrations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directive
|
str
|
Instruction text that precedes the examples, explaining the task or desired behavior. Defaults to None. |
required |
positive_example_pool
|
Sequence[dict]
|
Pool of positive examples demonstrating desired behavior. Each dict can contain multiple key-value pairs. Defaults to None. |
required |
negative_example_pool
|
Sequence[dict]
|
Pool of negative examples showing undesired behavior to avoid. Each dict can contain multiple key-value pairs. Defaults to None. |
required |
k_positive
|
int
|
Number of positive examples to sample from the pool per query. Defaults to None. |
required |
k_negative
|
int
|
Number of negative examples to sample from the pool per query. Defaults to None. |
required |
selector_name
|
str
|
Name of the selection strategy ('random', 'semantic', etc.). Determines how examples are chosen from pools. Defaults to 'random'. |
required |
template
|
str
|
Custom template for formatting the system prompt. Should contain {directive} and {example_blocks} placeholders. Defaults to built-in template. |
required |
Runtime keyword arguments:
positive_examples
(list[dict]
,optional
): Positive examples to use for this specific query (overrides pool-based selection).negative_examples
(list[dict]
,optional
): Negative examples to use for this specific query (overrides pool-based selection).
Notes:
- Requires a tokenizer with chat_template support for optimal formatting
- Examples are automatically labeled as "### Positive example" or "### Negative example"
- When both pools and runtime examples are available, runtime examples take precedence
- If no examples are provided, the original input is returned unchanged
Source code in aisteer360/algorithms/input_control/few_shot/control.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
args = self.Args.validate(*args, **kwargs)
instance-attribute
directive = None
class-attribute
instance-attribute
enabled = True
class-attribute
instance-attribute
k_negative = None
class-attribute
instance-attribute
k_positive = None
class-attribute
instance-attribute
negative_example_pool = None
class-attribute
instance-attribute
positive_example_pool = None
class-attribute
instance-attribute
selector = None
class-attribute
instance-attribute
selector_name = None
class-attribute
instance-attribute
tokenizer = None
class-attribute
instance-attribute
get_prompt_adapter()
Return a prompt adapter function that adds few-shot examples to the model's system prompt. Creates and returns a closure that modifies input token sequences by prepending few-shot examples.
The returned adapter function performs the following steps:
- Determines operational mode (runtime examples take precedence over pools)
- Decodes input tokens to retrieve the original user message
- Selects or retrieves appropriate examples based on mode
- Formats examples with positive/negative labels
- Constructs a system prompt containing the examples
- Applies the model's chat template (if available) to combine system prompt and user message
- Re-encodes the adapted text to tokens
Returns:
Type | Description |
---|---|
Callable[[list[int] | Tensor, dict[str, Any]], list[int] | Tensor]
|
A prompt adapter function. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If tokenizer is not set (requires calling |
Warns:
Type | Description |
---|---|
UserWarning
|
Issued when:
|
Source code in aisteer360/algorithms/input_control/few_shot/control.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
steer(model=None, tokenizer=None, **kwargs)
Optional steering/preparation.
Source code in aisteer360/algorithms/input_control/few_shot/control.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
selectors
Example selectors for few-shot learning prompt adaptation.
This module provides different strategies for selecting examples from pools during few-shot prompting. Selectors determine which examples are passed as demonstrations to the model.
Available selectors:
RandomSelector
: Randomly samples examples from the pool
base
Base interface for few-shot example selection strategies.
Selector
Bases: ABC
Base class for example selector.
Source code in aisteer360/algorithms/input_control/few_shot/selectors/base.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
sample(pool, k, **kwargs)
abstractmethod
Return k items chosen from pool.
Source code in aisteer360/algorithms/input_control/few_shot/selectors/base.py
13 14 15 16 17 18 19 20 21 |
|
random_selector
RandomSelector
Bases: Selector
Selects examples uniformly at random from a pool for few-shot prompting.
Source code in aisteer360/algorithms/input_control/few_shot/selectors/random_selector.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
sample(pool, k, **_)
Select k examples uniformly at random from the pool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pool
|
Sequence[dict]
|
Available examples to select from |
required |
k
|
int
|
Number of examples to select |
required |
**_
|
Ignored (for compatibility with other selectors) |
{}
|
Returns:
Type | Description |
---|---|
list[dict]
|
List of randomly selected examples (up to min(k, len(pool))) |
Source code in aisteer360/algorithms/input_control/few_shot/selectors/random_selector.py
10 11 12 13 14 15 16 17 18 19 20 21 |
|