From b8f8837a8f2ff165c5321cecf0688d26ee850b6d Mon Sep 17 00:00:00 2001 From: josc146 Date: Tue, 12 Dec 2023 22:37:36 +0800 Subject: [PATCH] allow overriding Core API URL --- frontend/src/_locales/ja/main.json | 4 +++- frontend/src/_locales/zh-hans/main.json | 4 +++- frontend/src/pages/Chat.tsx | 2 +- frontend/src/pages/Completion.tsx | 2 +- frontend/src/pages/Composition.tsx | 2 +- frontend/src/pages/Settings.tsx | 10 ++++++++++ frontend/src/stores/commonStore.ts | 3 ++- frontend/src/types/settings.ts | 1 + frontend/src/utils/index.tsx | 6 +++++- 9 files changed, 27 insertions(+), 7 deletions(-) diff --git a/frontend/src/_locales/ja/main.json b/frontend/src/_locales/ja/main.json index 6337b0b..e37e9f5 100644 --- a/frontend/src/_locales/ja/main.json +++ b/frontend/src/_locales/ja/main.json @@ -317,5 +317,7 @@ "Please convert model to GGML format first": "モデルをGGML形式に変換してください", "Convert To GGML Format": "GGML形式に変換", "CPU (rwkv.cpp, Faster)": "CPU (rwkv.cpp, 高速)", - "Play With External Player": "外部プレーヤーで再生" + "Play With External Player": "外部プレーヤーで再生", + "Core API URL": "コアAPI URL", + "Override core API URL(/chat/completions and /completions). If you don't know what this is, leave it blank.": "コアAPI URLを上書きします(/chat/completions と /completions)。何であるかわからない場合は空白のままにしてください。" } \ No newline at end of file diff --git a/frontend/src/_locales/zh-hans/main.json b/frontend/src/_locales/zh-hans/main.json index 4890e42..5b8f30f 100644 --- a/frontend/src/_locales/zh-hans/main.json +++ b/frontend/src/_locales/zh-hans/main.json @@ -317,5 +317,7 @@ "Please convert model to GGML format first": "请先将模型转换为GGML格式", "Convert To GGML Format": "转换为GGML格式", "CPU (rwkv.cpp, Faster)": "CPU (rwkv.cpp, 更快)", - "Play With External Player": "使用外部播放器播放" + "Play With External Player": "使用外部播放器播放", + "Core API URL": "核心 API URL", + "Override core API URL(/chat/completions and /completions). If you don't know what this is, leave it blank.": "覆盖核心的 API URL (/chat/completions 和 /completions)。如果你不知道这是什么,请留空" } \ No newline at end of file diff --git a/frontend/src/pages/Chat.tsx b/frontend/src/pages/Chat.tsx index cd42740..6a80322 100644 --- a/frontend/src/pages/Chat.tsx +++ b/frontend/src/pages/Chat.tsx @@ -436,7 +436,7 @@ const ChatPanel: FC = observer(() => { const chatSseController = new AbortController(); chatSseControllers[answerId] = chatSseController; fetchEventSource( // https://api.openai.com/v1/chat/completions || http://127.0.0.1:${port}/v1/chat/completions - getServerRoot(port) + '/v1/chat/completions', + getServerRoot(port, true) + '/v1/chat/completions', { method: 'POST', headers: { diff --git a/frontend/src/pages/Completion.tsx b/frontend/src/pages/Completion.tsx index 2f1f54e..8a209d0 100644 --- a/frontend/src/pages/Completion.tsx +++ b/frontend/src/pages/Completion.tsx @@ -82,7 +82,7 @@ const CompletionPanel: FC = observer(() => { let answer = ''; completionSseController = new AbortController(); fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/v1/completions - getServerRoot(port) + '/v1/completions', + getServerRoot(port, true) + '/v1/completions', { method: 'POST', headers: { diff --git a/frontend/src/pages/Composition.tsx b/frontend/src/pages/Composition.tsx index 0caaf1f..3a164ae 100644 --- a/frontend/src/pages/Composition.tsx +++ b/frontend/src/pages/Composition.tsx @@ -173,7 +173,7 @@ const CompositionPanel: FC = observer(() => { let answer = ''; compositionSseController = new AbortController(); fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/v1/completions - getServerRoot(port) + '/v1/completions', + getServerRoot(port, true) + '/v1/completions', { method: 'POST', headers: { diff --git a/frontend/src/pages/Settings.tsx b/frontend/src/pages/Settings.tsx index ed711b4..5a0db5f 100644 --- a/frontend/src/pages/Settings.tsx +++ b/frontend/src/pages/Settings.tsx @@ -186,6 +186,16 @@ export const AdvancedGeneralSettings: FC = observer(() => { } /> + { + commonStore.setSettings({ + coreApiUrl: data.value + }); + }} /> + } /> ; }); diff --git a/frontend/src/stores/commonStore.ts b/frontend/src/stores/commonStore.ts index 34962eb..b33cb2d 100644 --- a/frontend/src/stores/commonStore.ts +++ b/frontend/src/stores/commonStore.ts @@ -175,7 +175,8 @@ class CommonStore { apiUrl: '', apiKey: '', apiChatModelName: 'rwkv', - apiCompletionModelName: 'rwkv' + apiCompletionModelName: 'rwkv', + coreApiUrl: '' }; // about about: AboutContent = manifest.about; diff --git a/frontend/src/types/settings.ts b/frontend/src/types/settings.ts index 2b9357b..127e244 100644 --- a/frontend/src/types/settings.ts +++ b/frontend/src/types/settings.ts @@ -19,4 +19,5 @@ export type SettingsType = { apiKey: string apiChatModelName: string apiCompletionModelName: string + coreApiUrl: string } \ No newline at end of file diff --git a/frontend/src/utils/index.tsx b/frontend/src/utils/index.tsx index 8ae72eb..6f5bfc4 100644 --- a/frontend/src/utils/index.tsx +++ b/frontend/src/utils/index.tsx @@ -303,7 +303,11 @@ export function bytesToReadable(size: number) { else return bytesToGb(size) + ' GB'; } -export function getServerRoot(defaultLocalPort: number) { +export function getServerRoot(defaultLocalPort: number, isCore: boolean = false) { + const coreCustomApiUrl = commonStore.settings.coreApiUrl.trim().replace(/\/$/, ''); + if (isCore && coreCustomApiUrl) + return coreCustomApiUrl; + const defaultRoot = `http://127.0.0.1:${defaultLocalPort}`; if (commonStore.status.status !== ModelStatus.Offline) return defaultRoot;