Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
add models
Browse files Browse the repository at this point in the history
add:
ChatGPT 4
claude
  • Loading branch information
SIPC committed Mar 9, 2024
1 parent db2f963 commit 5c0c2bf
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 16 deletions.
11 changes: 9 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=''
Expand All @@ -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'
QWEN_MODEL='qwen-turbo'

GLM_API_ENDPOINT='https://open.bigmodel.cn/api/paas/v4/chat/completions'
GLM_API_KEY=''
GLM_MODEL='glm-3-turbo'
24 changes: 18 additions & 6 deletions src/components/ResultBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,33 @@ export function ResultContainer({
className={`result-container ${isExpanded ? "grid-cols-4" : "grid-cols-3"} grid gap-4`}
>
<ResultBox
name="ChatGPT"
name="ChatGPT 3.5"
loading={loading.chatgpt}
content={result.chatgpt}
/>
<ResultBox
name="Gemini"
name="ChatGPT 4"
loading={loading.chatgpt4}
content={result.chatgpt4}
/>
<ResultBox
name="Gemini Pro"
loading={loading.gemini}
content={result.gemini}
/>
<ResultBox
name="Claude 3 Sonnet"
loading={loading.claude}
content={result.claude}
/>
<ResultBox
name="Qwen"
name="Qwen Turbo"
loading={loading.qwen}
content={result.qwen}
/>
<ResultBox
name="GLM"
loading={loading.qwen}
<ResultBox
name="GLM 3 Turbo"
loading={loading.glm}
content={result.glm}
/>
<ResultBox
Expand Down Expand Up @@ -182,7 +192,9 @@ export function ResultContainer({
export function getResult() {
return [
"chatgpt",
"chatgpt4",
"gemini",
"claude",
"qwen",
"glm",
"deeplx",
Expand Down
10 changes: 9 additions & 1 deletion src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import axios from "axios";

export type TranslateResult = {
chatgpt: string;
chatgpt4: string;
gemini: string;
qwen: string;
claude: string;
glm: string;
deeplx: string;
microsoft: string;
Expand All @@ -14,10 +16,12 @@ export type TranslateResult = {
};

export type Translateloader = {
translate: boolean,
translate: boolean;
chatgpt: boolean;
chatgpt4: boolean;
gemini: boolean;
qwen: boolean;
claude: boolean;
glm: boolean;
deeplx: boolean;
microsoft: boolean;
Expand All @@ -36,8 +40,10 @@ export type TranslateResponse = {

export const initializeTranslateState: TranslateResult = {
chatgpt: "",
chatgpt4: "",
gemini: "",
qwen: "",
claude: "",
glm: "",
deeplx: "",
microsoft: "",
Expand All @@ -50,8 +56,10 @@ export const initializeTranslateState: TranslateResult = {
export const initializeTranslateloader: Translateloader = {
translate: false,
chatgpt: false,
chatgpt4: false,
gemini: false,
qwen: false,
claude: false,
glm: false,
deeplx: false,
microsoft: false,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ export async function copyClipboard(text: string) {
} catch (e) {
console.warn(e);
}
}
}
2 changes: 1 addition & 1 deletion src/pages/api/lib/autodetect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// code from sipc
import axios from "axios";
const { detectOne } = require("langdetect");
import { detectOne } from "langdetect";

const detectionMap: Record<string, string> = {
"zh-cn": "zh",
Expand Down
21 changes: 19 additions & 2 deletions src/pages/api/lib/chatgpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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",
Expand Down Expand Up @@ -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!,
);
66 changes: 66 additions & 0 deletions src/pages/api/lib/chatnio.ts
Original file line number Diff line number Diff line change
@@ -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 <start></end> tags, and they should not be included in the output.`,
},
{
role: "user",
content: `<start>${text}</end>`,
},
],
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!);
2 changes: 1 addition & 1 deletion src/pages/api/lib/gemini.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const axios = require("axios");
import axios from "axios";
import { getErrorMessage } from "@/pages/api/lib/utils";

export class Gemini {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/api/lib/m2m100-archive.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/pages/api/translate.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 5c0c2bf

Please sign in to comment.