bugfixing #3
This commit is contained in:
parent
1c4dcdd108
commit
e89e3b7a70
6 changed files with 137 additions and 30 deletions
|
|
@ -9,8 +9,9 @@ interface LlmResponse {
|
|||
promptTokens: number;
|
||||
completionTokens: number;
|
||||
totalTokens: number;
|
||||
promptTimeMs: number;
|
||||
completionTimeMs: number;
|
||||
promptTimeMs: number | null;
|
||||
completionTimeMs: number | null;
|
||||
totalTimeMs: number;
|
||||
}
|
||||
|
||||
export interface EnrichmentResult {
|
||||
|
|
@ -19,8 +20,9 @@ export interface EnrichmentResult {
|
|||
promptTokens: number;
|
||||
completionTokens: number;
|
||||
totalTokens: number;
|
||||
promptTimeMs: number;
|
||||
completionTimeMs: number;
|
||||
promptTimeMs: number | null;
|
||||
completionTimeMs: number | null;
|
||||
totalTimeMs: number;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -54,6 +56,60 @@ function sanitizeLlmOutput(raw: string): string {
|
|||
return cleaned.slice(start, end + 1);
|
||||
}
|
||||
|
||||
function validateSense(item: unknown, word: string, index: number): void {
|
||||
if (typeof item !== "object" || item === null || Array.isArray(item)) {
|
||||
throw new Error(`Sense ${index} for "${word}" is not an object`);
|
||||
}
|
||||
|
||||
const sense = item as Record<string, unknown>;
|
||||
|
||||
if (typeof sense["sense"] !== "string" || !sense["sense"]) {
|
||||
throw new Error(`Sense ${index} for "${word}": missing or invalid "sense"`);
|
||||
}
|
||||
if (typeof sense["example"] !== "string" || !sense["example"]) {
|
||||
throw new Error(
|
||||
`Sense ${index} for "${word}": missing or invalid "example"`,
|
||||
);
|
||||
}
|
||||
if (
|
||||
!["easy", "medium", "hard"].includes(sense["difficulty_level"] as string)
|
||||
) {
|
||||
throw new Error(`Sense ${index} for "${word}": invalid "difficulty_level"`);
|
||||
}
|
||||
if (
|
||||
typeof sense["translations"] !== "object" ||
|
||||
sense["translations"] === null
|
||||
) {
|
||||
throw new Error(`Sense ${index} for "${word}": missing "translations"`);
|
||||
}
|
||||
|
||||
const trans = sense["translations"] as Record<string, unknown>;
|
||||
for (const lang of ["de", "it", "es", "fr"]) {
|
||||
if (!Array.isArray(trans[lang])) {
|
||||
throw new Error(
|
||||
`Sense ${index} for "${word}": missing or invalid "${lang}" translations`,
|
||||
);
|
||||
}
|
||||
for (let j = 0; j < (trans[lang] as unknown[]).length; j++) {
|
||||
const t = (trans[lang] as unknown[])[j] as Record<string, unknown>;
|
||||
if (typeof t["word"] !== "string" || !t["word"]) {
|
||||
throw new Error(
|
||||
`Sense ${index} for "${word}": ${lang}[${j}] missing "word"`,
|
||||
);
|
||||
}
|
||||
if (
|
||||
!["masculine", "feminine", "neuter", null].includes(
|
||||
t["gender"] as string | null,
|
||||
)
|
||||
) {
|
||||
throw new Error(
|
||||
`Sense ${index} for "${word}": ${lang}[${j}] invalid "gender"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the LLM response string into a JavaScript object.
|
||||
* Throws if the response is not valid JSON or not an object with expected keys.
|
||||
|
|
@ -86,6 +142,12 @@ export function parseLlmResponse(
|
|||
if (!Array.isArray(obj[word]) || (obj[word] as unknown[]).length === 0) {
|
||||
throw new Error(`LLM output for "${word}" is not a non-empty array`);
|
||||
}
|
||||
|
||||
// Validate each sense in the array
|
||||
const senses = obj[word] as unknown[];
|
||||
for (let i = 0; i < senses.length; i++) {
|
||||
validateSense(senses[i], word, i);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
|
|
@ -145,6 +207,7 @@ export async function enrichWord(
|
|||
totalTokens: llmResponse.totalTokens,
|
||||
promptTimeMs: llmResponse.promptTimeMs,
|
||||
completionTimeMs: llmResponse.completionTimeMs,
|
||||
totalTimeMs: llmResponse.totalTimeMs,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -207,10 +270,13 @@ export async function enrichWordWithRetry(
|
|||
totalTokens:
|
||||
leftResult.metrics.totalTokens + rightResult.metrics.totalTokens,
|
||||
promptTimeMs:
|
||||
leftResult.metrics.promptTimeMs + rightResult.metrics.promptTimeMs,
|
||||
(leftResult.metrics.promptTimeMs ?? 0) +
|
||||
(rightResult.metrics.promptTimeMs ?? 0),
|
||||
completionTimeMs:
|
||||
leftResult.metrics.completionTimeMs +
|
||||
rightResult.metrics.completionTimeMs,
|
||||
(leftResult.metrics.completionTimeMs ?? 0) +
|
||||
(rightResult.metrics.completionTimeMs ?? 0),
|
||||
totalTimeMs:
|
||||
leftResult.metrics.totalTimeMs + rightResult.metrics.totalTimeMs,
|
||||
};
|
||||
|
||||
return { results: merged, metrics: mergedMetrics };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue