UNPKG

@yogesh0333/yogiway-prompt

Version:

Free & Open Source Prompt Optimization Library - Save 30-50% on AI API costs. Multi-language, multi-platform support.

135 lines (130 loc) • 5.56 kB
#!/usr/bin/env node "use strict"; /** * YOGIWAY Prompt CLI Tool * Command-line interface for prompt optimization */ Object.defineProperty(exports, "__esModule", { value: true }); const optimizer_1 = require("../optimizer"); const tokenizer_1 = require("../tokenizer"); const cost_calculator_1 = require("../cost-calculator"); const fs_1 = require("fs"); const path_1 = require("path"); const args = process.argv.slice(2); function showHelp() { console.log(` YOGIWAY Prompt CLI - Free & Open Source Prompt Optimization Usage: yogiway-prompt optimize <input> [options] yogiway-prompt count <input> [options] yogiway-prompt cost <input> [options] Commands: optimize Optimize a prompt file or text count Count tokens in a prompt cost Calculate cost for a prompt Options: -i, --input <file> Input file path -o, --output <file> Output file path -p, --provider <name> LLM provider (openai, anthropic, google, etc.) -m, --model <name> Model name (gpt-4, claude-3-opus, etc.) -a, --aggressive Use aggressive optimization --target <number> Target reduction percentage (default: 30) -h, --help Show this help message Examples: yogiway-prompt optimize -i prompt.txt -o optimized.txt yogiway-prompt count -i prompt.txt --provider openai yogiway-prompt cost -i prompt.txt --provider openai --model gpt-4 `); } function getInput(args) { const inputIndex = args.indexOf('-i') !== -1 ? args.indexOf('-i') : args.indexOf('--input'); if (inputIndex !== -1 && args[inputIndex + 1]) { const filePath = (0, path_1.resolve)(args[inputIndex + 1]); return (0, fs_1.readFileSync)(filePath, 'utf-8'); } // Check if input is provided as argument const commandIndex = args.findIndex(arg => ['optimize', 'count', 'cost'].includes(arg)); if (commandIndex !== -1 && args[commandIndex + 1] && !args[commandIndex + 1].startsWith('-')) { return args[commandIndex + 1]; } // Read from stdin return (0, fs_1.readFileSync)(0, 'utf-8'); } function getProvider(args) { const providerIndex = args.indexOf('-p') !== -1 ? args.indexOf('-p') : args.indexOf('--provider'); return providerIndex !== -1 && args[providerIndex + 1] ? args[providerIndex + 1] : 'openai'; } function getModel(args) { const modelIndex = args.indexOf('-m') !== -1 ? args.indexOf('-m') : args.indexOf('--model'); return modelIndex !== -1 && args[modelIndex + 1] ? args[modelIndex + 1] : 'gpt-5'; } function getOutput(args) { const outputIndex = args.indexOf('-o') !== -1 ? args.indexOf('-o') : args.indexOf('--output'); return outputIndex !== -1 && args[outputIndex + 1] ? (0, path_1.resolve)(args[outputIndex + 1]) : null; } async function main() { if (args.length === 0 || args.includes('-h') || args.includes('--help')) { showHelp(); return; } const command = args[0]; try { switch (command) { case 'optimize': { const input = getInput(args); const aggressive = args.includes('-a') || args.includes('--aggressive'); const targetIndex = args.indexOf('--target'); const target = targetIndex !== -1 && args[targetIndex + 1] ? parseInt(args[targetIndex + 1]) : 30; const result = (0, optimizer_1.optimizePrompt)(input, { aggressive, targetReduction: target, }); const output = getOutput(args); if (output) { (0, fs_1.writeFileSync)(output, result.optimized, 'utf-8'); console.log(`āœ… Optimized prompt saved to: ${output}`); } else { console.log(result.optimized); } console.error(`\nšŸ“Š Statistics:`); console.error(` Original tokens: ${result.stats.originalTokens}`); console.error(` Optimized tokens: ${result.stats.optimizedTokens}`); console.error(` Reduction: ${result.reduction.percentage}%`); console.error(` Estimated savings: $${result.savings.estimated.toFixed(4)}`); break; } case 'count': { const input = getInput(args); const provider = getProvider(args); const tokens = (0, tokenizer_1.countTokens)(input, provider); console.log(`Tokens: ${tokens}`); break; } case 'cost': { const input = getInput(args); const provider = getProvider(args); const model = getModel(args); const cost = (0, cost_calculator_1.calculateCost)(input, provider, model); console.log(`Cost estimate:`); console.log(` Input tokens: ${cost.inputTokens}`); console.log(` Output tokens: ${cost.estimatedOutputTokens}`); console.log(` Input cost: $${cost.inputCost.toFixed(6)}`); console.log(` Output cost: $${cost.outputCost.toFixed(6)}`); console.log(` Total cost: $${cost.totalCost.toFixed(6)}`); break; } default: console.error(`Unknown command: ${command}`); showHelp(); process.exit(1); } } catch (error) { console.error(`Error: ${error.message}`); process.exit(1); } } main();