strategy
Run a block several times and combine the results. sample collects every result, consensus takes the majority vote, and retry / retryWithFeedback re-run a block until one result passes a test.
import { consensus } from "std::strategy"
node main() {
// Run the same prompt 5 times and keep the majority answer.
let answer = consensus(5) as {
return llm("Is 17 prime? Answer yes or no.")
}
print(answer)
}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 n times in parallel. Returns an array of all results.
@param n - Number of times to run @param block - The block to execute
Parameters:
| Name | Type | Default |
|---|---|---|
| n | number | |
| block | () => any |
Returns: any[]
(source)
consensus
consensus(n: number, block: () -> any): anyRun a block n times in parallel and return the most common result (majority vote).
@param n - Number of times to run @param block - The block to execute
Parameters:
| Name | Type | Default |
|---|---|---|
| n | number | |
| block | () => any |
Returns: any
(source)
retry
retry(n: number, test: (any) -> boolean, block: () -> any): anyRun a block up to n times. Returns the first result that passes the test function. Returns null if all attempts fail.
@param n - Maximum number of attempts @param test - The function that returns true when the result is acceptable @param block - The block to execute
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,
): anyRun a block up to n times. Each attempt receives the previous result and the attempt number (starting from 1). Returns the first result that passes the test, or the last result if all fail.
@param n - Maximum number of attempts @param test - The function that returns true when the result is acceptable @param block - The block receiving (previousResult, attemptNumber)
Parameters:
| Name | Type | Default |
|---|---|---|
| n | number | |
| test | (any) => boolean | |
| block | (any, number) => any |
Returns: any
(source)
revise
revise(
maxAttempts: number,
check: (any) -> Critique,
block: (string) -> any,
): anyRun a block until its result is accepted, up to maxAttempts times. Each rejected attempt passes its critique to the next one, which receives it as its argument (an empty string on the first attempt). Returns the first accepted result, or the last result when every attempt is rejected.
@param maxAttempts - Maximum number of attempts @param check - Judges a result and returns the critique to address next @param block - The work, receiving the previous critique
Parameters:
| Name | Type | Default |
|---|---|---|
| maxAttempts | number | |
| check | (any) => Critique | |
| block | (string) => any |
Returns: any
(source)
firstValid
firstValid(variants: any[], test: (any) -> boolean, block: (any) -> any): anyRun a block for each variant in parallel, then return the first result that passes the test. Returns null if none pass.
@param variants - Array of variants to try @param test - The function that returns true for valid results @param block - The block receiving each variant
Parameters:
| Name | Type | Default |
|---|---|---|
| variants | any[] | |
| test | (any) => boolean | |
| block | (any) => any |
Returns: any
(source)