UNPKG

dspy.ts

Version:

DSPy.ts - Declarative Self-Learning TypeScript: A framework for compositional LM pipelines with self-improving prompt strategies.

125 lines 4.71 kB
"use strict"; /** * Bootstrap Few-Shot optimizer implementation */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BootstrapFewShot = void 0; const module_1 = require("../core/module"); const base_1 = require("./base"); const index_1 = require("../index"); /** * Optimized module with few-shot demonstrations */ class OptimizedModule extends module_1.Module { constructor(name, signature, promptTemplate) { super({ name, signature, promptTemplate, strategy: 'Predict' }); } async run(input) { this.validateInput(input); const lm = (0, index_1.getLM)(); const prompt = this.promptTemplate(input); const response = await lm.generate(prompt); const output = JSON.parse(response); this.validateOutput(output); return output; } } /** * BootstrapFewShot optimizer that generates demonstrations using a teacher model */ class BootstrapFewShot extends base_1.Optimizer { constructor(metric, config = {}) { super(metric, config); this.optimizedProgram = null; this.config = Object.assign({ maxIterations: 10, numThreads: 1, debug: false, maxLabeledDemos: 4, maxBootstrappedDemos: 4, minScore: 0.7 }, config); } /** * Generate demonstrations using the teacher model */ async generateDemonstrations(program, trainset) { const demos = []; // First, add labeled examples from trainset const labeledDemos = trainset .filter(ex => ex.output !== undefined) .slice(0, this.config.maxLabeledDemos); demos.push(...labeledDemos); // Then, generate bootstrapped examples const unlabeledExamples = trainset .filter(ex => ex.output === undefined) .slice(0, this.config.maxBootstrappedDemos); for (const example of unlabeledExamples) { try { // Run the program to generate output const output = await program.run(example.input); // Evaluate the output const score = this.metric(example.input, output); if (score >= this.config.minScore) { demos.push({ input: example.input, output }); } } catch (err) { this.log(`Error generating demonstration: ${err}`); continue; } } return demos; } /** * Compile a program with bootstrap few-shot optimization */ async compile(program, trainset) { this.log('Starting bootstrap few-shot optimization'); // Generate demonstrations const demos = await this.generateDemonstrations(program, trainset); this.log(`Generated ${demos.length} demonstrations`); // Create optimized module by updating prompt template this.optimizedProgram = new OptimizedModule(program.name, program.signature, (input) => { const demoText = demos.map(demo => `Example: Input: ${JSON.stringify(demo.input)} Expected Output: ${JSON.stringify(demo.output)}`).join('\n\n'); return `${demoText}\n\nAnalyze the following input and respond in JSON format:\nInput: ${JSON.stringify(input)}\n\nResponse:`; }); return this.optimizedProgram; } /** * Save the optimized program to a file */ save(path, saveFieldMeta = false) { var _a; if (!this.optimizedProgram) { throw new Error('No optimized program to save. Run compile() first.'); } const data = { config: this.config, program: { name: this.optimizedProgram.name, signature: this.optimizedProgram.signature, promptTemplate: (_a = this.optimizedProgram.promptTemplate) === null || _a === void 0 ? void 0 : _a.toString(), fieldMeta: saveFieldMeta ? this.optimizedProgram.signature : undefined } }; // Write to file const fs = require('fs'); fs.writeFileSync(path, JSON.stringify(data, null, 2)); } /** * Load an optimized program from a file */ load(path) { const fs = require('fs'); const data = JSON.parse(fs.readFileSync(path, 'utf8')); // Recreate the program this.optimizedProgram = new OptimizedModule(data.program.name, data.program.signature, eval(`(${data.program.promptTemplate})`)); this.config = data.config; } } exports.BootstrapFewShot = BootstrapFewShot; //# sourceMappingURL=bootstrap.js.map