From b9e7afc9970ecab7d2b73976d32c6e86084a7f30 Mon Sep 17 00:00:00 2001 From: "sipc.ink" <92251518+SIPC@users.noreply.github.com> Date: Thu, 29 Feb 2024 22:05:25 +0800 Subject: [PATCH] Upgrade to v1.1 add GLM model --- src/components/ResultBox.tsx | 39 ++++++++--------- src/lib/api.ts | 4 ++ src/pages/api/lib/glm.ts | 82 ++++++++++++++++++++++++++++++++++++ src/pages/api/translate.ts | 7 +++ 4 files changed, 111 insertions(+), 21 deletions(-) create mode 100644 src/pages/api/lib/glm.ts diff --git a/src/components/ResultBox.tsx b/src/components/ResultBox.tsx index ba69f44..d9bece1 100644 --- a/src/components/ResultBox.tsx +++ b/src/components/ResultBox.tsx @@ -92,12 +92,12 @@ export function ResultContainer({ isExpanded, }: ResultContainerProps) { useEffect(() => { - const version = "1.0" + const version = "1.1"; const localVersion = localStorage.getItem("version"); if (version !== localVersion) { const keys = Object.keys(localStorage); - const filteredKeys = keys.filter(key => key !== 'theme'); - filteredKeys.forEach(key => localStorage.removeItem(key)); + const filteredKeys = keys.filter((key) => key !== "theme"); + filteredKeys.forEach((key) => localStorage.removeItem(key)); } localStorage.setItem("version", version); @@ -127,57 +127,53 @@ export function ResultContainer({ className={`result-container ${isExpanded ? "grid-cols-4" : "grid-cols-3"} grid gap-4`} > + @@ -188,6 +184,7 @@ export function getResult() { "chatgpt", "gemini", "qwen", + "glm", "deeplx", "microsoft", "google", diff --git a/src/lib/api.ts b/src/lib/api.ts index c05d0d0..d45ca53 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -4,6 +4,7 @@ export type TranslateResult = { chatgpt: string; gemini: string; qwen: string; + glm: string; deeplx: string; microsoft: string; google: string; @@ -17,6 +18,7 @@ export type Translateloader = { chatgpt: boolean; gemini: boolean; qwen: boolean; + glm: boolean; deeplx: boolean; microsoft: boolean; google: boolean; @@ -36,6 +38,7 @@ export const initializeTranslateState: TranslateResult = { chatgpt: "", gemini: "", qwen: "", + glm: "", deeplx: "", microsoft: "", google: "", @@ -49,6 +52,7 @@ export const initializeTranslateloader: Translateloader = { chatgpt: false, gemini: false, qwen: false, + glm: false, deeplx: false, microsoft: false, google: false, diff --git a/src/pages/api/lib/glm.ts b/src/pages/api/lib/glm.ts new file mode 100644 index 0000000..2d28df3 --- /dev/null +++ b/src/pages/api/lib/glm.ts @@ -0,0 +1,82 @@ +// code from sipc + +import axios from "axios"; +import jwt from "jsonwebtoken"; +import { getErrorMessage } from "@/pages/api/lib/utils"; + +function generate_token(apikey: string) { + const [apiKeyId, secret] = apikey.split("."); + const headers = { alg: "HS256", sign_type: "SIGN" }; + const payload = { + api_key: apiKeyId, + exp: Date.now() + 30 * 1000, + timestamp: Date.now(), + }; + return jwt.sign(payload, secret, { header:headers }); +} + +export class GLM { + public key: string; + public apiUrl: string; + public model: string; + + constructor( + key: string, + apiUrl = "https://open.bigmodel.cn/api/paas/v4/chat/completions", + model = "glm-3-turbo", + ) { + this.key = key; + this.apiUrl = apiUrl; + this.model = model; + } + + async translate(text: string, target: string, source: string = "auto") { + if (target === "classical-chinese") { + target = "文言文"; + if (source === "zh") { + source = "白话文"; + } + } + if (source === "classical-chinese") { + source = "文言文"; + if (target === "zh") { + target = "白话文"; + } + } + try { + const token = generate_token(this.key); + const headers = { + "Content-Type": "application/json", + Authorization: token, + }; + const data = JSON.stringify({ + model: this.model, + messages: [ + { + role: "system", + content: `You are a professional, authentic translation engine, only returns translations.`, + }, + { + role: "user", + content: `Please translate the text from ${source} to ${target} language,Translation will be enclosed within tags, and they should not be included in the output.`, + }, + { + role: "user", + content: `${text}`, + }, + ], + temperature: 0.7, + }); + const response = await axios.post(this.apiUrl, data, { headers }); + return response.data.choices[0].message.content; + } catch (error) { + throw new Error(`Error while translating: ${getErrorMessage(error)}`); + } + } +} + +export const GLMInstance = new GLM( + process.env.GLM_API_KEY!, + process.env.GLM_API_ENDPOINT!, + process.env.GLM_MODEL!, +); diff --git a/src/pages/api/translate.ts b/src/pages/api/translate.ts index 2f8b37e..41808b2 100644 --- a/src/pages/api/translate.ts +++ b/src/pages/api/translate.ts @@ -9,6 +9,7 @@ import { TransmartInstance } from "./lib/transmart"; import { NiutransInstance } from "./lib/niutrans"; import { BaiduInstance } from "./lib/baidu"; import { QwenInstance } from "./lib/qwen"; +import { GLMInstance } from "./lib/glm"; import { autodetect } from "./lib/autodetect"; type TranslateResponse = { @@ -61,6 +62,12 @@ export default async function handler( targetLanguage, sourceLanguage, ).catch((e) => e.message); + case "glm": + return await GLMInstance.translate( + text, + targetLanguage, + sourceLanguage, + ).catch((e) => e.message); case "baidu": return await BaiduInstance.translate( text,