From 5c0c2bf20d079e674be2618341a34acfa8931332 Mon Sep 17 00:00:00 2001
From: "sipc.ink" <92251518+SIPC@users.noreply.github.com>
Date: Sun, 10 Mar 2024 00:30:20 +0800
Subject: [PATCH] add models
add:
ChatGPT 4
claude
---
.env.example | 11 ++++-
src/components/ResultBox.tsx | 24 ++++++++---
src/lib/api.ts | 10 ++++-
src/lib/utils.ts | 2 +-
src/pages/api/lib/autodetect.ts | 2 +-
src/pages/api/lib/chatgpt.ts | 21 ++++++++-
src/pages/api/lib/chatnio.ts | 66 +++++++++++++++++++++++++++++
src/pages/api/lib/gemini.ts | 2 +-
src/pages/api/lib/m2m100-archive.ts | 3 +-
src/pages/api/translate.ts | 16 +++++++
10 files changed, 141 insertions(+), 16 deletions(-)
create mode 100644 src/pages/api/lib/chatnio.ts
diff --git a/.env.example b/.env.example
index 9da9f23..bb5fad1 100644
--- a/.env.example
+++ b/.env.example
@@ -1,6 +1,9 @@
-OpenAI_API_ENDPOINT='https://api.chatnio.net/v1/chat/completions'
+ChatNio_API_KEY='sk-'
+
+OpenAI_API_ENDPOINT='https://api.openai.com/v1/chat/completions'
OpenAI_API_KEY='sk-'
OpenAI_MODEL='gpt-3.5-turbo'
+OpenAI_MODEL_4='gpt-4'
Gemini_API_ENDPOINT='https://generativelanguage.googleapis.com'
Gemini_API_KEY=''
@@ -14,4 +17,8 @@ BAIDU_KEY=''
QWEN_API_ENDPOINT='https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation'
QWEN_API_KEY='sk-'
-QWEN_MODEL='qwen-turbo'
\ No newline at end of file
+QWEN_MODEL='qwen-turbo'
+
+GLM_API_ENDPOINT='https://open.bigmodel.cn/api/paas/v4/chat/completions'
+GLM_API_KEY=''
+GLM_MODEL='glm-3-turbo'
\ No newline at end of file
diff --git a/src/components/ResultBox.tsx b/src/components/ResultBox.tsx
index d9bece1..0822aa4 100644
--- a/src/components/ResultBox.tsx
+++ b/src/components/ResultBox.tsx
@@ -127,23 +127,33 @@ export function ResultContainer({
className={`result-container ${isExpanded ? "grid-cols-4" : "grid-cols-3"} grid gap-4`}
>
+
+
-
= {
"zh-cn": "zh",
diff --git a/src/pages/api/lib/chatgpt.ts b/src/pages/api/lib/chatgpt.ts
index 514dd4e..4375d0c 100644
--- a/src/pages/api/lib/chatgpt.ts
+++ b/src/pages/api/lib/chatgpt.ts
@@ -7,18 +7,26 @@ export class ChatGPT {
public key: string;
public apiUrl: string;
public model: string;
+ public model4: string;
constructor(
key: string,
apiUrl = "https://api.openai.com/v1/chat/completions",
model = "gpt-3.5-turbo",
+ model4 = "gpt-4",
) {
this.key = key;
this.apiUrl = apiUrl;
this.model = model;
+ this.model4 = model4;
}
- async translate(text: string, target: string, source: string = "auto") {
+ async translate(
+ model: string,
+ text: string,
+ target: string,
+ source: string = "auto",
+ ) {
if (target === "classical-chinese") {
target = "文言文";
if (source === "zh") {
@@ -31,13 +39,21 @@ export class ChatGPT {
target = "白话文";
}
}
+ const models = (model:string) =>{
+ if (model === '3') {
+ return this.model
+ }
+ if (model === '4') {
+ return this.model4
+ }
+ }
try {
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${this.key}`,
};
const data = JSON.stringify({
- model: this.model,
+ model: models(model),
messages: [
{
role: "system",
@@ -66,4 +82,5 @@ export const ChatGPTInstance = new ChatGPT(
process.env.OpenAI_API_KEY!,
process.env.OpenAI_API_ENDPOINT!,
process.env.OpenAI_MODEL!,
+ process.env.OpenAI_MODEL_4!,
);
diff --git a/src/pages/api/lib/chatnio.ts b/src/pages/api/lib/chatnio.ts
new file mode 100644
index 0000000..2652053
--- /dev/null
+++ b/src/pages/api/lib/chatnio.ts
@@ -0,0 +1,66 @@
+// code from sipc
+
+import axios from "axios";
+import { getErrorMessage } from "@/pages/api/lib/utils";
+
+export class ChatNio {
+ public key: string;
+
+ constructor(key: string) {
+ this.key = key;
+ }
+
+ async translate(
+ model: string,
+ 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 headers = {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${this.key}`,
+ };
+ const data = JSON.stringify({
+ 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(
+ "https://api.chatnio.net/v1/chat/completions",
+ data,
+ { headers },
+ );
+ return response.data.choices[0].message.content;
+ } catch (error) {
+ throw new Error(`Error while translating: ${getErrorMessage(error)}`);
+ }
+ }
+}
+
+export const ChatNioInstance = new ChatNio(process.env.ChatNio_API_KEY!);
diff --git a/src/pages/api/lib/gemini.ts b/src/pages/api/lib/gemini.ts
index 577c8a5..0e43f83 100644
--- a/src/pages/api/lib/gemini.ts
+++ b/src/pages/api/lib/gemini.ts
@@ -1,4 +1,4 @@
-const axios = require("axios");
+import axios from "axios";
import { getErrorMessage } from "@/pages/api/lib/utils";
export class Gemini {
diff --git a/src/pages/api/lib/m2m100-archive.ts b/src/pages/api/lib/m2m100-archive.ts
index cbb1956..2a34a82 100644
--- a/src/pages/api/lib/m2m100-archive.ts
+++ b/src/pages/api/lib/m2m100-archive.ts
@@ -1,8 +1,7 @@
// code from sipc
-import { getErrorMessage } from "@/pages/api/lib/utils";
-
import axios from "axios";
+import { getErrorMessage } from "@/pages/api/lib/utils";
export class M2m100 {
public apiUrl: string;
diff --git a/src/pages/api/translate.ts b/src/pages/api/translate.ts
index 41808b2..3831ac2 100644
--- a/src/pages/api/translate.ts
+++ b/src/pages/api/translate.ts
@@ -1,5 +1,6 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
+import { ChatNioInstance } from "./lib/chatnio";
import { ChatGPTInstance } from "./lib/chatgpt";
import { DeeplXInstance } from "./lib/deeplx";
import { MicrosoftInstance } from "./lib/microsoft";
@@ -46,6 +47,14 @@ export default async function handler(
switch (model) {
case "chatgpt":
return await ChatGPTInstance.translate(
+ "3",
+ text,
+ targetLanguage,
+ sourceLanguage,
+ ).catch((e) => e.message);
+ case "chatgpt4":
+ return await ChatGPTInstance.translate(
+ "4",
text,
targetLanguage,
sourceLanguage,
@@ -62,6 +71,13 @@ export default async function handler(
targetLanguage,
sourceLanguage,
).catch((e) => e.message);
+ case "claude":
+ return await ChatNioInstance.translate(
+ "claude-3-sonnet-20240229",
+ text,
+ targetLanguage,
+ sourceLanguage,
+ ).catch((e) => e.message);
case "glm":
return await GLMInstance.translate(
text,