UNPKG

@lewist9x/distil

Version:

An opinionated library for managing LLM pipelines. Define, track, rate, and curate prompt–completion pairs for fine-tuning.

65 lines (64 loc) • 2.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeTemplateHash = exports.postprocess = exports.retry = exports.calculateCost = exports.validateInput = void 0; // src/utils.ts const crypto_1 = __importDefault(require("crypto")); const config_1 = require("./config"); /** * Validate required fields on LLMInput. */ function validateInput(input) { if (typeof input.modelName !== "string" || typeof input.systemPrompt !== "string" || typeof input.userPrompt !== "string") { throw new Error("Invalid input: modelName, systemPrompt, and userPrompt must be strings."); } return input; } exports.validateInput = validateInput; /** * Estimate cost using an average token length heuristic. */ function calculateCost(inputStr, outputStr) { const avgTokenLength = 4; const inputTokens = Math.ceil(inputStr.length / avgTokenLength); const outputTokens = Math.ceil(outputStr.length / avgTokenLength); return (inputTokens + outputTokens) * config_1.config.costPerToken; } exports.calculateCost = calculateCost; /** * Simple retry wrapper for async functions. */ async function retry(fn, retries = config_1.config.retry.retries, delay = config_1.config.retry.delay) { let error; for (let i = 0; i < retries; i++) { try { return await fn(); } catch (err) { error = err; await new Promise((resolve) => setTimeout(resolve, delay * (i + 1))); } } throw error; } exports.retry = retry; /** * Generic postprocessing function. Extend as needed. */ function postprocess(response, extraData) { return response; } exports.postprocess = postprocess; /** * Compute a unique SHA-256 hash based on the systemPrompt, userPrompt, and sorted parameter keys. */ function computeTemplateHash(input) { const hash = crypto_1.default.createHash("sha256"); hash.update(input.systemPrompt + input.userPrompt); return hash.digest("hex"); } exports.computeTemplateHash = computeTemplateHash;