Skip to content

Commit

Permalink
v0.8
Browse files Browse the repository at this point in the history
添加 GLM 全系模型
  • Loading branch information
SIPC committed Jan 31, 2025
1 parent 8cd0f21 commit 6e842c1
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/glm/glm_4_air.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
async function glm_4_air_Translate(text, source_lang, target_lang) {
try {
const url = "https://open.bigmodel.cn/api/paas/v4/chat/completions";
const data = {
model: "glm-4-air",
messages: [
{
role: "user",
content: "You are a professional translation engine, please translate the text into a colloquial, professional, elegant and fluent content, without the style of machine translation. You must only translate the text content, never interpret it.",
},
{
role: "assistant",
content: "Ok, I will only translate the text content, never interpret it.",
},
{
role: "user",
content: `Translate into Chinese """hello"""`,
},
{
role: "assistant",
content: "你好",
},
{
role: "user",
content: `Translate into ${target_lang} """${text}"""`,
},
]
};

const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.GLM_API_KEY}`,
};

const response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(data),
});

const result = await response.json();
console.log(result.choices[0].message);

return JSON.stringify({
model: "glm_4_air",
raw: text,
text: result.choices[0].message.content,
from: source_lang,
to: target_lang,
});
} catch (error) {
console.error("glm_4_air error:", error);
return null;
}
}

export { glm_4_air_Translate };

57 changes: 57 additions & 0 deletions lib/glm/glm_4_flash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
async function glm_4_flash_Translate(text, source_lang, target_lang) {
try {
const url = "https://open.bigmodel.cn/api/paas/v4/chat/completions";
const data = {
model: "glm-4-flash",
messages: [
{
role: "user",
content: "You are a professional translation engine, please translate the text into a colloquial, professional, elegant and fluent content, without the style of machine translation. You must only translate the text content, never interpret it.",
},
{
role: "assistant",
content: "Ok, I will only translate the text content, never interpret it.",
},
{
role: "user",
content: `Translate into Chinese """hello"""`,
},
{
role: "assistant",
content: "你好",
},
{
role: "user",
content: `Translate into ${target_lang} """${text}"""`,
},
]
};

const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.GLM_API_KEY}`,
};

const response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(data),
});

const result = await response.json();
console.log(result.choices[0].message);

return JSON.stringify({
model: "glm_4_flash",
raw: text,
text: result.choices[0].message.content,
from: source_lang,
to: target_lang,
});
} catch (error) {
console.error("glm_4_flash error:", error);
return null;
}
}

export { glm_4_flash_Translate };
57 changes: 57 additions & 0 deletions lib/glm/glm_4_plus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
async function glm_4_plus_Translate(text, source_lang, target_lang) {
try {
const url = "https://open.bigmodel.cn/api/paas/v4/chat/completions";
const data = {
model: "glm-4-plus",
messages: [
{
role: "user",
content: "You are a professional translation engine, please translate the text into a colloquial, professional, elegant and fluent content, without the style of machine translation. You must only translate the text content, never interpret it.",
},
{
role: "assistant",
content: "Ok, I will only translate the text content, never interpret it.",
},
{
role: "user",
content: `Translate into Chinese """hello"""`,
},
{
role: "assistant",
content: "你好",
},
{
role: "user",
content: `Translate into ${target_lang} """${text}"""`,
},
]
};

const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.GLM_API_KEY}`,
};

const response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(data),
});

const result = await response.json();
console.log(result.choices[0].message);

return JSON.stringify({
model: "glm_4_plus",
raw: text,
text: result.choices[0].message.content,
from: source_lang,
to: target_lang,
});
} catch (error) {
console.error("glm_4_plus error:", error);
return null;
}
}

export { glm_4_plus_Translate };
9 changes: 9 additions & 0 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ import { papago_Translate } from "./lib/papago.js";
import { niutrans_Translate } from "./lib/niutrans.js";
import { reverso_Translate } from "./lib/reverso.js";
import { caiyun_Translate } from "./lib/caiyun.js";
import { glm_4_air_Translate } from "./lib/glm/glm_4_air.js";
import { glm_4_flash_Translate } from "./lib/glm/glm_4_flash.js";
import { glm_4_plus_Translate } from "./lib/glm/glm_4_plus.js";



const translationFunctions = {
xinghuo_lite: xinghuo_lite_Translate,
xinghuo_pro: xinghuo_pro_Translate,
xinghuo_max: xinghuo_max_Translate,

glm_4_flash: glm_4_flash_Translate,
glm_4_air: glm_4_air_Translate,
glm_4_plus: glm_4_plus_Translate,

deepl: deepl_Translate,
google: google_Translate,
microsoft: microsoft_Translate,
Expand All @@ -30,6 +38,7 @@ const translationFunctions = {
niutrans: niutrans_Translate,
reverso: reverso_Translate,
caiyun: caiyun_Translate,

};

async function Translate(model, text, form, to) {
Expand Down
4 changes: 4 additions & 0 deletions tool/iso.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const modelLanguageCodes = {
'xinghuo_lite': { 'zh-Hans': 'Simplified Chinese', 'en': 'English', 'de': 'German', 'fr': 'French', 'it': 'Italian', 'ja': 'Japanese', 'ko': 'Korean' },
'xinghuo_pro': { 'zh-Hans': 'Simplified Chinese', 'en': 'English', 'de': 'German', 'fr': 'French', 'it': 'Italian', 'ja': 'Japanese', 'ko': 'Korean' },
'xinghuo_max': { 'zh-Hans': 'Simplified Chinese', 'en': 'English', 'de': 'German', 'fr': 'French', 'it': 'Italian', 'ja': 'Japanese', 'ko': 'Korean' },
// glm
'glm_4_flash': { 'zh-Hans': 'Simplified Chinese', 'en': 'English', 'de': 'German', 'fr': 'French', 'it': 'Italian', 'ja': 'Japanese', 'ko': 'Korean' },
'glm_4_air': { 'zh-Hans': 'Simplified Chinese', 'en': 'English', 'de': 'German', 'fr': 'French', 'it': 'Italian', 'ja': 'Japanese', 'ko': 'Korean' },
'glm_4_plus': { 'zh-Hans': 'Simplified Chinese', 'en': 'English', 'de': 'German', 'fr': 'French', 'it': 'Italian', 'ja': 'Japanese', 'ko': 'Korean' },

'deepl': { 'zh-Hans': 'ZH', 'en': 'EN', 'de': 'DE', 'fr': 'FR', 'it': 'IT', 'ja': 'JA', 'ko': 'KO' },
'google': { 'zh-Hans': 'zh-CN', 'zh-Hant': 'zh-TW', 'en': 'en', 'de': 'de', 'fr': 'fr', 'it': 'it', 'ja': 'ja', 'ko': 'ko' },
Expand Down

0 comments on commit 6e842c1

Please sign in to comment.