UNPKG

mtengines

Version:

Machine Translation (MT) library written in TypeScript

176 lines 7.24 kB
"use strict"; /******************************************************************************* * 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 *******************************************************************************/ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnthropicTranslator = void 0; const sdk_1 = __importDefault(require("@anthropic-ai/sdk")); const MTMatch_1 = require("./MTMatch"); const MTUtils_1 = require("./MTUtils"); class AnthropicTranslator { // Claude 4 Models static CLAUDE_OPUS_4 = "claude-opus-4-0"; static CLAUDE_SONNET_4 = "claude-sonnet-4-0"; // Claude 3.7 Models static CLAUDE_SONNET_3_7 = "claude-3-7-sonnet-latest"; // Claude 3.5 Models static CLAUDE_HAIKU_3_5 = "claude-3-5-haiku-latest"; static CLAUDE_SONNET_3_5 = "claude-3-5-sonnet-latest"; // Claude 3 Models static CLAUDE_HAIKU_3_0 = "claude-3-haiku-20240307"; static CLAUDE_SONNET_3_0 = "claude-3-sonnet-20240229"; static CLAUDE_OPUS_3_0 = "claude-3-opus-latest"; model = AnthropicTranslator.CLAUDE_SONNET_3_5; // Default model anthropic; srcLang; tgtLang; constructor(apiKey, model) { if (model) { this.model = model; } this.anthropic = new sdk_1.default({ apiKey: apiKey }); } getName() { return 'Anthropic Claude'; } getShortName() { return 'Anthropic'; } getModels() { let models = [ AnthropicTranslator.CLAUDE_OPUS_4, AnthropicTranslator.CLAUDE_SONNET_4, AnthropicTranslator.CLAUDE_SONNET_3_7, AnthropicTranslator.CLAUDE_SONNET_3_7, AnthropicTranslator.CLAUDE_HAIKU_3_5, AnthropicTranslator.CLAUDE_SONNET_3_5, AnthropicTranslator.CLAUDE_HAIKU_3_0, AnthropicTranslator.CLAUDE_SONNET_3_0, AnthropicTranslator.CLAUDE_OPUS_3_0 ]; models.sort((a, b) => { return a.localeCompare(b, 'en'); }); return models; } 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.getRole(this.srcLang, this.tgtLang) + ' ' + MTUtils_1.MTUtils.translatePropmt(source, this.srcLang, this.tgtLang); return new Promise((resolve, reject) => { this.createMessage(propmt).then((message) => { let jsonString = JSON.stringify(message, null, 2); let jsonObject = JSON.parse(jsonString); let translation = jsonObject.content[0].text.trim(); if (translation.startsWith('"""') && translation.endsWith('"""')) { translation = translation.substring(3, translation.length - 3).trim(); } resolve(translation); }).catch((error) => { reject(error); }); }); } getMTMatch(source, terms) { let propmt = MTUtils_1.MTUtils.getRole(this.srcLang, this.tgtLang) + ' ' + MTUtils_1.MTUtils.generatePrompt(source, this.srcLang, this.tgtLang, terms); return new Promise((resolve, reject) => { this.createMessage(propmt).then((message) => { let jsonString = JSON.stringify(message, null, 2); let jsonObject = JSON.parse(jsonString); let translation = jsonObject.content[0].text.trim(); 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(); } let target = MTUtils_1.MTUtils.toXMLElement(translation); if (source.hasAttribute('xml:space')) { target.setAttribute(source.getAttribute('xml:space')); } resolve(new MTMatch_1.MTMatch(source, target, this.getShortName())); }).catch((error) => { reject(error); }); }); } async createMessage(source) { return await this.anthropic.messages.create({ model: this.model, max_tokens: 1024, messages: [{ role: "user", content: source }], }); } handlesTags() { return true; } fixesMatches() { return true; } fixMatch(originalSource, matchSource, matchTarget) { let propmt = MTUtils_1.MTUtils.getRole(this.srcLang, this.tgtLang) + ' ' + MTUtils_1.MTUtils.fixMatchPrompt(originalSource, matchSource, matchTarget); return new Promise((resolve, reject) => { this.createMessage(propmt).then((message) => { let jsonString = JSON.stringify(message, null, 2); let jsonObject = JSON.parse(jsonString); let translation = jsonObject.content[0].text; let target = MTUtils_1.MTUtils.toXMLElement(translation); if (originalSource.hasAttribute('xml:space')) { target.setAttribute(originalSource.getAttribute('xml:space')); } resolve(new MTMatch_1.MTMatch(originalSource, target, this.getShortName())); }).catch((error) => { reject(error); }); }); } fixesTags() { return true; } fixTags(source, target) { let propmt = MTUtils_1.MTUtils.getRole(this.srcLang, this.tgtLang) + ' ' + MTUtils_1.MTUtils.fixTagsPrompt(source, target, this.srcLang, this.tgtLang); return new Promise((resolve, reject) => { this.createMessage(propmt).then((message) => { let jsonString = JSON.stringify(message, null, 2); let jsonObject = JSON.parse(jsonString); let translation = jsonObject.content[0].text; let target = MTUtils_1.MTUtils.toXMLElement(translation); if (source.hasAttribute('xml:space')) { target.setAttribute(source.getAttribute('xml:space')); } resolve(target); }).catch((error) => { reject(error); }); }); } } exports.AnthropicTranslator = AnthropicTranslator; //# sourceMappingURL=AnthropicTranslator.js.map