File size: 5,577 Bytes
e3278e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
import {
Button as Button2,
Modal,
Form,
Select as Select2,
InputNumber,
message,
} from "antd";
import {
TextInput,
Button,
} from "@tremor/react";
import { organizationCreateCall } from "../networking";
// types.ts
export interface FormData {
name: string;
models: string[];
maxBudget: number | null;
budgetDuration: string | null;
tpmLimit: number | null;
rpmLimit: number | null;
}
export interface OrganizationFormProps {
title?: string;
onCancel?: () => void;
accessToken: string | null;
availableModels?: string[];
initialValues?: Partial<FormData>;
submitButtonText?: string;
modelSelectionType?: 'single' | 'multiple';
}
// OrganizationForm.tsx
import React, { useState } from 'react';
const onSubmit = async (formValues: Record<string, any>, accessToken: string | null, setIsModalVisible: any) => {
if (accessToken == null) {
return;
}
try {
message.info("Creating Organization");
console.log("formValues: " + JSON.stringify(formValues));
const response: any = await organizationCreateCall(accessToken, formValues);
console.log(`response for organization create call: ${response}`);
message.success("Organization created");
sessionStorage.removeItem('organizations');
setIsModalVisible(false);
} catch (error) {
console.error("Error creating the organization:", error);
message.error("Error creating the organization: " + error, 20);
}
}
const OrganizationForm: React.FC<OrganizationFormProps> = ({
title = "Create Organization",
onCancel,
accessToken,
availableModels = [],
initialValues = {},
submitButtonText = "Create",
modelSelectionType = "multiple",
}) => {
const [form] = Form.useForm();
const [isModalVisible, setIsModalVisible] = useState<boolean>(false);
const [formData, setFormData] = useState<FormData>({
name: initialValues.name || '',
models: initialValues.models || [],
maxBudget: initialValues.maxBudget || null,
budgetDuration: initialValues.budgetDuration || null,
tpmLimit: initialValues.tpmLimit || null,
rpmLimit: initialValues.rpmLimit || null
});
console.log(`availableModels: ${availableModels}`)
const handleSubmit = async (formValues: Record<string, any>) => {
if (accessToken == null) {
return;
}
await onSubmit(formValues, accessToken, setIsModalVisible);
setIsModalVisible(false);
};
const handleCancel = (): void => {
setIsModalVisible(false);
if (onCancel) onCancel();
};
return (
<div className="w-full">
<Button
onClick={() => setIsModalVisible(true)}
className="mx-auto"
type="button"
>
+ Create New {title}
</Button>
<Modal
title={`Create ${title}`}
visible={isModalVisible}
width={800}
footer={null}
onCancel={handleCancel}
>
<Form
form={form}
onFinish={handleSubmit}
labelCol={{ span: 8 }}
wrapperCol={{ span: 16 }}
labelAlign="left"
>
<>
<Form.Item
label={`${title} Name`}
name="organization_alias"
rules={[
{ required: true, message: `Please input a ${title} name` },
]}
>
<TextInput placeholder="" />
</Form.Item>
<Form.Item label="Models" name="models">
<Select2
mode="multiple"
placeholder="Select models"
style={{ width: "100%" }}
>
<Select2.Option
key="all-proxy-models"
value="all-proxy-models"
>
All Proxy Models
</Select2.Option>
{availableModels.map((model) => (
<Select2.Option key={model} value={model}>
{model}
</Select2.Option>
))}
</Select2>
</Form.Item>
<Form.Item label="Max Budget (USD)" name="max_budget">
<InputNumber step={0.01} precision={2} width={200} />
</Form.Item>
<Form.Item
className="mt-8"
label="Reset Budget"
name="budget_duration"
>
<Select2 defaultValue={null} placeholder="n/a">
<Select2.Option value="24h">daily</Select2.Option>
<Select2.Option value="7d">weekly</Select2.Option>
<Select2.Option value="30d">monthly</Select2.Option>
</Select2>
</Form.Item>
<Form.Item
label="Tokens per minute Limit (TPM)"
name="tpm_limit"
>
<InputNumber step={1} width={400} />
</Form.Item>
<Form.Item
label="Requests per minute Limit (RPM)"
name="rpm_limit"
>
<InputNumber step={1} width={400} />
</Form.Item>
</>
<div style={{ textAlign: "right", marginTop: "10px" }}>
<Button2 htmlType="submit">{submitButtonText}</Button2>
</div>
</Form>
</Modal>
</div>
);
};
export default OrganizationForm; |