|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { PlusCircleOutlined } from '@ant-design/icons'; |
| 3 | +import { Alert, Button, Form, Input, Modal, notification, Radio, Select } from 'antd'; |
| 4 | +import type { CheckboxGroupProps } from 'antd/es/checkbox'; |
| 5 | +import get from 'lodash/get'; |
| 6 | +import isEqual from 'lodash/isEqual'; |
| 7 | +import { useMutation } from "@tanstack/react-query"; |
| 8 | +import { addModelProvider, useGetModelProvider } from './hooks'; |
| 9 | +import Loading from '../Evaluator/Loading'; |
| 10 | +import { CustomModel } from './SettingsPage'; |
| 11 | +import isEmpty from 'lodash/isEmpty'; |
| 12 | + |
| 13 | +export enum ModelProviderType { |
| 14 | + OPENAI = 'openai', |
| 15 | + GEMINIE = 'gemini', |
| 16 | + CAII = 'caii' |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +const modelProviderTypeOptions: CheckboxGroupProps<string>['options'] = [ |
| 21 | + { label: 'OpenAI', value: 'openai' }, |
| 22 | + // { label: 'CAII', value: 'caii' }, |
| 23 | + { label: 'Gemini', value: 'gemini' }, |
| 24 | +]; |
| 25 | + |
| 26 | +const OPENAI_MODELS = [ |
| 27 | + "gpt-4.1", // Latest GPT-4.1 series (April 2025) |
| 28 | + "gpt-4.1-mini", |
| 29 | + "gpt-4.1-nano", |
| 30 | + "o3", // Latest reasoning models (April 2025) |
| 31 | + "o4-mini", |
| 32 | + "o3-mini", // January 2025 |
| 33 | + "o1", // December 2024 |
| 34 | + "gpt-4o", // November 2024 |
| 35 | + "gpt-4o-mini", // July 2024 |
| 36 | + "gpt-4-turbo", // April 2024 |
| 37 | + "gpt-3.5-turbo" // Legacy but still widely used |
| 38 | +]; |
| 39 | + |
| 40 | +const OPENAI_MODELS_OPTIONS = OPENAI_MODELS.map((model: string) => ({ |
| 41 | + label: model, |
| 42 | + value: model |
| 43 | +})); |
| 44 | + |
| 45 | +const GEMINI_MODELS = [ |
| 46 | + "gemini-2.5-pro", // June 2025 - most powerful thinking model |
| 47 | + "gemini-2.5-flash", // June 2025 - best price-performance |
| 48 | + "gemini-2.5-flash-lite", // June 2025 - cost-efficient |
| 49 | + "gemini-2.0-flash", // February 2025 - next-gen features |
| 50 | + "gemini-2.0-flash-lite", // February 2025 - low latency |
| 51 | + "gemini-1.5-pro", // September 2024 - complex reasoning |
| 52 | + "gemini-1.5-flash", // September 2024 - fast & versatile |
| 53 | + "gemini-1.5-flash-8b" // October 2024 - lightweight |
| 54 | +]; |
| 55 | + |
| 56 | +const GEMINI_MODELS_OPTIONS = GEMINI_MODELS.map((model: string) => ({ |
| 57 | + label: model, |
| 58 | + value: model |
| 59 | +})); |
| 60 | + |
| 61 | +interface Props { |
| 62 | + refetch: () => void; |
| 63 | + onClose: () => void; |
| 64 | + model: CustomModel; |
| 65 | +} |
| 66 | + |
| 67 | +const EditModelProvider: React.FC<Props> = ({ model, refetch, onClose }) => { |
| 68 | + const [form] = Form.useForm(); |
| 69 | + const modelProviderReq = useGetModelProvider(model.endpoint_id); |
| 70 | + console.log('modelProviderReq', modelProviderReq); |
| 71 | + const [models, setModels] = useState(OPENAI_MODELS_OPTIONS); |
| 72 | + const mutation = useMutation({ |
| 73 | + mutationFn: addModelProvider |
| 74 | + }); |
| 75 | + |
| 76 | + useEffect(() => { |
| 77 | + if (!isEmpty(modelProviderReq.data)) { |
| 78 | + console.log('-------->', modelProviderReq.data); |
| 79 | + const endpoint = get(modelProviderReq, 'data.endpoint'); |
| 80 | + form.setFieldsValue({ |
| 81 | + ...endpoint |
| 82 | + }); |
| 83 | + } |
| 84 | + }, [modelProviderReq.data]); |
| 85 | + |
| 86 | + |
| 87 | + useEffect(() => { |
| 88 | + if (mutation.isError) { |
| 89 | + notification.error({ |
| 90 | + message: 'Error', |
| 91 | + description: `An error occurred while fetching the model.\n ${mutation.error}` |
| 92 | + }); |
| 93 | + } |
| 94 | + if (mutation.isSuccess) { |
| 95 | + notification.success({ |
| 96 | + message: 'Success', |
| 97 | + description: `THe model provider has been edited successfully!.` |
| 98 | + }); |
| 99 | + refetch(); |
| 100 | + } |
| 101 | + }, [mutation.error, mutation.isSuccess]); |
| 102 | + |
| 103 | + const onCancel = () => { |
| 104 | + form.resetFields(); |
| 105 | + onClose(); |
| 106 | + } |
| 107 | + |
| 108 | + const onSubmit = async () => { |
| 109 | + try { |
| 110 | + await form.validateFields(); |
| 111 | + const values = form.getFieldsValue(); |
| 112 | + console.log('values', values); |
| 113 | + mutation.mutate({ |
| 114 | + endpoint_config: { |
| 115 | + display_name: values.display_name, |
| 116 | + endpoint_id: values.endpoint_id, |
| 117 | + model_id: values.model_id, |
| 118 | + provider_type: values.provider_type, |
| 119 | + api_key: values.api_key, |
| 120 | + endpoint_url: values.endpoint_url |
| 121 | + } |
| 122 | + }); |
| 123 | + } catch (error) { |
| 124 | + console.error(error); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + |
| 129 | + const initialValues = { |
| 130 | + provider_type: 'openai' |
| 131 | + }; |
| 132 | + |
| 133 | + const onChange = (e: any) => { |
| 134 | + console.log('onChange', e); |
| 135 | + const value = get(e, 'target.value'); |
| 136 | + console.log('value:', value); |
| 137 | + if (value === 'openai' && !isEqual(OPENAI_MODELS_OPTIONS, models)) { |
| 138 | + setModels(OPENAI_MODELS_OPTIONS); |
| 139 | + } else if (value === 'gemini' && !isEqual(GEMINI_MODELS_OPTIONS, models)) { |
| 140 | + setModels(GEMINI_MODELS_OPTIONS); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return ( |
| 145 | + <> |
| 146 | + <Modal |
| 147 | + visible |
| 148 | + okText={`Edit`} |
| 149 | + title={`Edit Model Provider`} |
| 150 | + onCancel={onCancel} |
| 151 | + onOk={onSubmit} |
| 152 | + width={800} |
| 153 | + > |
| 154 | + <Form form={form} layout="vertical" initialValues={initialValues}> |
| 155 | + <br /> |
| 156 | + <br /> |
| 157 | + {(mutation.isPending || modelProviderReq.isLoading) && <Loading />} |
| 158 | + {mutation.error && ( |
| 159 | + <Alert |
| 160 | + type="error" |
| 161 | + message="Error Occurred" |
| 162 | + description={ |
| 163 | + <div>{mutation.error instanceof Error ? mutation.error.message : String(mutation.error)}</div> |
| 164 | + } |
| 165 | + /> |
| 166 | + )} |
| 167 | + <Form.Item name="provider_type"> |
| 168 | + <Radio.Group |
| 169 | + block |
| 170 | + options={modelProviderTypeOptions} |
| 171 | + defaultValue="openai" |
| 172 | + optionType="button" |
| 173 | + buttonStyle="solid" |
| 174 | + style={{ width: '40%' }} |
| 175 | + onChange={onChange} |
| 176 | + /> |
| 177 | + </Form.Item> |
| 178 | + <Form.Item |
| 179 | + name="display_name" |
| 180 | + label="Display Name" |
| 181 | + rules={[ |
| 182 | + { |
| 183 | + required: true, |
| 184 | + message: 'This field is required.' |
| 185 | + } |
| 186 | + ]}> |
| 187 | + <Input /> |
| 188 | + </Form.Item> |
| 189 | + <Form.Item |
| 190 | + name="endpoint_id" |
| 191 | + label="Endpoint ID" |
| 192 | + rules={[ |
| 193 | + { |
| 194 | + required: true, |
| 195 | + message: 'This field is required.' |
| 196 | + } |
| 197 | + ]}> |
| 198 | + <Input /> |
| 199 | + </Form.Item> |
| 200 | + <Form.Item |
| 201 | + name="model_id" |
| 202 | + label="Model" |
| 203 | + rules={[ |
| 204 | + { |
| 205 | + required: true, |
| 206 | + message: 'This field is required.' |
| 207 | + } |
| 208 | + ]}> |
| 209 | + <Select options={models} /> |
| 210 | + </Form.Item> |
| 211 | + <Form.Item |
| 212 | + name="endpoint_url" |
| 213 | + label="Endpoint URL" |
| 214 | + rules={[ |
| 215 | + { |
| 216 | + required: true, |
| 217 | + message: 'This field is required.' |
| 218 | + } |
| 219 | + ]}> |
| 220 | + <Input /> |
| 221 | + </Form.Item> |
| 222 | + <Form.Item |
| 223 | + name="api_key" |
| 224 | + label="API Key" |
| 225 | + rules={[ |
| 226 | + { |
| 227 | + required: true, |
| 228 | + message: 'This field is required.' |
| 229 | + } |
| 230 | + ]}> |
| 231 | + <Input.Password /> |
| 232 | + </Form.Item> |
| 233 | + </Form> |
| 234 | + </Modal> |
| 235 | + </> |
| 236 | + ); |
| 237 | +} |
| 238 | + |
| 239 | +export default EditModelProvider; |
| 240 | + |
0 commit comments