model-form.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { apiInterceptors, getSupportModels, startModel } from '@/client/api';
  2. import { renderModelIcon } from '@/components/chat/header/model-selector';
  3. import { SupportModel, SupportModelParams } from '@/types/model';
  4. import { Button, Form, Select, Tooltip, message } from 'antd';
  5. import { useEffect, useState } from 'react';
  6. import { useTranslation } from 'react-i18next';
  7. import ModelParams from './model-params';
  8. const { Option } = Select;
  9. function ModelForm({ onCancel, onSuccess }: { onCancel: () => void; onSuccess: () => void }) {
  10. const { t } = useTranslation();
  11. const [models, setModels] = useState<Array<SupportModel> | null>([]);
  12. const [selectedModel, setSelectedModel] = useState<SupportModel>();
  13. const [params, setParams] = useState<Array<SupportModelParams> | null>(null);
  14. const [loading, setLoading] = useState<boolean>(false);
  15. const [form] = Form.useForm();
  16. async function getModels() {
  17. const [, res] = await apiInterceptors(getSupportModels());
  18. if (res && res.length) {
  19. setModels(
  20. res.sort((a: SupportModel, b: SupportModel) => {
  21. if (a.enabled && !b.enabled) {
  22. return -1;
  23. } else if (!a.enabled && b.enabled) {
  24. return 1;
  25. } else {
  26. return a.model.localeCompare(b.model);
  27. }
  28. }),
  29. );
  30. }
  31. setModels(res);
  32. }
  33. useEffect(() => {
  34. getModels();
  35. }, []);
  36. function handleChange(_: string, option: any) {
  37. setSelectedModel(option.model);
  38. setParams(option.model.params);
  39. }
  40. async function onFinish(values: any) {
  41. if (!selectedModel) {
  42. return;
  43. }
  44. delete values.model;
  45. setLoading(true);
  46. const [, , data] = await apiInterceptors(
  47. startModel({
  48. host: selectedModel.host,
  49. port: selectedModel.port,
  50. model: selectedModel.model,
  51. worker_type: selectedModel?.worker_type,
  52. params: values,
  53. }),
  54. );
  55. setLoading(false);
  56. if (data?.success === true) {
  57. onSuccess?.();
  58. return message.success(t('start_model_success'));
  59. }
  60. }
  61. return (
  62. <Form labelCol={{ span: 8 }} wrapperCol={{ span: 16 }} onFinish={onFinish} form={form}>
  63. <Form.Item label='Model' name='model' rules={[{ required: true, message: t('model_select_tips') }]}>
  64. <Select showSearch onChange={handleChange}>
  65. {models?.map(model => (
  66. <Option key={model.model} value={model.model} label={model.model} model={model} disabled={!model.enabled}>
  67. {renderModelIcon(model.model)}
  68. <Tooltip title={model.enabled ? model.model : t('download_model_tip')}>
  69. <span className='ml-2'>{model.model}</span>
  70. </Tooltip>
  71. <Tooltip title={model.enabled ? `${model.host}:${model.port}` : t('download_model_tip')}>
  72. <p className='inline-block absolute right-4'>
  73. <span>{model.host}:</span>
  74. <span>{model.port}</span>
  75. </p>
  76. </Tooltip>
  77. </Option>
  78. ))}
  79. </Select>
  80. </Form.Item>
  81. <ModelParams params={params} form={form} />
  82. <div className='flex justify-center'>
  83. <Button type='primary' htmlType='submit' loading={loading}>
  84. {t('submit')}
  85. </Button>
  86. <Button className='ml-10' onClick={onCancel}>
  87. Cancel
  88. </Button>
  89. </div>
  90. </Form>
  91. );
  92. }
  93. export default ModelForm;