diff --git a/src/apis/index.js b/src/apis/index.js index 2e3f29f..d8bb923 100644 --- a/src/apis/index.js +++ b/src/apis/index.js @@ -9,6 +9,7 @@ import { OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, OPT_TRANS_OPENAI, + OPT_TRANS_CLOUDFLAREAI, OPT_TRANS_CUSTOMIZE, URL_CACHE_TRAN, KV_SALT_SYNC, @@ -176,12 +177,15 @@ export const apiTranslate = async ({ trText = res?.choices?.[0].message.content; isSame = text === trText; break; + case OPT_TRANS_CLOUDFLAREAI: + trText = res?.result?.translated_text; + isSame = text === trText; + break; case OPT_TRANS_CUSTOMIZE: trText = res.text; isSame = to === res.from; break; default: - break; } return [trText, isSame, res]; diff --git a/src/config/index.js b/src/config/index.js index e28325a..cd8915b 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -87,6 +87,7 @@ export const OPT_TRANS_DEEPLFREE = "DeepLFree"; export const OPT_TRANS_BAIDU = "Baidu"; export const OPT_TRANS_TENCENT = "Tencent"; export const OPT_TRANS_OPENAI = "OpenAI"; +export const OPT_TRANS_CLOUDFLAREAI = "CloudflareAI"; export const OPT_TRANS_CUSTOMIZE = "Custom"; export const OPT_TRANS_ALL = [ OPT_TRANS_GOOGLE, @@ -97,6 +98,7 @@ export const OPT_TRANS_ALL = [ OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, OPT_TRANS_OPENAI, + OPT_TRANS_CLOUDFLAREAI, OPT_TRANS_CUSTOMIZE, ]; @@ -218,6 +220,20 @@ export const OPT_LANGS_SPECIAL = { [OPT_TRANS_OPENAI]: new Map( OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]]) ), + [OPT_TRANS_CLOUDFLAREAI]: new Map([ + ["auto", ""], + ["zh-CN", "chinese"], + ["zh-TW", "chinese"], + ["en", "english"], + ["ar", "arabic"], + ["de", "german"], + ["ru", "russian"], + ["fr", "french"], + ["pt", "portuguese"], + ["ja", "japanese"], + ["es", "spanish"], + ["hi", "hindi"], + ]), [OPT_TRANS_CUSTOMIZE]: new Map([ ...OPT_LANGS_FROM.map(([key]) => [key, key]), ["auto", ""], @@ -360,6 +376,10 @@ export const DEFAULT_TRANS_APIS = { model: "gpt-4", prompt: `You will be provided with a sentence in ${PROMPT_PLACE_FROM}, and your task is to translate it into ${PROMPT_PLACE_TO}.`, }, + [OPT_TRANS_CLOUDFLAREAI]: { + url: "https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/m2m100-1.2b", + key: "", + }, [OPT_TRANS_CUSTOMIZE]: { url: "", key: "", diff --git a/src/libs/req.js b/src/libs/req.js index bc42f63..b3b5ab9 100644 --- a/src/libs/req.js +++ b/src/libs/req.js @@ -8,6 +8,7 @@ import { OPT_TRANS_BAIDU, OPT_TRANS_TENCENT, OPT_TRANS_OPENAI, + OPT_TRANS_CLOUDFLAREAI, OPT_TRANS_CUSTOMIZE, URL_MICROSOFT_TRAN, URL_TENCENT_TRANSMART, @@ -143,7 +144,7 @@ const genTencent = ({ text, from, to }) => { return [URL_TENCENT_TRANSMART, init]; }; -const genOpenai = ({ text, from, to, url, key, prompt, model }) => { +const genOpenAI = ({ text, from, to, url, key, prompt, model }) => { prompt = prompt .replaceAll(PROMPT_PLACE_FROM, from) .replaceAll(PROMPT_PLACE_TO, to); @@ -177,6 +178,25 @@ const genOpenai = ({ text, from, to, url, key, prompt, model }) => { return [url, init]; }; +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]; +}; + const genCustom = ({ text, from, to, url, key }) => { const data = { text, @@ -197,6 +217,11 @@ const genCustom = ({ text, from, to, url, key }) => { return [url, init]; }; +/** + * 构造翻译接口 request + * @param {*} + * @returns + */ export const newTransReq = ({ translator, text, from, to }, apiSetting) => { const args = { text, from, to, ...apiSetting }; switch (translator) { @@ -215,7 +240,9 @@ export const newTransReq = ({ translator, text, from, to }, apiSetting) => { case OPT_TRANS_TENCENT: return genTencent(args); case OPT_TRANS_OPENAI: - return genOpenai(args); + return genOpenAI(args); + case OPT_TRANS_CLOUDFLAREAI: + return genCloudflareAI(args); case OPT_TRANS_CUSTOMIZE: return genCustom(args); default: