From bae9ae6551ed88a973ae314a1043f4cdc5116be7 Mon Sep 17 00:00:00 2001 From: josc146 Date: Tue, 20 Jun 2023 23:24:51 +0800 Subject: [PATCH] allow custom api url, key, model --- frontend/src/_locales/zh-hans/main.json | 4 +- frontend/src/pages/Chat.tsx | 9 +- frontend/src/pages/Completion.tsx | 9 +- frontend/src/pages/Settings.tsx | 107 +++++++++++++++++++++++- frontend/src/stores/commonStore.ts | 11 ++- 5 files changed, 130 insertions(+), 10 deletions(-) diff --git a/frontend/src/_locales/zh-hans/main.json b/frontend/src/_locales/zh-hans/main.json index 7fb5399..0d76584 100644 --- a/frontend/src/_locales/zh-hans/main.json +++ b/frontend/src/_locales/zh-hans/main.json @@ -151,5 +151,7 @@ "Open": "打开", "DPI Scaling": "显示缩放", "Restart the app to apply DPI Scaling.": "重启应用以使显示缩放生效", - "Restart": "重启" + "Restart": "重启", + "API Chat Model Name": "API聊天模型名", + "API Completion Model Name": "API补全模型名" } \ No newline at end of file diff --git a/frontend/src/pages/Chat.tsx b/frontend/src/pages/Chat.tsx index 9e4239c..d9aabd5 100644 --- a/frontend/src/pages/Chat.tsx +++ b/frontend/src/pages/Chat.tsx @@ -154,17 +154,20 @@ const ChatPanel: FC = observer(() => { setTimeout(scrollToBottom); let answer = ''; chatSseController = new AbortController(); - fetchEventSource(`http://127.0.0.1:${port}/chat/completions`, // https://api.openai.com/v1/chat/completions || http://127.0.0.1:${port}/chat/completions + fetchEventSource( // https://api.openai.com/v1/chat/completions || http://127.0.0.1:${port}/chat/completions + commonStore.settings.apiUrl ? + commonStore.settings.apiUrl + '/v1/chat/completions' : + `http://127.0.0.1:${port}/chat/completions`, { method: 'POST', headers: { 'Content-Type': 'application/json', - Authorization: `Bearer sk-` + Authorization: `Bearer ${commonStore.settings.apiKey}` }, body: JSON.stringify({ messages, stream: true, - model: 'gpt-3.5-turbo' + model: commonStore.settings.apiChatModelName // 'gpt-3.5-turbo' }), signal: chatSseController?.signal, onmessage(e) { diff --git a/frontend/src/pages/Completion.tsx b/frontend/src/pages/Completion.tsx index 6cc8e14..ce48fc9 100644 --- a/frontend/src/pages/Completion.tsx +++ b/frontend/src/pages/Completion.tsx @@ -190,17 +190,20 @@ const CompletionPanel: FC = observer(() => { let answer = ''; completionSseController = new AbortController(); - fetchEventSource(`http://127.0.0.1:${port}/completions`, // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/completions + fetchEventSource( // https://api.openai.com/v1/completions || http://127.0.0.1:${port}/completions + commonStore.settings.apiUrl ? + commonStore.settings.apiUrl + '/v1/completions' : + `http://127.0.0.1:${port}/completions`, { method: 'POST', headers: { 'Content-Type': 'application/json', - Authorization: `Bearer sk-` + Authorization: `Bearer ${commonStore.settings.apiKey}` }, body: JSON.stringify({ prompt, stream: true, - model: 'text-davinci-003', + model: commonStore.settings.apiCompletionModelName, // 'text-davinci-003' max_tokens: params.maxResponseToken, temperature: params.temperature, top_p: params.topP, diff --git a/frontend/src/pages/Settings.tsx b/frontend/src/pages/Settings.tsx index 4685c68..0345ce3 100644 --- a/frontend/src/pages/Settings.tsx +++ b/frontend/src/pages/Settings.tsx @@ -34,6 +34,10 @@ export type SettingsType = { dpiScaling: number customModelsPath: string customPythonPath: string + apiUrl: string + apiKey: string + apiChatModelName: string + apiCompletionModelName: string } export const Settings: FC = observer(() => { @@ -139,8 +143,11 @@ export const Settings: FC = observer(() => { }); }} /> } /> - - + { + if (data.value === 'advanced') + commonStore.setAdvancedCollapsed(!commonStore.advancedCollapsed); + }}> + {t('Advanced')}
@@ -164,6 +171,102 @@ export const Settings: FC = observer(() => { }); }} /> } /> + + { + commonStore.setSettings({ + apiUrl: data.value + }); + }} /> + { + commonStore.setSettings({ + apiUrl: data.optionValue + }); + if (data.optionText === 'OpenAI') { + if (commonStore.settings.apiChatModelName === 'rwkv') + commonStore.setSettings({ + apiChatModelName: 'gpt-3.5-turbo' + }); + if (commonStore.settings.apiCompletionModelName === 'rwkv') + commonStore.setSettings({ + apiCompletionModelName: 'text-davinci-003' + }); + } + }}> + + + +
+ } /> + { + commonStore.setSettings({ + apiKey: data.value + }); + }} /> + } /> + + { + commonStore.setSettings({ + apiChatModelName: data.value + }); + }} /> + { + if (data.optionValue) { + commonStore.setSettings({ + apiChatModelName: data.optionValue + }); + } + }}> + { + ['rwkv', 'gpt-4', 'gpt-4-0613', 'gpt-4-32k', 'gpt-4-32k-0613', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-16k-0613'] + .map((v, i) => + + ) + } + + + } /> + + { + commonStore.setSettings({ + apiCompletionModelName: data.value + }); + }} /> + { + if (data.optionValue) { + commonStore.setSettings({ + apiCompletionModelName: data.optionValue + }); + } + }}> + { + ['rwkv', 'text-davinci-003', 'text-davinci-002', 'text-curie-001', 'text-babbage-001', 'text-ada-001'] + .map((v, i) => + + ) + } + + + } />
diff --git a/frontend/src/stores/commonStore.ts b/frontend/src/stores/commonStore.ts index d73d048..f093ac5 100644 --- a/frontend/src/stores/commonStore.ts +++ b/frontend/src/stores/commonStore.ts @@ -56,6 +56,7 @@ class CommonStore { // downloads downloadList: DownloadStatus[] = []; // settings + advancedCollapsed: boolean = true; settings: SettingsType = { language: getUserLanguage(), darkMode: !isSystemLightMode(), @@ -65,7 +66,11 @@ class CommonStore { host: '127.0.0.1', dpiScaling: 100, customModelsPath: './models', - customPythonPath: '' + customPythonPath: '', + apiUrl: '', + apiKey: 'sk-', + apiChatModelName: 'rwkv', + apiCompletionModelName: 'rwkv' }; // about about: AboutContent = manifest.about; @@ -188,6 +193,10 @@ class CommonStore { setCurrentInput(value: string) { this.currentInput = value; } + + setAdvancedCollapsed(value: boolean) { + this.advancedCollapsed = value; + } } export default new CommonStore(); \ No newline at end of file