import IconFont from '@/components/icon-font'; import ModalFooter from '@/components/modal-footer'; import SealAutoComplete from '@/components/seal-form/auto-complete'; import SealInput from '@/components/seal-form/seal-input'; import SealSelect from '@/components/seal-form/seal-select'; import { PageAction } from '@/config'; import { PageActionType } from '@/config/types'; import { useIntl } from '@umijs/max'; import { Form, Modal, Tooltip, Typography } from 'antd'; import _ from 'lodash'; import React, { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import SimpleBar from 'simplebar-react'; import 'simplebar-react/dist/simplebar.min.css'; import { queryGPUList } from '../apis'; import { backendOptionsMap, modelSourceMap, ollamaModelOptions, setSourceRepoConfigValue } from '../config'; import { FormData, GPUListItem, ListItem } from '../config/types'; import AdvanceConfig from './advance-config'; type AddModalProps = { title: string; action: PageActionType; open: boolean; data?: ListItem; onOk: (values: FormData) => void; onCancel: () => void; }; const SEARCH_SOURCE = [ modelSourceMap.huggingface_value, modelSourceMap.modelscope_value ]; const sourceOptions = [ { label: 'Hugging Face', value: modelSourceMap.huggingface_value, key: 'huggingface' }, { label: 'Ollama Library', value: modelSourceMap.ollama_library_value, key: 'ollama_library' }, { label: 'ModelScope', value: modelSourceMap.modelscope_value, key: 'model_scope' }, { label: 'models.form.localPath', value: modelSourceMap.local_path_value, locale: true, key: 'local_path' } ]; const UpdateModal: React.FC = (props) => { const { title, action, open, onOk, onCancel } = props || {}; const [form] = Form.useForm(); const intl = useIntl(); const [gpuOptions, setGpuOptions] = useState([]); const [isGGUF, setIsGGUF] = useState(false); const [loading, setLoading] = useState(false); const localPathCache = useRef(''); const generateCascaderOptions = (list: GPUListItem[]) => { const workerFields = ['worker_name', 'worker_id', 'worker_ip']; const workers = _.groupBy(list, 'worker_name'); const workerList = _.map(workers, (value: GPUListItem[]) => { return { label: `${value[0].worker_name}`, value: value[0].worker_name, parent: true, ..._.pick(value[0], workerFields), children: _.map(value, (item: GPUListItem) => { return { label: `${item.name}`, value: item.id, ..._.omit(item, workerFields) }; }) }; }); return workerList; }; const getGPUList = async () => { const data = await queryGPUList(); const gpuList = generateCascaderOptions(data.items); setGpuOptions(gpuList); }; const generateGPUSelector = (data: any) => { const gpu_ids = _.get(data, 'gpu_selector.gpu_ids', []); if (gpu_ids.length === 0) { return []; } const gpuids: string[][] = []; gpuOptions?.forEach((item) => { item.children?.forEach((child: any) => { if (gpu_ids.includes(child.value)) { gpuids.push([item.value, child.value]); } }); }); return data.backend === backendOptionsMap.voxBox ? gpuids[0] : gpuids; }; useEffect(() => { if (action === PageAction.EDIT && open) { const result = setSourceRepoConfigValue( props.data?.source || '', props.data ); const formData = { ...result.values, ..._.omit(props.data, result.omits), categories: props.data?.categories?.length ? props.data.categories[0] : null, scheduleType: props.data?.gpu_selector ? 'manual' : 'auto', gpu_selector: props.data?.gpu_selector?.gpu_ids.length ? { gpu_ids: generateGPUSelector(props.data) } : [] }; form.setFieldsValue(formData); } }, [open]); useEffect(() => { setIsGGUF(props.data?.backend === backendOptionsMap.llamaBox); }, [props.data?.backend]); const handleSetGPUIds = (backend: string) => { if (backend === backendOptionsMap.llamaBox) { return; } const gpuids = form.getFieldValue(['gpu_selector', 'gpu_ids']); if (!gpuids?.length) { return; } if (gpuids.length > 1 && Array.isArray(gpuids[0])) { form.setFieldValue(['gpu_selector', 'gpu_ids'], [gpuids[0]]); } }; const handleBackendChange = useCallback((val: string) => { if (val === backendOptionsMap.llamaBox) { form.setFieldsValue({ distributed_inference_across_workers: true, cpu_offloading: true }); } form.setFieldValue('backend_version', ''); handleSetGPUIds(val); }, []); const handleOnFocus = () => { localPathCache.current = form.getFieldValue('local_path'); }; const handleLocalPathBlur = (e: any) => { const value = e.target.value; if (value === localPathCache.current && value) { return; } const isEndwithGGUF = _.endsWith(value, '.gguf'); let backend = backendOptionsMap.llamaBox; if (!isEndwithGGUF) { backend = backendOptionsMap.vllm; } handleBackendChange?.(backend); form.setFieldValue('backend', backend); }; const renderHuggingfaceFields = () => { return ( <> name="repo_id" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.repoid' }) } ) } ]} > {isGGUF && ( name="file_name" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.filename' }) } ) } ]} > )} ); }; const renderS3Fields = () => { return ( <> name="s3_address" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.s3address' }) } ) } ]} > ); }; const renderOllamaModelFields = () => { return ( <> name="ollama_library_model_name" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.table.name' }) } ) } ]} > } required > ); }; const renderLocalPathFields = () => { return ( <> name="local_path" key="local_path" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.filePath' }) } ) } ]} > ); }; const renderFieldsBySource = useMemo(() => { if (SEARCH_SOURCE.includes(props.data?.source || '')) { return renderHuggingfaceFields(); } if (props.data?.source === modelSourceMap.ollama_library_value) { return renderOllamaModelFields(); } if (props.data?.source === modelSourceMap.s3_value) { return renderS3Fields(); } if (props.data?.source === modelSourceMap.local_path_value) { return renderLocalPathFields(); } return null; }, [props.data?.source, isGGUF, intl]); const handleSumit = () => { form.submit(); }; const generateGPUIds = (data: FormData) => { const gpu_ids = _.get(data, 'gpu_selector.gpu_ids', []); if (!gpu_ids.length) { return {}; } const result = _.map(gpu_ids, (item: string[][] | string[]) => { if (Array.isArray(item)) { return item[1]; } return item; }); if (result.length) { return { gpu_selector: { gpu_ids: result } }; } return {}; }; const handleOk = (formdata: FormData) => { let obj = {}; if ( [backendOptionsMap.vllm, backendOptionsMap.voxBox].includes( formdata.backend ) ) { obj = { distributed_inference_across_workers: false, cpu_offloading: false }; } if (formdata.scheduleType === 'manual') { const gpuSelector = generateGPUIds(formdata); onOk({ ..._.omit(formdata, ['scheduleType']), categories: formdata.categories ? [formdata.categories] : [], worker_selector: null, gpu_selector: formdata.gpu_selector?.gpu_ids?.length ? { gpu_ids: formdata.gpu_selector.gpu_ids } : null, ...obj, ...gpuSelector }); } else { onOk({ ..._.omit(formdata, ['scheduleType']), categories: formdata.categories ? [formdata.categories] : [], gpu_selector: null, ...obj }); } }; const handleOnClose = () => { onCancel?.(); }; useEffect(() => { getGPUList(); }, []); return ( } >
name="name" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'common.table.name' }) } ) } ]} > name="source" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.select' }, { name: intl.formatMessage({ id: 'models.form.source' }) } ) } ]} > {action === PageAction.EDIT && ( )} {renderFieldsBySource} name="replicas" rules={[ { required: true, message: intl.formatMessage( { id: 'common.form.rule.input' }, { name: intl.formatMessage({ id: 'models.form.replicas' }) } ) } ]} > name="description">
); }; export default memo(UpdateModal);