/** * Self-consistency orchestrator (Wang et al. 2022, "Self-Consistency Improves * Chain of Thought Reasoning in Language Models"). * * Runs an arbitrary stochastic operation N times and aggregates the results. * Reduces variance from single-shot stochastic predictions; on reasoning * benchmarks the technique typically gains 5–15pp accuracy at the cost of * Nx compute. The package's RL algorithms, pattern matching, and routing * all benefit since they sample randomly and can produce different outputs * across calls. * * Aggregators: * - 'majority': pick the most common output (for discrete answers / labels). * Uses JSON.stringify for grouping; stable for primitive and * plain-object outputs. Float32Array values aggregate via * their array-form encoding. * - 'mean': numeric average (operation must return number). * Agreement = 1 - normalized stddev. * - 'first': take the first sample (no aggregation; useful for testing * or deterministic ops). * * Pair with `setGlobalRng(new Mulberry32(seed))` to make a self-consistency * run reproducible across machines. */ import type { RNG } from './rng.js'; import { random } from './rng.js'; export type SelfConsistencyAggregator = 'majority' | 'mean' | 'first'; export interface SelfConsistencyConfig { /** Number of samples to draw. Required. Larger N = more stable, more compute. */ N: number; /** How to aggregate samples into a single answer. Default: 'majority'. */ aggregator?: SelfConsistencyAggregator; /** * Optional RNG to advance per sample (e.g. to differentiate seeds across * samples when the operation reads from the global RNG). The orchestrator * itself doesn't directly call this RNG; it's provided so callers can * thread per-sample state (e.g. by reseeding before each call). */ rng?: RNG; } export interface SelfConsistencyResult { /** The aggregated answer (majority vote, mean, or first). */ finalAnswer: T; /** Every sample produced, in the order produced. Length === config.N. */ samples: T[]; /** * For 'majority': fraction of samples matching finalAnswer (0..1). * For 'mean': 1 - normalized stddev (rough confidence proxy). * For 'first': always 1. */ agreement: number; /** The config used for this run. */ config: SelfConsistencyConfig; } /** * Run an operation N times and aggregate. The operation is awaited * sequentially — if the caller wants concurrency they can wrap it themselves. */ export declare function selfConsistency(operation: () => Promise | T, config: SelfConsistencyConfig): Promise>; export { random }; //# sourceMappingURL=self-consistency.d.ts.map