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

Commit

Permalink
feat: add env
Browse files Browse the repository at this point in the history
  • Loading branch information
Hk-Gosuto committed Jun 6, 2024
1 parent 991dd04 commit c289305
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 37 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,19 @@ anthropic claude Api Url.
- 多个地址以`,`相连

### `DEFAULT_INPUT_TEMPLATE` (可选)

自定义默认的 template,用于初始化『设置』中的『用户输入预处理』配置项

### `EDGE_TTS_VOICE_NAME` (可选)

配置 Edge TTS 使用的语音声音,默认为:zh-CN-YunxiNeural
可访问 https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/language-support?tabs=tts#supported-languages 查看支持的参数

### `NEXT_PUBLIC_USE_OPENAI_ENDPOINT_FOR_ALL_MODELS` (可选)

配置所有模型都使用 OpenAI 路由,在使用类似 `one-api` 的中转项目时会很有用
将此环境变量设置为 1 即可

## 部署

### 容器部署 (推荐)
Expand Down
2 changes: 1 addition & 1 deletion app/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export async function requestOpenai(req: NextRequest) {
fetchOptions.body = clonedBody;

// not undefined and is false
if (modelTable[jsonBody?.model ?? ""].available === false) {
if (modelTable[jsonBody?.model ?? ""]?.available === false) {
return NextResponse.json(
{
error: true,
Expand Down
4 changes: 3 additions & 1 deletion app/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ export function getHeaders(ignoreHeaders?: boolean) {
const accessStore = useAccessStore.getState();
let headers: Record<string, string> = {};
const modelConfig = useChatStore.getState().currentSession().mask.modelConfig;
const isGoogle = modelConfig.model.startsWith("gemini");
const isGoogle =
modelConfig.model.startsWith("gemini") &&
!!!process.env.NEXT_PUBLIC_USE_OPENAI_ENDPOINT_FOR_ALL_MODELS;
if (!ignoreHeaders && !isGoogle) {
headers = {
"Content-Type": "application/json",
Expand Down
10 changes: 2 additions & 8 deletions app/components/exporter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IconButton } from "./button";
import {
copyToClipboard,
downloadAs,
getClientApi,
getMessageImages,
useMobileScreen,
} from "../utils";
Expand Down Expand Up @@ -313,14 +314,7 @@ export function PreviewActions(props: {
const onRenderMsgs = (msgs: ChatMessage[]) => {
setShouldExport(false);

var api: ClientApi;
if (config.modelConfig.model.startsWith("gemini")) {
api = new ClientApi(ModelProvider.GeminiPro);
} else if (identifyDefaultClaudeModel(config.modelConfig.model)) {
api = new ClientApi(ModelProvider.Claude);
} else {
api = new ClientApi(ModelProvider.GPT);
}
var api: ClientApi = getClientApi(config.modelConfig.model);

api
.share(msgs)
Expand Down
12 changes: 3 additions & 9 deletions app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import styles from "./home.module.scss";
import BotIcon from "../icons/bot.svg";
import LoadingIcon from "../icons/three-dots.svg";

import { useMobileScreen } from "../utils";
import { getClientApi, useMobileScreen } from "../utils";

import dynamic from "next/dynamic";
import { ModelProvider, Path, SlotID } from "../constant";
Expand Down Expand Up @@ -178,14 +178,8 @@ function Screen() {
export function useLoadData() {
const config = useAppConfig();

var api: ClientApi;
if (config.modelConfig.model.startsWith("gemini")) {
api = new ClientApi(ModelProvider.GeminiPro);
} else if (identifyDefaultClaudeModel(config.modelConfig.model)) {
api = new ClientApi(ModelProvider.Claude);
} else {
api = new ClientApi(ModelProvider.GPT);
}
var api: ClientApi = getClientApi(config.modelConfig.model);

useEffect(() => {
(async () => {
const models = await api.llm.models();
Expand Down
18 changes: 2 additions & 16 deletions app/store/chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { trimTopic, getMessageTextContent } from "../utils";
import { trimTopic, getMessageTextContent, getClientApi } from "../utils";

import Locale, { getLang } from "../locales";
import { showToast } from "../components/ui-lib";
Expand Down Expand Up @@ -484,13 +484,6 @@ export const useChatStore = createPersistStore(
agentCall();
}
} else {
if (modelConfig.model.startsWith("gemini")) {
api = new ClientApi(ModelProvider.GeminiPro);
} else if (identifyDefaultClaudeModel(modelConfig.model)) {
api = new ClientApi(ModelProvider.Claude);
} else {
api = new ClientApi(ModelProvider.GPT);
}
// make request
api.llm.chat({
messages: sendMessages,
Expand Down Expand Up @@ -667,14 +660,7 @@ export const useChatStore = createPersistStore(
const session = get().currentSession();
const modelConfig = session.mask.modelConfig;

var api: ClientApi;
if (modelConfig.model.startsWith("gemini")) {
api = new ClientApi(ModelProvider.GeminiPro);
} else if (identifyDefaultClaudeModel(modelConfig.model)) {
api = new ClientApi(ModelProvider.Claude);
} else {
api = new ClientApi(ModelProvider.GPT);
}
var api: ClientApi = getClientApi(config.modelConfig.model);

// remove error messages if any
const messages = session.messages;
Expand Down
19 changes: 17 additions & 2 deletions app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useEffect, useState } from "react";
import { showToast } from "./components/ui-lib";
import Locale from "./locales";
import { RequestMessage } from "./client/api";
import { DEFAULT_MODELS } from "./constant";
import { ClientApi, RequestMessage } from "./client/api";
import { DEFAULT_MODELS, ModelProvider } from "./constant";
import { identifyDefaultClaudeModel } from "./utils/checkers";

export function trimTopic(topic: string) {
// Fix an issue where double quotes still show in the Indonesian language
Expand Down Expand Up @@ -279,3 +280,17 @@ export function isSupportRAGModel(modelName: string) {
(model) => model.name === modelName,
);
}

export function getClientApi(modelName: string): ClientApi {
if (!!process.env.NEXT_PUBLIC_USE_OPENAI_ENDPOINT_FOR_ALL_MODELS)
return new ClientApi(ModelProvider.GPT);
var api: ClientApi;
if (modelName.startsWith("gemini")) {
api = new ClientApi(ModelProvider.GeminiPro);
} else if (identifyDefaultClaudeModel(modelName)) {
api = new ClientApi(ModelProvider.Claude);
} else {
api = new ClientApi(ModelProvider.GPT);
}
return api;
}

0 comments on commit c289305

Please sign in to comment.