Files
kiss-translator/src/apis/trans.js

471 lines
9.8 KiB
JavaScript
Raw Normal View History

import queryString from "query-string";
import {
OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT,
OPT_TRANS_DEEPL,
OPT_TRANS_DEEPLFREE,
OPT_TRANS_DEEPLX,
2024-04-12 11:31:01 +08:00
OPT_TRANS_NIUTRANS,
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
OPT_TRANS_OPENAI,
2024-04-28 16:58:09 +08:00
OPT_TRANS_OPENAI_2,
OPT_TRANS_OPENAI_3,
2023-12-21 14:15:14 +08:00
OPT_TRANS_GEMINI,
2024-09-23 18:22:19 +08:00
OPT_TRANS_CLAUDE,
2023-10-26 11:13:50 +08:00
OPT_TRANS_CLOUDFLAREAI,
2024-04-28 21:43:20 +08:00
OPT_TRANS_OLLAMA,
OPT_TRANS_OLLAMA_2,
OPT_TRANS_OLLAMA_3,
OPT_TRANS_CUSTOMIZE,
2024-04-10 13:37:16 +08:00
OPT_TRANS_CUSTOMIZE_2,
OPT_TRANS_CUSTOMIZE_3,
OPT_TRANS_CUSTOMIZE_4,
OPT_TRANS_CUSTOMIZE_5,
URL_MICROSOFT_TRAN,
URL_TENCENT_TRANSMART,
2024-04-17 17:38:54 +08:00
INPUT_PLACE_URL,
INPUT_PLACE_FROM,
INPUT_PLACE_TO,
INPUT_PLACE_TEXT,
INPUT_PLACE_KEY,
2024-05-12 16:25:20 +08:00
INPUT_PLACE_MODEL,
} from "../config";
import { msAuth } from "../libs/auth";
import { genDeeplFree } from "./deepl";
import { genBaidu } from "./baidu";
2024-05-12 16:10:11 +08:00
import interpreter from "../libs/interpreter";
2023-12-22 11:35:46 +08:00
const keyMap = new Map();
2024-04-20 14:01:34 +08:00
const urlMap = new Map();
2023-12-22 11:35:46 +08:00
2024-04-20 14:01:34 +08:00
// 轮询key/url
const keyPick = (translator, key = "", cacheMap) => {
2023-12-22 11:35:46 +08:00
const keys = key
2024-01-19 16:02:53 +08:00
.split(/\n|,/)
2023-12-22 11:35:46 +08:00
.map((item) => item.trim())
.filter(Boolean);
if (keys.length === 0) {
return "";
}
2024-04-20 14:01:34 +08:00
const preIndex = cacheMap.get(translator) ?? -1;
2024-01-04 09:40:03 +08:00
const curIndex = (preIndex + 1) % keys.length;
2024-04-20 14:01:34 +08:00
cacheMap.set(translator, curIndex);
2023-12-22 11:35:46 +08:00
2024-01-04 09:40:03 +08:00
return keys[curIndex];
2023-12-22 11:35:46 +08:00
};
const genGoogle = ({ text, from, to, url, key }) => {
2025-04-16 18:00:54 -04:00
const body = JSON.stringify([[ [text], from, to ], "wt_lib"]);
const init = {
2025-04-16 18:00:54 -04:00
method: "POST",
headers: {
2025-04-16 18:00:54 -04:00
"Content-Type": "application/json+protobuf",
"X-Goog-API-Key": key,
},
2025-04-16 18:00:54 -04:00
body,
};
2025-04-16 18:00:54 -04:00
return [url, init];
};
const genMicrosoft = async ({ text, from, to }) => {
const [token] = await msAuth();
const params = {
from,
to,
"api-version": "3.0",
};
const input = `${URL_MICROSOFT_TRAN}?${queryString.stringify(params)}`;
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${token}`,
},
method: "POST",
body: JSON.stringify([{ Text: text }]),
};
return [input, init];
};
const genDeepl = ({ text, from, to, url, key }) => {
const data = {
text: [text],
target_lang: to,
source_lang: from,
// split_sentences: "0",
};
const init = {
headers: {
"Content-type": "application/json",
Authorization: `DeepL-Auth-Key ${key}`,
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
const genDeeplX = ({ text, from, to, url, key }) => {
const data = {
text,
target_lang: to,
source_lang: from,
};
const init = {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
if (key) {
init.headers.Authorization = `Bearer ${key}`;
}
return [url, init];
};
2024-04-12 11:31:01 +08:00
const genNiuTrans = ({ text, from, to, url, key, dictNo, memoryNo }) => {
const data = {
from,
to,
apikey: key,
src_text: text,
dictNo,
memoryNo,
};
const init = {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
const genTencent = ({ text, from, to }) => {
const data = {
header: {
fn: "auto_translation_block",
},
source: {
text_block: text,
lang: from,
},
target: {
lang: to,
},
};
const init = {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
return [URL_TENCENT_TRANSMART, init];
};
const genOpenAI = ({
text,
from,
to,
url,
key,
2024-09-25 14:03:12 +08:00
systemPrompt,
2024-11-30 00:41:29 +08:00
userPrompt,
model,
temperature,
maxTokens,
}) => {
2024-09-25 14:03:12 +08:00
// 兼容历史上作为systemPrompt的prompt如果prompt中不包含带翻译文本则添加文本到prompt末尾
2024-11-30 00:41:29 +08:00
// if (!prompt.includes(INPUT_PLACE_TEXT)) {
// prompt += `\nSource Text: ${INPUT_PLACE_TEXT}`;
// }
systemPrompt = systemPrompt
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, text);
userPrompt = userPrompt
2024-04-17 17:38:54 +08:00
.replaceAll(INPUT_PLACE_FROM, from)
2024-09-25 14:03:12 +08:00
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, text);
const data = {
model,
messages: [
{
role: "system",
2024-09-25 14:03:12 +08:00
content: systemPrompt,
},
{
role: "user",
2024-11-30 00:41:29 +08:00
content: userPrompt,
},
],
temperature,
max_tokens: maxTokens,
};
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${key}`, // OpenAI
"api-key": key, // Azure OpenAI
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
2024-11-30 00:41:29 +08:00
const genGemini = ({ text, from, to, url, key, systemPrompt, userPrompt, model }) => {
2024-05-12 16:25:20 +08:00
url = url
.replaceAll(INPUT_PLACE_MODEL, model)
.replaceAll(INPUT_PLACE_KEY, key);
2024-11-30 00:41:29 +08:00
systemPrompt = systemPrompt
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, text);
userPrompt = userPrompt
2024-04-17 17:38:54 +08:00
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
2024-04-18 00:31:36 +08:00
.replaceAll(INPUT_PLACE_TEXT, text);
2023-12-21 14:15:14 +08:00
const data = {
2024-11-30 00:41:29 +08:00
system_instruction: {
parts: {
text: systemPrompt,
}
},
contents: {
parts: {
text: userPrompt,
}
}
2023-12-21 14:15:14 +08:00
};
const init = {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
2024-05-12 16:25:20 +08:00
return [url, init];
2023-12-21 14:15:14 +08:00
};
2024-09-23 18:22:19 +08:00
const genClaude = ({
text,
from,
to,
url,
key,
systemPrompt,
2024-11-30 00:41:29 +08:00
userPrompt,
2024-09-23 18:22:19 +08:00
model,
temperature,
maxTokens,
}) => {
2024-09-25 14:03:12 +08:00
systemPrompt = systemPrompt
2024-11-30 00:41:29 +08:00
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, text);
userPrompt = userPrompt
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, text);
2024-09-23 18:22:19 +08:00
const data = {
model,
system: systemPrompt,
messages: [
{
role: "user",
2024-11-30 00:41:29 +08:00
content: userPrompt,
2024-09-23 18:22:19 +08:00
},
],
temperature,
max_tokens: maxTokens,
};
const init = {
headers: {
"Content-type": "application/json",
"anthropic-version": "2023-06-01",
"x-api-key": key,
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
const genOllama = ({ text, from, to, think,url, key, systemPrompt, userPrompt,model }) => {
2024-11-30 00:41:29 +08:00
systemPrompt = systemPrompt
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, text);
userPrompt = userPrompt
2024-04-28 21:43:20 +08:00
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, text);
const data = {
model,
2024-11-30 00:41:29 +08:00
system: systemPrompt,
prompt: userPrompt,
think: think,
2024-04-28 21:43:20 +08:00
stream: false,
};
const init = {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
if (key) {
init.headers.Authorization = `Bearer ${key}`;
}
return [url, init];
};
2023-10-26 11:13:50 +08:00
const genCloudflareAI = ({ text, from, to, url, key }) => {
const data = {
text,
source_lang: from,
target_lang: to,
};
const init = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${key}`,
},
method: "POST",
body: JSON.stringify(data),
};
return [url, init];
};
2024-05-12 16:10:11 +08:00
const genCustom = ({ text, from, to, url, key, reqHook }) => {
url = url
.replaceAll(INPUT_PLACE_URL, url)
.replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to)
.replaceAll(INPUT_PLACE_TEXT, text)
.replaceAll(INPUT_PLACE_KEY, key);
let init = {};
if (reqHook?.trim()) {
interpreter.run(`exports.reqHook = ${reqHook}`);
[url, init] = interpreter.exports.reqHook(text, from, to, url, key);
return [url, init];
}
const data = {
text,
from,
to,
};
2024-05-12 16:10:11 +08:00
init = {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify(data),
};
if (key) {
init.headers.Authorization = `Bearer ${key}`;
}
return [url, init];
};
2023-10-26 11:13:50 +08:00
/**
* 构造翻译接口请求参数
2023-10-26 11:13:50 +08:00
* @param {*}
* @returns
*/
export const genTransReq = ({ translator, text, from, to }, apiSetting) => {
const args = { text, from, to, ...apiSetting };
2023-12-22 11:35:46 +08:00
switch (translator) {
case OPT_TRANS_DEEPL:
case OPT_TRANS_OPENAI:
2024-04-28 16:58:09 +08:00
case OPT_TRANS_OPENAI_2:
case OPT_TRANS_OPENAI_3:
2023-12-22 11:35:46 +08:00
case OPT_TRANS_GEMINI:
2024-09-23 18:22:19 +08:00
case OPT_TRANS_CLAUDE:
2023-12-22 11:35:46 +08:00
case OPT_TRANS_CLOUDFLAREAI:
2024-04-28 21:43:20 +08:00
case OPT_TRANS_OLLAMA:
case OPT_TRANS_OLLAMA_2:
case OPT_TRANS_OLLAMA_3:
2024-04-12 11:31:01 +08:00
case OPT_TRANS_NIUTRANS:
2024-04-20 14:01:34 +08:00
args.key = keyPick(translator, args.key, keyMap);
break;
case OPT_TRANS_DEEPLX:
args.url = keyPick(translator, args.url, urlMap);
2023-12-22 11:35:46 +08:00
break;
default:
}
switch (translator) {
case OPT_TRANS_GOOGLE:
return genGoogle(args);
case OPT_TRANS_MICROSOFT:
return genMicrosoft(args);
case OPT_TRANS_DEEPL:
return genDeepl(args);
case OPT_TRANS_DEEPLFREE:
return genDeeplFree(args);
case OPT_TRANS_DEEPLX:
return genDeeplX(args);
2024-04-12 11:31:01 +08:00
case OPT_TRANS_NIUTRANS:
return genNiuTrans(args);
case OPT_TRANS_BAIDU:
return genBaidu(args);
case OPT_TRANS_TENCENT:
return genTencent(args);
case OPT_TRANS_OPENAI:
2024-04-28 16:58:09 +08:00
case OPT_TRANS_OPENAI_2:
case OPT_TRANS_OPENAI_3:
2023-10-26 11:13:50 +08:00
return genOpenAI(args);
2023-12-21 14:15:14 +08:00
case OPT_TRANS_GEMINI:
return genGemini(args);
2024-09-23 18:22:19 +08:00
case OPT_TRANS_CLAUDE:
return genClaude(args);
2023-10-26 11:13:50 +08:00
case OPT_TRANS_CLOUDFLAREAI:
return genCloudflareAI(args);
2024-04-28 21:43:20 +08:00
case OPT_TRANS_OLLAMA:
case OPT_TRANS_OLLAMA_2:
case OPT_TRANS_OLLAMA_3:
return genOllama(args);
case OPT_TRANS_CUSTOMIZE:
2024-04-10 13:37:16 +08:00
case OPT_TRANS_CUSTOMIZE_2:
case OPT_TRANS_CUSTOMIZE_3:
case OPT_TRANS_CUSTOMIZE_4:
case OPT_TRANS_CUSTOMIZE_5:
return genCustom(args);
default:
throw new Error(`[trans] translator: ${translator} not support`);
}
};