2024-07-17 19:07:34 +08:00
|
|
|
import { useSetModalState, useShowDeleteConfirm } from '@/hooks/common-hooks';
|
2024-03-15 19:35:59 +08:00
|
|
|
import {
|
|
|
|
|
IApiKeySavingParams,
|
|
|
|
|
ISystemModelSettingSavingParams,
|
2024-04-08 19:13:45 +08:00
|
|
|
useAddLlm,
|
2024-05-10 10:38:39 +08:00
|
|
|
useDeleteLlm,
|
2024-03-15 19:35:59 +08:00
|
|
|
useFetchLlmList,
|
|
|
|
|
useSaveApiKey,
|
|
|
|
|
useSaveTenantInfo,
|
2024-03-18 16:45:01 +08:00
|
|
|
useSelectLlmOptionsByModelType,
|
2024-07-17 19:07:34 +08:00
|
|
|
} from '@/hooks/llm-hooks';
|
|
|
|
|
import { useOneNamespaceEffectsLoading } from '@/hooks/store-hooks';
|
2024-03-15 19:35:59 +08:00
|
|
|
import {
|
|
|
|
|
useFetchTenantInfo,
|
|
|
|
|
useSelectTenantInfo,
|
2024-07-17 19:07:34 +08:00
|
|
|
} from '@/hooks/user-setting-hooks';
|
2024-04-08 19:13:45 +08:00
|
|
|
import { IAddLlmRequestBody } from '@/interfaces/request/llm';
|
2024-03-18 16:45:01 +08:00
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
2024-07-17 09:45:10 +08:00
|
|
|
import { ApiKeyPostBody } from '../interface';
|
2024-03-12 18:58:09 +08:00
|
|
|
|
|
|
|
|
type SavingParamsState = Omit<IApiKeySavingParams, 'api_key'>;
|
|
|
|
|
|
|
|
|
|
export const useSubmitApiKey = () => {
|
|
|
|
|
const [savingParams, setSavingParams] = useState<SavingParamsState>(
|
|
|
|
|
{} as SavingParamsState,
|
|
|
|
|
);
|
|
|
|
|
const saveApiKey = useSaveApiKey();
|
|
|
|
|
const {
|
|
|
|
|
visible: apiKeyVisible,
|
|
|
|
|
hideModal: hideApiKeyModal,
|
|
|
|
|
showModal: showApiKeyModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onApiKeySavingOk = useCallback(
|
2024-07-17 09:45:10 +08:00
|
|
|
async (postBody: ApiKeyPostBody) => {
|
2024-03-29 09:52:19 +08:00
|
|
|
const ret = await saveApiKey({
|
|
|
|
|
...savingParams,
|
2024-07-17 09:45:10 +08:00
|
|
|
...postBody,
|
2024-03-29 09:52:19 +08:00
|
|
|
});
|
2024-03-12 18:58:09 +08:00
|
|
|
|
2024-03-15 19:35:59 +08:00
|
|
|
if (ret === 0) {
|
2024-03-12 18:58:09 +08:00
|
|
|
hideApiKeyModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideApiKeyModal, saveApiKey, savingParams],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onShowApiKeyModal = useCallback(
|
|
|
|
|
(savingParams: SavingParamsState) => {
|
|
|
|
|
setSavingParams(savingParams);
|
|
|
|
|
showApiKeyModal();
|
|
|
|
|
},
|
|
|
|
|
[showApiKeyModal, setSavingParams],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const loading = useOneNamespaceEffectsLoading('settingModel', [
|
|
|
|
|
'set_api_key',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
saveApiKeyLoading: loading,
|
|
|
|
|
initialApiKey: '',
|
2024-03-29 09:52:19 +08:00
|
|
|
llmFactory: savingParams.llm_factory,
|
2024-03-12 18:58:09 +08:00
|
|
|
onApiKeySavingOk,
|
|
|
|
|
apiKeyVisible,
|
|
|
|
|
hideApiKeyModal,
|
|
|
|
|
showApiKeyModal: onShowApiKeyModal,
|
|
|
|
|
};
|
|
|
|
|
};
|
2024-03-15 19:35:59 +08:00
|
|
|
|
|
|
|
|
export const useSubmitSystemModelSetting = () => {
|
|
|
|
|
const systemSetting = useSelectTenantInfo();
|
|
|
|
|
const loading = useOneNamespaceEffectsLoading('settingModel', [
|
|
|
|
|
'set_tenant_info',
|
|
|
|
|
]);
|
|
|
|
|
const saveSystemModelSetting = useSaveTenantInfo();
|
|
|
|
|
const {
|
|
|
|
|
visible: systemSettingVisible,
|
|
|
|
|
hideModal: hideSystemSettingModal,
|
|
|
|
|
showModal: showSystemSettingModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onSystemSettingSavingOk = useCallback(
|
|
|
|
|
async (
|
|
|
|
|
payload: Omit<ISystemModelSettingSavingParams, 'tenant_id' | 'name'>,
|
|
|
|
|
) => {
|
|
|
|
|
const ret = await saveSystemModelSetting({
|
|
|
|
|
tenant_id: systemSetting.tenant_id,
|
|
|
|
|
name: systemSetting.name,
|
|
|
|
|
...payload,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (ret === 0) {
|
|
|
|
|
hideSystemSettingModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideSystemSettingModal, saveSystemModelSetting, systemSetting],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
saveSystemModelSettingLoading: loading,
|
|
|
|
|
onSystemSettingSavingOk,
|
|
|
|
|
systemSettingVisible,
|
|
|
|
|
hideSystemSettingModal,
|
|
|
|
|
showSystemSettingModal,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-18 16:45:01 +08:00
|
|
|
export const useFetchSystemModelSettingOnMount = (visible: boolean) => {
|
2024-03-15 19:35:59 +08:00
|
|
|
const systemSetting = useSelectTenantInfo();
|
2024-03-18 16:45:01 +08:00
|
|
|
const allOptions = useSelectLlmOptionsByModelType();
|
|
|
|
|
const fetchLlmList = useFetchLlmList();
|
|
|
|
|
const fetchTenantInfo = useFetchTenantInfo();
|
2024-03-15 19:35:59 +08:00
|
|
|
|
2024-03-18 16:45:01 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (visible) {
|
|
|
|
|
fetchLlmList();
|
|
|
|
|
fetchTenantInfo();
|
|
|
|
|
}
|
|
|
|
|
}, [fetchLlmList, fetchTenantInfo, visible]);
|
|
|
|
|
|
|
|
|
|
return { systemSetting, allOptions };
|
2024-03-15 19:35:59 +08:00
|
|
|
};
|
2024-03-20 11:13:51 +08:00
|
|
|
|
|
|
|
|
export const useSelectModelProvidersLoading = () => {
|
|
|
|
|
const loading = useOneNamespaceEffectsLoading('settingModel', [
|
|
|
|
|
'my_llm',
|
|
|
|
|
'factories_list',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return loading;
|
|
|
|
|
};
|
2024-04-08 19:13:45 +08:00
|
|
|
|
|
|
|
|
export const useSubmitOllama = () => {
|
|
|
|
|
const loading = useOneNamespaceEffectsLoading('settingModel', ['add_llm']);
|
2024-04-11 18:17:45 +08:00
|
|
|
const [selectedLlmFactory, setSelectedLlmFactory] = useState<string>('');
|
2024-04-08 19:13:45 +08:00
|
|
|
const addLlm = useAddLlm();
|
|
|
|
|
const {
|
|
|
|
|
visible: llmAddingVisible,
|
|
|
|
|
hideModal: hideLlmAddingModal,
|
|
|
|
|
showModal: showLlmAddingModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onLlmAddingOk = useCallback(
|
|
|
|
|
async (payload: IAddLlmRequestBody) => {
|
|
|
|
|
const ret = await addLlm(payload);
|
|
|
|
|
if (ret === 0) {
|
|
|
|
|
hideLlmAddingModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideLlmAddingModal, addLlm],
|
|
|
|
|
);
|
|
|
|
|
|
2024-04-11 18:17:45 +08:00
|
|
|
const handleShowLlmAddingModal = (llmFactory: string) => {
|
|
|
|
|
setSelectedLlmFactory(llmFactory);
|
|
|
|
|
showLlmAddingModal();
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-08 19:13:45 +08:00
|
|
|
return {
|
|
|
|
|
llmAddingLoading: loading,
|
|
|
|
|
onLlmAddingOk,
|
|
|
|
|
llmAddingVisible,
|
|
|
|
|
hideLlmAddingModal,
|
2024-04-11 18:17:45 +08:00
|
|
|
showLlmAddingModal: handleShowLlmAddingModal,
|
|
|
|
|
selectedLlmFactory,
|
2024-04-08 19:13:45 +08:00
|
|
|
};
|
|
|
|
|
};
|
2024-05-10 10:38:39 +08:00
|
|
|
|
2024-05-23 11:15:29 +08:00
|
|
|
export const useSubmitVolcEngine = () => {
|
|
|
|
|
const loading = useOneNamespaceEffectsLoading('settingModel', ['add_llm']);
|
|
|
|
|
const [selectedVolcFactory, setSelectedVolcFactory] = useState<string>('');
|
|
|
|
|
const addLlm = useAddLlm();
|
|
|
|
|
const {
|
|
|
|
|
visible: volcAddingVisible,
|
|
|
|
|
hideModal: hideVolcAddingModal,
|
|
|
|
|
showModal: showVolcAddingModal,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
|
|
|
|
|
const onVolcAddingOk = useCallback(
|
|
|
|
|
async (payload: IAddLlmRequestBody) => {
|
|
|
|
|
const ret = await addLlm(payload);
|
|
|
|
|
if (ret === 0) {
|
|
|
|
|
hideVolcAddingModal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[hideVolcAddingModal, addLlm],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleShowVolcAddingModal = (llmFactory: string) => {
|
|
|
|
|
setSelectedVolcFactory(llmFactory);
|
|
|
|
|
showVolcAddingModal();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
volcAddingLoading: loading,
|
|
|
|
|
onVolcAddingOk,
|
|
|
|
|
volcAddingVisible,
|
|
|
|
|
hideVolcAddingModal,
|
|
|
|
|
showVolcAddingModal: handleShowVolcAddingModal,
|
|
|
|
|
selectedVolcFactory,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-10 10:38:39 +08:00
|
|
|
export const useHandleDeleteLlm = (llmFactory: string) => {
|
|
|
|
|
const deleteLlm = useDeleteLlm();
|
|
|
|
|
const showDeleteConfirm = useShowDeleteConfirm();
|
|
|
|
|
|
|
|
|
|
const handleDeleteLlm = (name: string) => () => {
|
|
|
|
|
showDeleteConfirm({
|
|
|
|
|
onOk: async () => {
|
|
|
|
|
deleteLlm({ llm_factory: llmFactory, llm_name: name });
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { handleDeleteLlm };
|
|
|
|
|
};
|