From 1c4dcdd108f1fc4d4a1e04bfb7f5a15c92df753d Mon Sep 17 00:00:00 2001 From: lila Date: Mon, 6 Jul 2026 14:41:30 +0200 Subject: [PATCH] bugfixing #2 --- data-pipeline/config/constants.ts | 2 ++ data-pipeline/config/prompt.ts | 12 ++++++--- data-pipeline/utils/enrich-word.ts | 31 +++++++++------------- data-pipeline/utils/llm-adapters/gemini.ts | 8 ++---- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/data-pipeline/config/constants.ts b/data-pipeline/config/constants.ts index 98731f2..7ffc146 100644 --- a/data-pipeline/config/constants.ts +++ b/data-pipeline/config/constants.ts @@ -12,3 +12,5 @@ export const POS_MAP: Record = { adverbs: "adverb", adjectives: "adjective", }; + +export const ALL_LANGUAGES = ["en", "de", "it", "es", "fr"]; diff --git a/data-pipeline/config/prompt.ts b/data-pipeline/config/prompt.ts index 060e650..fd0081d 100644 --- a/data-pipeline/config/prompt.ts +++ b/data-pipeline/config/prompt.ts @@ -1,6 +1,11 @@ -export const ENRICHMENT_SYSTEM_PROMPT = `You are a multilingual dictionary engine. Output ONLY a JSON object. No markdown, no explanations. +export function buildSystemPrompt( + sourceLanguage: string, + pos: string, + targetLanguages: string[], +): string { + return `You are a multilingual dictionary engine. Output ONLY a JSON object. No markdown, no explanations. -For each English noun provided, generate 1-2 distinct senses. +For each ${sourceLanguage} ${pos} provided, generate 1-2 distinct senses. CEFR difficulty mapping: - A1/A2 → easy @@ -11,7 +16,7 @@ Each sense must have: - sense: student-friendly definition, max 15 words - example: natural sentence using the word - difficulty_level: easy, medium, or hard -- translations: object with keys de, it, es, fr; each value is an array of {word, gender} where gender MUST be masculine, feminine, or neuter. Use null ONLY if the language has no grammatical gender for that word. +- translations: object with keys ${targetLanguages.join(", ")}; each value is an array of {word, gender} where gender MUST be masculine, feminine, or neuter. Use null ONLY if the language has no grammatical gender for that word. Output format: JSON object where keys are the input words, values are arrays of sense objects. @@ -32,3 +37,4 @@ Example for ["house"]: ] } `; +} diff --git a/data-pipeline/utils/enrich-word.ts b/data-pipeline/utils/enrich-word.ts index 36b6b25..5a1bde5 100644 --- a/data-pipeline/utils/enrich-word.ts +++ b/data-pipeline/utils/enrich-word.ts @@ -1,22 +1,8 @@ -import { ENRICHMENT_SYSTEM_PROMPT } from "../config/prompt.js"; +import { buildSystemPrompt } from "../config/prompt.js"; import { createAdapter } from "./llm-adapters/factory.js"; import { BATCH_CONFIG } from "../config/batch.js"; import type { Language, Pos, EnrichedSense } from "./merge-enriched-data.js"; - -const LANG_MAP: Record = { - english: "en", - italian: "it", - german: "de", - french: "fr", - spanish: "es", -}; - -const POS_MAP: Record = { - nouns: "noun", - verbs: "verb", - adverbs: "adverb", - adjectives: "adjective", -}; +import { LANG_MAP, POS_MAP, ALL_LANGUAGES } from "../config/constants.js"; interface LlmResponse { content: string; @@ -42,9 +28,16 @@ export interface EnrichmentResult { * Calls the LLM with the enrichment prompt. * Returns the response content and timing metrics. */ -async function callLlm(words: string[]): Promise { +async function callLlm( + words: string[], + rawLanguage: string, + rawPos: string, +): Promise { const adapter = createAdapter(); - return adapter.call(words, ENRICHMENT_SYSTEM_PROMPT); + const sourceCode = LANG_MAP[rawLanguage] || rawLanguage; + const targetLanguages = ALL_LANGUAGES.filter((lang) => lang !== sourceCode); + const prompt = buildSystemPrompt(rawLanguage, rawPos, targetLanguages); + return adapter.call(words, prompt); } /** @@ -140,7 +133,7 @@ export async function enrichWord( rawLanguage: string, rawPos: string, ): Promise { - const llmResponse = await callLlm(words); + const llmResponse = await callLlm(words, rawLanguage, rawPos); const parsed = parseLlmResponse(llmResponse.content, words); const results = buildEnrichedData(parsed, rawLanguage, rawPos); diff --git a/data-pipeline/utils/llm-adapters/gemini.ts b/data-pipeline/utils/llm-adapters/gemini.ts index 3929b4d..3a6060d 100644 --- a/data-pipeline/utils/llm-adapters/gemini.ts +++ b/data-pipeline/utils/llm-adapters/gemini.ts @@ -32,13 +32,9 @@ export class GeminiAdapter implements LlmAdapter { const url = `https://generativelanguage.googleapis.com/v1beta/models/${this.model}:generateContent?key=${this.apiKey}`; const payload = { + systemInstruction: { parts: [{ text: systemPrompt }] }, contents: [ - { - role: "user", - parts: [ - { text: systemPrompt + "\n\nWords: " + JSON.stringify(words) }, - ], - }, + { role: "user", parts: [{ text: "Words: " + JSON.stringify(words) }] }, ], generationConfig: { temperature: 0.1,