Refactor App.tsx and ConfigStore.ts to handle multiple providers for API base and API key
This commit is contained in:
parent
9ebd041102
commit
f443ade6cd
@ -28,12 +28,31 @@ export default function App() {
|
||||
const getConfig = () => {
|
||||
MessageUtil.registerHandler("readConfig", (data: { value: any }) => {
|
||||
console.log("readConfig registerHandler: ", data);
|
||||
const modelsUrl =
|
||||
data.value?.providers?.devchat?.api_base || "https://api.devchat.ai/v1";
|
||||
axios.get(`${modelsUrl}/models`).then((res) => {
|
||||
|
||||
// 尝试获取 devchat 的 api_base
|
||||
let provider: string = "devchat";
|
||||
let modelsUrl = data.value?.providers?.devchat?.api_base;
|
||||
let devchatApiKey = data.value?.providers?.devchat?.api_key;
|
||||
|
||||
// 如果 devchat 的 api_base 没有设置,尝试获取 openai 的 api_base
|
||||
if (!modelsUrl || !devchatApiKey) {
|
||||
modelsUrl = data.value?.providers?.openai?.api_base;
|
||||
devchatApiKey = data.value?.providers?.openai?.api_key;
|
||||
provider = "openai";
|
||||
}
|
||||
|
||||
// 如果以上两者都没有设置,使用默认链接
|
||||
if (!modelsUrl) {
|
||||
modelsUrl = "https://api.devchat.ai/v1";
|
||||
devchatApiKey = "1234";
|
||||
provider = "devchat";
|
||||
}
|
||||
|
||||
// 添加 header: "Authorization: Bearer ${devchatApiKey}"
|
||||
axios.get(`${modelsUrl}/models`, { headers: { 'Authorization': `Bearer ${devchatApiKey}` }}).then((res) => {
|
||||
// 获取 models 模版列表
|
||||
if (res?.data?.data && Array.isArray(res?.data?.data)) {
|
||||
config.setTemplate(res.data.data);
|
||||
config.setTemplate(res.data.data, provider);
|
||||
config.setConfig(data.value);
|
||||
setReady(true);
|
||||
}
|
||||
|
@ -17,14 +17,14 @@ export const ConfigStore = types
|
||||
defaultModel: types.optional(types.string, ""),
|
||||
})
|
||||
.actions((self) => ({
|
||||
setTemplate: (value: any) => {
|
||||
setTemplate: (value: any, provider: string) => {
|
||||
const models = value
|
||||
.filter((item) => item.category === "chat")
|
||||
.filter((item) => !item.category || item.category === "chat")
|
||||
.map((item) => {
|
||||
return {
|
||||
name: item.model,
|
||||
max_input_tokens: item.max_input_tokens,
|
||||
provider: "devchat",
|
||||
name: item.model ?? item.id,
|
||||
max_input_tokens: item.max_input_tokens ?? 6000,
|
||||
provider: provider,
|
||||
stream: true,
|
||||
};
|
||||
});
|
||||
@ -89,6 +89,7 @@ export const ConfigStore = types
|
||||
...item,
|
||||
};
|
||||
delete currentModel.name;
|
||||
|
||||
if (!newConfig.models[item.name]) {
|
||||
newConfig.models[item.name] = {
|
||||
...currentModel,
|
||||
@ -99,10 +100,9 @@ export const ConfigStore = types
|
||||
...newConfig.models[item.name],
|
||||
};
|
||||
}
|
||||
if (
|
||||
newConfig.models[item.name].provider !== "devchat" &&
|
||||
newConfig.models[item.name].provider !== "openai"
|
||||
) {
|
||||
|
||||
if (newConfig.models[item.name].provider !== currentModel.provider) {
|
||||
needUpdate = true;
|
||||
newConfig.models[item.name].provider = currentModel.provider;
|
||||
}
|
||||
// 只有之前配置过 openai 的,provider 才可以是 openai
|
||||
|
Loading…
x
Reference in New Issue
Block a user