@yogesh0333/yogiway-prompt
Version:
Free & Open Source Prompt Optimization Library - Save 30-50% on AI API costs. Multi-language, multi-platform support.
184 lines (183 loc) • 6.46 kB
JavaScript
;
/**
* Prompt Optimizer - Core optimization engine
* Removes redundancy, compresses text, preserves important content
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.optimizePrompt = optimizePrompt;
const tokenizer_1 = require("./tokenizer");
/**
* Optimize prompt to reduce tokens while preserving meaning
*/
function optimizePrompt(prompt, options = {}) {
const { targetReduction = 30, removeRedundancy = true, compressWhitespace = true, removePunctuation = false, preserveCodeBlocks = true, preserveMarkdown = true, aggressive = false, } = options;
let optimized = prompt;
// Step 1: Preserve code blocks and markdown
const codeBlocks = [];
const markdownBlocks = [];
if (preserveCodeBlocks) {
// Extract code blocks
optimized = optimized.replace(/```[\s\S]*?```/g, (match) => {
codeBlocks.push(match);
return `__CODE_BLOCK_${codeBlocks.length - 1}__`;
});
}
if (preserveMarkdown) {
// Extract markdown headers, lists, etc.
optimized = optimized.replace(/^#{1,6}\s+.+$/gm, (match) => {
markdownBlocks.push(match);
return `__MD_${markdownBlocks.length - 1}__`;
});
}
// Step 2: Remove redundant words and phrases
if (removeRedundancy) {
optimized = removeRedundantWords(optimized, aggressive);
// Additional pass for common verbose patterns
optimized = removeVerbosePatterns(optimized, aggressive);
}
// Step 3: Compress whitespace
if (compressWhitespace) {
optimized = compressWhitespaceFunc(optimized);
}
// Step 4: Remove unnecessary punctuation (if aggressive)
if (removePunctuation && aggressive) {
optimized = removeUnnecessaryPunctuation(optimized);
}
// Step 5: Restore code blocks and markdown
if (preserveCodeBlocks) {
codeBlocks.forEach((block, i) => {
optimized = optimized.replace(`__CODE_BLOCK_${i}__`, block);
});
}
if (preserveMarkdown) {
markdownBlocks.forEach((block, i) => {
optimized = optimized.replace(`__MD_${i}__`, block);
});
}
// Step 6: Final cleanup
optimized = optimized.trim();
// Calculate statistics
const originalCount = (0, tokenizer_1.getTokenCount)(prompt);
const optimizedCount = (0, tokenizer_1.getTokenCount)(optimized);
const tokenReduction = originalCount.tokens - optimizedCount.tokens;
const reductionPercentage = (tokenReduction / originalCount.tokens) * 100;
const savings = originalCount.estimatedCost - optimizedCount.estimatedCost;
return {
original: prompt,
optimized,
reduction: {
tokens: tokenReduction,
percentage: Math.round(reductionPercentage * 100) / 100,
characters: prompt.length - optimized.length,
words: originalCount.words - optimizedCount.words,
},
savings: {
estimated: savings,
currency: "USD",
},
stats: {
originalTokens: originalCount.tokens,
optimizedTokens: optimizedCount.tokens,
originalChars: prompt.length,
optimizedChars: optimized.length,
},
};
}
/**
* Remove redundant words and phrases
*/
function removeRedundantWords(text, aggressive) {
// Common redundant phrases
const redundantPhrases = [
/\bplease\s+note\s+that\b/gi,
/\bit\s+is\s+important\s+to\s+note\s+that\b/gi,
/\bas\s+you\s+can\s+see\b/gi,
/\bin\s+order\s+to\b/gi,
/\bfor\s+the\s+purpose\s+of\b/gi,
/\bdue\s+to\s+the\s+fact\s+that\b/gi,
/\bthe\s+reason\s+why\b/gi,
/\bat\s+this\s+point\s+in\s+time\b/gi,
/\bvery\s+important\b/gi,
/\babsolutely\s+essential\b/gi,
];
let result = text;
redundantPhrases.forEach((pattern) => {
result = result.replace(pattern, "");
});
// Remove redundant adjectives (aggressive mode)
if (aggressive) {
result = result.replace(/\bvery\s+/gi, "");
result = result.replace(/\breally\s+/gi, "");
result = result.replace(/\bquite\s+/gi, "");
result = result.replace(/\babsolutely\s+/gi, "");
}
// Remove filler words
const fillerWords = /\b(um|uh|like|you know|I mean)\b/gi;
result = result.replace(fillerWords, "");
return result;
}
/**
* Compress whitespace
*/
function compressWhitespaceFunc(text) {
return text
.replace(/\s+/g, " ") // Multiple spaces to single
.replace(/\n\s*\n\s*\n/g, "\n\n") // Multiple newlines to double
.replace(/[ \t]+$/gm, "") // Trailing spaces
.replace(/^[ \t]+/gm, ""); // Leading spaces
}
/**
* Remove unnecessary punctuation (aggressive)
*/
function removeUnnecessaryPunctuation(text) {
return text
.replace(/[;]/g, ",") // Semicolons to commas
.replace(/[!]{2,}/g, "!") // Multiple exclamations to single
.replace(/[?]{2,}/g, "?") // Multiple questions to single
.replace(/[.]{3,}/g, "..."); // Multiple dots to ellipsis
}
/**
* Remove verbose patterns (improved)
*/
function removeVerbosePatterns(text, aggressive) {
// Remove common verbose patterns
const patterns = [
// Verbose connectors
/\bwith\s+regard\s+to\b/gi,
"about",
/\bwith\s+respect\s+to\b/gi,
"about",
/\bin\s+relation\s+to\b/gi,
"about",
/\bwith\s+reference\s+to\b/gi,
"about",
// Verbose time phrases
/\bat\s+the\s+present\s+time\b/gi,
"now",
/\bat\s+this\s+point\s+in\s+time\b/gi,
"now",
/\bat\s+the\s+current\s+time\b/gi,
"now",
// Verbose conditionals
/\bin\s+the\s+event\s+that\b/gi,
"if",
/\bin\s+case\s+that\b/gi,
"if",
// Verbose comparisons
/\bin\s+comparison\s+to\b/gi,
"vs",
/\bin\s+comparison\s+with\b/gi,
"vs",
];
let result = text;
for (let i = 0; i < patterns.length; i += 2) {
result = result.replace(patterns[i], patterns[i + 1]);
}
if (aggressive) {
// More aggressive patterns
result = result.replace(/\bI\s+would\s+like\s+to\b/gi, "I want to");
result = result.replace(/\bI\s+am\s+writing\s+to\b/gi, "");
result = result.replace(/\bI\s+hope\s+this\s+email\b/gi, "");
}
return result;
}