UNPKG

dspy.ts

Version:

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

95 lines 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Module = void 0; /** * Base class for DSPy.ts modules. * Each module must define a signature and implement the run method. */ class Module { constructor(options) { this.name = options.name; this.signature = options.signature; this.promptTemplate = options.promptTemplate; this.strategy = options.strategy; } /** * Validates that the input matches the module's input signature */ validateInput(input) { for (const field of this.signature.inputs) { const value = input[field.name]; // Check required fields if (field.required && value === undefined) { throw new Error(`Missing required input field: ${field.name}`); } // Skip validation for optional undefined fields if (value === undefined) { continue; } // Validate type switch (field.type) { case 'string': if (typeof value !== 'string') { throw new Error(`Invalid input: ${field.name} must be of type string`); } break; case 'number': if (typeof value !== 'number') { throw new Error(`Invalid input: ${field.name} must be of type number`); } break; case 'boolean': if (typeof value !== 'boolean') { throw new Error(`Invalid input: ${field.name} must be of type boolean`); } break; case 'object': if (typeof value !== 'object' || value === null) { throw new Error(`Invalid input: ${field.name} must be of type object`); } break; } } } /** * Validates that the output matches the module's output signature */ validateOutput(output) { for (const field of this.signature.outputs) { const value = output[field.name]; // Check required fields if (field.required && value === undefined) { throw new Error(`Missing required output field: ${field.name}`); } // Skip validation for optional undefined fields if (value === undefined) { continue; } // Validate type switch (field.type) { case 'string': if (typeof value !== 'string') { throw new Error(`Invalid output: ${field.name} must be of type string`); } break; case 'number': if (typeof value !== 'number') { throw new Error(`Invalid output: ${field.name} must be of type number`); } break; case 'boolean': if (typeof value !== 'boolean') { throw new Error(`Invalid output: ${field.name} must be of type boolean`); } break; case 'object': if (typeof value !== 'object' || value === null) { throw new Error(`Invalid output: ${field.name} must be of type object`); } break; } } } } exports.Module = Module; //# sourceMappingURL=module.js.map