import { apiInterceptors, getSupportModels, startModel } from '@/client/api'; import { renderModelIcon } from '@/components/chat/header/model-selector'; import { SupportModel, SupportModelParams } from '@/types/model'; import { Button, Form, Select, Tooltip, message } from 'antd'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import ModelParams from './model-params'; const { Option } = Select; function ModelForm({ onCancel, onSuccess }: { onCancel: () => void; onSuccess: () => void }) { const { t } = useTranslation(); const [models, setModels] = useState | null>([]); const [selectedModel, setSelectedModel] = useState(); const [params, setParams] = useState | null>(null); const [loading, setLoading] = useState(false); const [form] = Form.useForm(); async function getModels() { const [, res] = await apiInterceptors(getSupportModels()); if (res && res.length) { setModels( res.sort((a: SupportModel, b: SupportModel) => { if (a.enabled && !b.enabled) { return -1; } else if (!a.enabled && b.enabled) { return 1; } else { return a.model.localeCompare(b.model); } }), ); } setModels(res); } useEffect(() => { getModels(); }, []); function handleChange(_: string, option: any) { setSelectedModel(option.model); setParams(option.model.params); } async function onFinish(values: any) { if (!selectedModel) { return; } delete values.model; setLoading(true); const [, , data] = await apiInterceptors( startModel({ host: selectedModel.host, port: selectedModel.port, model: selectedModel.model, worker_type: selectedModel?.worker_type, params: values, }), ); setLoading(false); if (data?.success === true) { onSuccess?.(); return message.success(t('start_model_success')); } } return (
); } export default ModelForm;