strategy
Use sample to collect parallel attempts and consensus to choose the most common result. retry accepts the first result that passes a test. revise gives each new attempt the prior attempt's critique.
import { retry } from "std::strategy"
node main() {
const answer = retry(3, test: \text -> text.length > 0) as {
return llm("Suggest a short title for a gardening guide")
}
print(answer)
}Function budget decorators live in std::decorators.
Types
Critique
What a revise check decides about one attempt: whether to accept it, and if not, the critique the next attempt should address. feedback is ignored when accepted is true.
/** What a `revise` check decides about one attempt: whether to accept it,
and if not, the critique the next attempt should address. `feedback` is
ignored when `accepted` is true. */
export type Critique = {
accepted: boolean;
feedback: string
}(source)
Functions
sample
sample(n: number, block: () -> any): any[]Run a block several times in parallel and collect the results in attempt order.
Parameters:
| Name | Type | Default |
|---|---|---|
| n | number | |
| block | () => any |
Returns: any[]
(source)
consensus
consensus(n: number, block: () -> any): anyRun a block several times in parallel and return its most common result.
Parameters:
| Name | Type | Default |
|---|---|---|
| n | number | |
| block | () => any |
Returns: any
(source)
retry
retry(n: number, test: (any) -> boolean, block: () -> any): anyRetry a block until its result passes a test. Return the first passing result, or null when no attempt passes.
Parameters:
| Name | Type | Default |
|---|---|---|
| n | number | |
| test | (any) => boolean | |
| block | () => any |
Returns: any
(source)
retryWithFeedback
retryWithFeedback(
n: number,
test: (any) -> boolean,
block: (any, number) -> any,
): anyRetry a block using the prior result to improve each attempt. Return the first passing result, or the last result if no attempt passes.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| n | number | ||
| test | (any) => boolean | ||
| block | (any, number) => any | Receives null on the first attempt, then the previous result, followed by a one-based attempt number. |
Returns: any
(source)
revise
revise(
maxAttempts: number,
check: (any) -> Critique,
block: (string) -> any,
): anyRevise work using critiques until a check accepts it or attempts run out. Return the accepted result, or the last result when all attempts are rejected.
The first attempt receives an empty critique. Later attempts receive the feedback from the last rejected result. When a budget guard stops the work, the last completed attempt can reach the guard as a partial result. If no attempt completed, an attempt's saved draft can be used instead.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| maxAttempts | number | ||
| check | (any) => Critique | ||
| block | (string) => any | Receives an empty string first, then the prior critique's feedback. |
Returns: any
(source)
firstValid
firstValid(variants: any[], test: (any) -> boolean, block: (any) -> any): anyRun a block for each variant in parallel and return the first passing result in input order, or null when none passes.
Parameters:
| Name | Type | Default |
|---|---|---|
| variants | any[] | |
| test | (any) => boolean | |
| block | (any) => any |
Returns: any
(source)