Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
DDMeaqua committed Nov 1, 2024
1 parent 7a8d557 commit afe12c2
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 71 deletions.
6 changes: 3 additions & 3 deletions app/api/[provider]/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { handle as moonshotHandler } from "../../moonshot";
import { handle as stabilityHandler } from "../../stability";
import { handle as iflytekHandler } from "../../iflytek";
import { handle as xaiHandler } from "../../xai";
import { handle as glmHandler } from "../../glm";
import { handle as chatglmHandler } from "../../glm";
import { handle as proxyHandler } from "../../proxy";

async function handle(
Expand Down Expand Up @@ -42,8 +42,8 @@ async function handle(
return iflytekHandler(req, { params });
case ApiPath.XAI:
return xaiHandler(req, { params });
case ApiPath.GLM:
return glmHandler(req, { params });
case ApiPath.ChatGLM:
return chatglmHandler(req, { params });
case ApiPath.OpenAI:
return openaiHandler(req, { params });
default:
Expand Down
4 changes: 2 additions & 2 deletions app/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export function auth(req: NextRequest, modelProvider: ModelProvider) {
case ModelProvider.XAI:
systemApiKey = serverConfig.xaiApiKey;
break;
case ModelProvider.GLM:
systemApiKey = serverConfig.glmApiKey;
case ModelProvider.ChatGLM:
systemApiKey = serverConfig.chatglmApiKey;
break;
case ModelProvider.GPT:
default:
Expand Down
8 changes: 4 additions & 4 deletions app/api/glm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getServerSideConfig } from "@/app/config/server";
import {
GLM_BASE_URL,
CHATGLM_BASE_URL,
ApiPath,
ModelProvider,
ServiceProvider,
Expand Down Expand Up @@ -42,9 +42,9 @@ async function request(req: NextRequest) {
const controller = new AbortController();

// alibaba use base url or just remove the path
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.GLM, "");
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.ChatGLM, "");

let baseUrl = serverConfig.glmUrl || GLM_BASE_URL;
let baseUrl = serverConfig.chatglmUrl || CHATGLM_BASE_URL;

if (!baseUrl.startsWith("http")) {
baseUrl = `https://${baseUrl}`;
Expand Down Expand Up @@ -92,7 +92,7 @@ async function request(req: NextRequest) {
isModelAvailableInServer(
serverConfig.customModels,
jsonBody?.model as string,
ServiceProvider.GLM as string,
ServiceProvider.ChatGLM as string,
)
) {
return NextResponse.json(
Expand Down
18 changes: 9 additions & 9 deletions app/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { HunyuanApi } from "./platforms/tencent";
import { MoonshotApi } from "./platforms/moonshot";
import { SparkApi } from "./platforms/iflytek";
import { XAIApi } from "./platforms/xai";
import { GLMApi } from "./platforms/glm";
import { ChatGLMApi } from "./platforms/glm";

export const ROLES = ["system", "user", "assistant"] as const;
export type MessageRole = (typeof ROLES)[number];
Expand Down Expand Up @@ -157,8 +157,8 @@ export class ClientApi {
case ModelProvider.XAI:
this.llm = new XAIApi();
break;
case ModelProvider.GLM:
this.llm = new GLMApi();
case ModelProvider.ChatGLM:
this.llm = new ChatGLMApi();
break;
default:
this.llm = new ChatGPTApi();
Expand Down Expand Up @@ -248,7 +248,7 @@ export function getHeaders(ignoreHeaders: boolean = false) {
const isMoonshot = modelConfig.providerName === ServiceProvider.Moonshot;
const isIflytek = modelConfig.providerName === ServiceProvider.Iflytek;
const isXAI = modelConfig.providerName === ServiceProvider.XAI;
const isGLM = modelConfig.providerName === ServiceProvider.GLM;
const isChatGLM = modelConfig.providerName === ServiceProvider.ChatGLM;
const isEnabledAccessControl = accessStore.enabledAccessControl();
const apiKey = isGoogle
? accessStore.googleApiKey
Expand All @@ -264,8 +264,8 @@ export function getHeaders(ignoreHeaders: boolean = false) {
? accessStore.moonshotApiKey
: isXAI
? accessStore.xaiApiKey
: isGLM
? accessStore.glmApiKey
: isChatGLM
? accessStore.chatglmApiKey
: isIflytek
? accessStore.iflytekApiKey && accessStore.iflytekApiSecret
? accessStore.iflytekApiKey + ":" + accessStore.iflytekApiSecret
Expand All @@ -281,7 +281,7 @@ export function getHeaders(ignoreHeaders: boolean = false) {
isMoonshot,
isIflytek,
isXAI,
isGLM,
isChatGLM,
apiKey,
isEnabledAccessControl,
};
Expand Down Expand Up @@ -346,8 +346,8 @@ export function getClientApi(provider: ServiceProvider): ClientApi {
return new ClientApi(ModelProvider.Iflytek);
case ServiceProvider.XAI:
return new ClientApi(ModelProvider.XAI);
case ServiceProvider.GLM:
return new ClientApi(ModelProvider.GLM);
case ServiceProvider.ChatGLM:
return new ClientApi(ModelProvider.ChatGLM);
default:
return new ClientApi(ModelProvider.GPT);
}
Expand Down
19 changes: 12 additions & 7 deletions app/client/platforms/glm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"use client";
import { ApiPath, GLM_BASE_URL, GLM, REQUEST_TIMEOUT_MS } from "@/app/constant";
import {
ApiPath,
CHATGLM_BASE_URL,
ChatGLM,
REQUEST_TIMEOUT_MS,
} from "@/app/constant";
import {
useAccessStore,
useAppConfig,
Expand All @@ -20,7 +25,7 @@ import { getMessageTextContent } from "@/app/utils";
import { RequestPayload } from "./openai";
import { fetch } from "@/app/utils/stream";

export class GLMApi implements LLMApi {
export class ChatGLMApi implements LLMApi {
private disableListModels = true;

path(path: string): string {
Expand All @@ -29,19 +34,19 @@ export class GLMApi implements LLMApi {
let baseUrl = "";

if (accessStore.useCustomConfig) {
baseUrl = accessStore.glmUrl;
baseUrl = accessStore.chatglmUrl;
}

if (baseUrl.length === 0) {
const isApp = !!getClientConfig()?.isApp;
const apiPath = ApiPath.GLM;
baseUrl = isApp ? GLM_BASE_URL : apiPath;
const apiPath = ApiPath.ChatGLM;
baseUrl = isApp ? CHATGLM_BASE_URL : apiPath;
}

if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
}
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.GLM)) {
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.ChatGLM)) {
baseUrl = "https://" + baseUrl;
}

Expand Down Expand Up @@ -91,7 +96,7 @@ export class GLMApi implements LLMApi {
options.onController?.(controller);

try {
const chatPath = this.path(GLM.ChatPath);
const chatPath = this.path(ChatGLM.ChatPath);
const chatPayload = {
method: "POST",
body: JSON.stringify(requestPayload),
Expand Down
32 changes: 17 additions & 15 deletions app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import {
Stability,
Iflytek,
SAAS_CHAT_URL,
GLM,
ChatGLM,
} from "../constant";
import { Prompt, SearchService, usePromptStore } from "../store/prompt";
import { ErrorBoundary } from "./error";
Expand Down Expand Up @@ -1235,38 +1235,40 @@ export function Settings() {
</>
);

const glmConfigComponent = accessStore.provider === ServiceProvider.GLM && (
const chatglmConfigComponent = accessStore.provider ===
ServiceProvider.ChatGLM && (
<>
<ListItem
title={Locale.Settings.Access.GLM.Endpoint.Title}
title={Locale.Settings.Access.ChatGLM.Endpoint.Title}
subTitle={
Locale.Settings.Access.GLM.Endpoint.SubTitle + GLM.ExampleEndpoint
Locale.Settings.Access.ChatGLM.Endpoint.SubTitle +
ChatGLM.ExampleEndpoint
}
>
<input
aria-label={Locale.Settings.Access.GLM.Endpoint.Title}
aria-label={Locale.Settings.Access.ChatGLM.Endpoint.Title}
type="text"
value={accessStore.glmUrl}
placeholder={GLM.ExampleEndpoint}
value={accessStore.chatglmUrl}
placeholder={ChatGLM.ExampleEndpoint}
onChange={(e) =>
accessStore.update(
(access) => (access.glmUrl = e.currentTarget.value),
(access) => (access.chatglmUrl = e.currentTarget.value),
)
}
></input>
</ListItem>
<ListItem
title={Locale.Settings.Access.GLM.ApiKey.Title}
subTitle={Locale.Settings.Access.GLM.ApiKey.SubTitle}
title={Locale.Settings.Access.ChatGLM.ApiKey.Title}
subTitle={Locale.Settings.Access.ChatGLM.ApiKey.SubTitle}
>
<PasswordInput
aria-label={Locale.Settings.Access.GLM.ApiKey.Title}
value={accessStore.glmApiKey}
aria-label={Locale.Settings.Access.ChatGLM.ApiKey.Title}
value={accessStore.chatglmApiKey}
type="text"
placeholder={Locale.Settings.Access.GLM.ApiKey.Placeholder}
placeholder={Locale.Settings.Access.ChatGLM.ApiKey.Placeholder}
onChange={(e) => {
accessStore.update(
(access) => (access.glmApiKey = e.currentTarget.value),
(access) => (access.chatglmApiKey = e.currentTarget.value),
);
}}
/>
Expand Down Expand Up @@ -1733,7 +1735,7 @@ export function Settings() {
{stabilityConfigComponent}
{lflytekConfigComponent}
{XAIConfigComponent}
{glmConfigComponent}
{chatglmConfigComponent}
</>
)}
</>
Expand Down
14 changes: 7 additions & 7 deletions app/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ declare global {
XAI_URL?: string;
XAI_API_KEY?: string;

// glm only
GLM_URL?: string;
GLM_API_KEY?: string;
// chatglm only
CHATGLM_URL?: string;
CHATGLM_API_KEY?: string;

// custom template for preprocessing user input
DEFAULT_INPUT_TEMPLATE?: string;
Expand Down Expand Up @@ -155,7 +155,7 @@ export const getServerSideConfig = () => {
const isMoonshot = !!process.env.MOONSHOT_API_KEY;
const isIflytek = !!process.env.IFLYTEK_API_KEY;
const isXAI = !!process.env.XAI_API_KEY;
const isGLM = !!process.env.GLM_API_KEY;
const isChatGLM = !!process.env.CHATGLM_API_KEY;
// const apiKeyEnvVar = process.env.OPENAI_API_KEY ?? "";
// const apiKeys = apiKeyEnvVar.split(",").map((v) => v.trim());
// const randomIndex = Math.floor(Math.random() * apiKeys.length);
Expand Down Expand Up @@ -222,9 +222,9 @@ export const getServerSideConfig = () => {
xaiUrl: process.env.XAI_URL,
xaiApiKey: getApiKey(process.env.XAI_API_KEY),

isGLM,
glmUrl: process.env.GLM_URL,
glmApiKey: getApiKey(process.env.GLM_API_KEY),
isChatGLM,
chatglmUrl: process.env.CHATGLM_URL,
chatglmApiKey: getApiKey(process.env.CHATGLM_API_KEY),

cloudflareAccountId: process.env.CLOUDFLARE_ACCOUNT_ID,
cloudflareKVNamespaceId: process.env.CLOUDFLARE_KV_NAMESPACE_ID,
Expand Down
16 changes: 8 additions & 8 deletions app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const IFLYTEK_BASE_URL = "https://spark-api-open.xf-yun.com";

export const XAI_BASE_URL = "https://api.x.ai";

export const GLM_BASE_URL = "https://open.bigmodel.cn";
export const CHATGLM_BASE_URL = "https://open.bigmodel.cn";

export const CACHE_URL_PREFIX = "/api/cache";
export const UPLOAD_URL = `${CACHE_URL_PREFIX}/upload`;
Expand Down Expand Up @@ -64,7 +64,7 @@ export enum ApiPath {
Stability = "/api/stability",
Artifacts = "/api/artifacts",
XAI = "/api/xai",
GLM = "/api/glm",
ChatGLM = "/api/chatglm",
}

export enum SlotID {
Expand Down Expand Up @@ -118,7 +118,7 @@ export enum ServiceProvider {
Stability = "Stability",
Iflytek = "Iflytek",
XAI = "XAI",
GLM = "ChatGLM",
ChatGLM = "ChatGLM",
}

// Google API safety settings, see https://ai.google.dev/gemini-api/docs/safety-settings
Expand All @@ -142,7 +142,7 @@ export enum ModelProvider {
Moonshot = "Moonshot",
Iflytek = "Iflytek",
XAI = "XAI",
GLM = "ChatGLM",
ChatGLM = "ChatGLM",
}

export const Stability = {
Expand Down Expand Up @@ -230,8 +230,8 @@ export const XAI = {
ChatPath: "v1/chat/completions",
};

export const GLM = {
ExampleEndpoint: GLM_BASE_URL,
export const ChatGLM = {
ExampleEndpoint: CHATGLM_BASE_URL,
ChatPath: "/api/paas/v4/chat/completions",
};

Expand Down Expand Up @@ -386,7 +386,7 @@ const iflytekModels = [

const xAIModes = ["grok-beta"];

const glmModels = [
const chatglmModels = [
"glm-4-plus",
"glm-4-0520",
"glm-4",
Expand Down Expand Up @@ -520,7 +520,7 @@ export const DEFAULT_MODELS = [
sorted: 11,
},
})),
...glmModels.map((name) => ({
...chatglmModels.map((name) => ({
name,
available: true,
sorted: seq++,
Expand Down
6 changes: 3 additions & 3 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,11 @@ const cn = {
SubTitle: "样例:",
},
},
GLM: {
ChatGLM: {
ApiKey: {
Title: "接口密钥",
SubTitle: "使用自定义 GLM API Key",
Placeholder: "GLM API Key",
SubTitle: "使用自定义 ChatGLM API Key",
Placeholder: "ChatGLM API Key",
},
Endpoint: {
Title: "接口地址",
Expand Down
8 changes: 4 additions & 4 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,11 @@ const en: LocaleType = {
SubTitle: "Example: ",
},
},
GLM: {
ChatGLM: {
ApiKey: {
Title: "GLM API Key",
SubTitle: "Use a custom GLM API Key",
Placeholder: "GLM API Key",
Title: "ChatGLM API Key",
SubTitle: "Use a custom ChatGLM API Key",
Placeholder: "ChatGLM API Key",
},
Endpoint: {
Title: "Endpoint Address",
Expand Down
Loading

0 comments on commit afe12c2

Please sign in to comment.