io / artifacts /api-server /src /lib /guide.ts
thagnitti's picture
cleanup: strip dead code (web-scraping, fine-tuning, scoring)
9b2f353 verified
/**
* guide.ts — best-of-N sample reranker.
*/
export interface Sample {
text: string;
score: number;
}
const STOP = new Set([
'the','a','an','is','are','was','were','of','in','to','and','or','it','its',
'this','that','with','for','on','as','by','be','at','from','but','not','if',
]);
export function rerankSamples(
samples: Sample[],
context: string,
query: string,
): { best: Sample | null; bestScore: number } {
if (!samples.length) return { best: null, bestScore: 0 };
const terms = query.toLowerCase().match(/[a-z]{3,}/g) ?? [];
const keywords = terms.filter(t => !STOP.has(t));
let best = samples[0];
let bestScore = -Infinity;
for (const s of samples) {
let score = s.score;
const lower = s.text.toLowerCase();
for (const kw of keywords) {
if (lower.includes(kw)) score += 0.3;
}
// Penalise very short outputs
const words = s.text.split(/\s+/).filter(Boolean).length;
if (words < 5) score -= 2;
if (score > bestScore) {
bestScore = score;
best = s;
}
}
return { best, bestScore };
}