mtengines
Version:
Machine Translation (MT) library written in TypeScript
235 lines • 9.76 kB
JavaScript
;
/*******************************************************************************
* Copyright (c) 2023 - 2025 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-v10.html
*
* Contributors:
* Maxprograms - initial API and implementation
*******************************************************************************/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatGPTTranslator = void 0;
const openai_1 = require("openai");
const typesxml_1 = require("typesxml");
const MTMatch_1 = require("./MTMatch");
const MTUtils_1 = require("./MTUtils");
class ChatGPTTranslator {
static GPT_41 = "gpt-4.1";
static GPT_41_MINI = "gpt-4.1-mini";
static GPT_41_NANO = "gpt-4.1-nano";
static GPT_4o = "gpt-4o";
static GPT_4o_MINI = "gpt-4o-mini";
static GPT_4 = "gpt-4";
static GPT_4_TURBO = "gpt-4-turbo";
static GPT_35_TURBO = "gpt-3.5-turbo";
static GPT_o3 = "o3-mini";
static GPT_o1 = "o1";
static GPT_o1_MINI = "o1-mini";
static GPT_o1_PRO = "o1-pro";
static GPT_o4_MINI = "o4-mini";
openai;
srcLang;
tgtLang;
model = ChatGPTTranslator.GPT_4o_MINI; // Default model
constructor(apiKey, model) {
this.openai = new openai_1.OpenAI({ apiKey: apiKey });
if (model) {
this.model = model;
}
}
getName() {
return 'ChatGPT API';
}
getShortName() {
return 'ChatGPT';
}
getSourceLanguages() {
return MTUtils_1.MTUtils.getLanguages();
}
getTargetLanguages() {
return MTUtils_1.MTUtils.getLanguages();
}
setSourceLanguage(lang) {
this.srcLang = lang;
}
getSourceLanguage() {
return this.srcLang;
}
setTargetLanguage(lang) {
this.tgtLang = lang;
}
getTargetLanguage() {
return this.tgtLang;
}
translate(source) {
let propmt = MTUtils_1.MTUtils.translatePropmt(source, this.srcLang, this.tgtLang);
return new Promise((resolve, reject) => {
this.openai.chat.completions.create({
model: this.model,
messages: [
{ "role": "system", "content": MTUtils_1.MTUtils.getRole(this.srcLang, this.tgtLang) },
{ "role": "user", "content": propmt }
]
}).then((completion) => {
let choices = completion.choices;
let translation = choices[0].message.content;
if (translation.startsWith('\n\n')) {
translation = translation.substring(2);
}
while (translation.startsWith('"') && translation.endsWith('"')) {
translation = translation.substring(1, translation.length - 1);
}
if (source.startsWith('"') && source.endsWith('"')) {
translation = '"' + translation + '"';
}
resolve(translation);
}).catch((error) => {
reject(error);
});
});
}
getMTMatch(source, terms) {
let propmt = MTUtils_1.MTUtils.generatePrompt(source, this.srcLang, this.tgtLang, terms);
return new Promise((resolve, reject) => {
this.openai.chat.completions.create({
model: this.model,
messages: [
{ "role": "system", "content": MTUtils_1.MTUtils.getRole(this.srcLang, this.tgtLang) },
{ "role": "user", "content": propmt }
]
}).then((completion) => {
let choices = completion.choices;
let translation = choices[0].message.content.trim();
if (translation.startsWith('\n\n')) {
translation = translation.substring(2);
}
while (translation.startsWith('"') && translation.endsWith('"')) {
translation = translation.substring(1, translation.length - 1);
}
if (translation.startsWith('```xml') && translation.endsWith('```')) {
translation = translation.substring(6, translation.length - 3).trim();
}
if (translation.startsWith('```') && translation.endsWith('```')) {
translation = translation.substring(3, translation.length - 3).trim();
}
if (!translation.trim().startsWith('<target>') && !translation.trim().endsWith('</target>')) {
translation = '<target>' + translation + '</target>';
}
let target = MTUtils_1.MTUtils.toXMLElement(translation);
resolve(new MTMatch_1.MTMatch(source, target, this.getShortName()));
}).catch((error) => {
reject(error);
});
});
}
handlesTags() {
return true;
}
getModels() {
let models = [
ChatGPTTranslator.GPT_41,
ChatGPTTranslator.GPT_41_MINI,
ChatGPTTranslator.GPT_41_NANO,
ChatGPTTranslator.GPT_4o,
ChatGPTTranslator.GPT_4o_MINI,
ChatGPTTranslator.GPT_4,
ChatGPTTranslator.GPT_4_TURBO,
ChatGPTTranslator.GPT_35_TURBO,
ChatGPTTranslator.GPT_o3,
ChatGPTTranslator.GPT_o1,
ChatGPTTranslator.GPT_o1_MINI,
ChatGPTTranslator.GPT_o1_PRO,
ChatGPTTranslator.GPT_o4_MINI
];
models.sort((a, b) => {
return a.localeCompare(b, 'en');
});
return models;
}
fixMatch(originalSource, matchSource, matchTarget) {
return new Promise((resolve, reject) => {
this.fixTranslation(originalSource, matchSource, matchTarget).then((translation) => {
let target = MTUtils_1.MTUtils.toXMLElement(translation);
resolve(new MTMatch_1.MTMatch(originalSource, target, this.getShortName()));
}).catch((error) => {
reject(error);
});
});
}
fixTranslation(originalSource, matchSource, matchTarget) {
let propmt = MTUtils_1.MTUtils.fixMatchPrompt(originalSource, matchSource, matchTarget);
return new Promise((resolve, reject) => {
this.openai.chat.completions.create({
model: this.model,
messages: [
{ "role": "system", "content": MTUtils_1.MTUtils.getRole(this.srcLang, this.tgtLang) },
{ "role": "user", "content": propmt }
]
}).then((completion) => {
let choices = completion.choices;
let translation = choices[0].message.content;
if (translation.startsWith('\n\n')) {
translation = translation.substring(2);
}
while (translation.startsWith('"') && translation.endsWith('"')) {
translation = translation.substring(1, translation.length - 1);
}
if (translation.startsWith('```xml') && translation.endsWith('```')) {
translation = translation.substring(6, translation.length - 3).trim();
}
if (!translation.trim().startsWith('<target>') && !translation.trim().endsWith('</target>')) {
translation = '<target>' + translation + '</target>';
}
resolve(translation);
}).catch((error) => {
reject(error);
});
});
}
fixesMatches() {
return true;
}
fixesTags() {
return true;
}
fixTags(source, target) {
let propmt = MTUtils_1.MTUtils.fixTagsPrompt(source, target, this.srcLang, this.tgtLang);
return new Promise((resolve, reject) => {
this.openai.chat.completions.create({
model: this.model,
messages: [
{ "role": "system", "content": MTUtils_1.MTUtils.getRole(this.srcLang, this.tgtLang) },
{ "role": "user", "content": propmt }
]
}).then((completion) => {
let choices = completion.choices;
let translation = choices[0].message.content;
if (translation.startsWith('\n\n')) {
translation = translation.substring(2);
}
while (translation.startsWith('"') && translation.endsWith('"')) {
translation = translation.substring(1, translation.length - 1);
}
if (translation.startsWith('```xml') && translation.endsWith('```')) {
translation = translation.substring(6, translation.length - 3).trim();
}
if (!translation.trim().startsWith('<target>') && !translation.trim().endsWith('</target>')) {
translation = '<target>' + translation + '</target>';
}
let contentHandler = new typesxml_1.DOMBuilder();
let xmlParser = new typesxml_1.SAXParser();
xmlParser.setContentHandler(contentHandler);
xmlParser.parseString(translation);
let newDoc = contentHandler.getDocument();
resolve(newDoc.getRoot());
}).catch((error) => {
reject(error);
});
});
}
}
exports.ChatGPTTranslator = ChatGPTTranslator;
//# sourceMappingURL=ChatGPTTranslator.js.map