src / translate.ts
import { GeneratorController, Chat } from "@lmstudio/sdk";
export async function translate(
ctl: GeneratorController,
model: string,
sourceLang: string,
targetLang: string,
text: string
): Promise<string> {
try {
const llm = await ctl.client.llm.model(model);
let systemPromt = `You are a translation assistant. Translate the following text from the ${sourceLang} to ${targetLang} language, preserving meaning and style. Do not add any extra explanation.`
if (sourceLang === "") {
systemPromt = `You are a translation assistant. Translate the following text to ${targetLang} language, preserving meaning and style. Do not add any extra explanation.`
}
const chat = Chat.from([
{
role: "system",
content: systemPromt
},
{
role: "user",
content: text
},
]);
const response = await llm.respond(chat);
return response.content.trim();
} catch (error) {
throw new Error(`Translation failed: ${(error as Error).message}`);
}
}