InstructionRewrite

Compiler that uses LLM to generate and test instruction variations.

Constructor

class InstructionRewrite<O> {
  constructor(evaluator: Evaluator<O>)
}

compile()

async compile<I, O>(
  program: Program<I, O>,
  trainset: Example<I, O>[],
  options: InstructionRewriteOptions
): Promise<CompilationResult<I, O>>

Options

OptionTypeRequiredDescription
providerLLMProviderYesLLM to generate instruction variations
instructionVariationsnumberNo (5)Number of instruction rewrites
candidatesnumberNo (5)Few-shot combinations per instruction
concurrencynumberNo (5)Parallel evaluations
examplesPerCandidatenumberNo (3)Examples per prompt
validationSplitnumberNo (0.3)Validation data fraction
seednumberNoRandom seed
budgetobjectNoToken budget settings
onProgressfunctionNoProgress callback
Provider Required
InstructionRewrite requires a provider to generate variations. This adds one extra API call at the start of compilation.

How It Works

InstructionRewrite:

  1. Takes your schema description as the base instruction
  2. Uses an LLM to generate N variations (different wording, tone, structure)
  3. Tests each variation with different few-shot example combinations
  4. Returns the best-performing instruction + examples

Example

import { 
  InstructionRewrite, 
  exactMatch, 
  createProvider 
} from "@mzhub/promptc";

// Provider for generating variations
const optimizerProvider = createProvider("openai", {
  apiKey: process.env.OPENAI_API_KEY,
  defaultModel: "gpt-4o-mini"
});

const compiler = new InstructionRewrite(exactMatch());

const result = await compiler.compile(program, trainset, {
  provider: optimizerProvider,
  instructionVariations: 5,  // Generate 5 instruction variations
  candidates: 5,             // 5 few-shot combos per variation = 25 total
  concurrency: 3,
  onProgress: ({ currentBestScore }) => {
    console.log("Best:", currentBestScore);
  }
});

// The optimized instruction
console.log("Instructions:", result.config.instructions);

// Might be different from your original schema.description!
// e.g., "You are an expert at extracting names. Given text, 
//        identify all proper names of people mentioned..."

estimateCost()

estimateCost(
  trainsetSize: number,
  options?: Partial<InstructionRewriteOptions>
): { estimatedCalls: number; estimatedTokens: number }
const estimate = compiler.estimateCost(trainset.length, {
  instructionVariations: 5,
  candidates: 5
});

// Note: This includes the instruction generation call
console.log("Estimated calls:", estimate.estimatedCalls);