You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { IModalManagerChildrenProps } from '@/components/modal-manager';
  2. import { LLMFactory } from '@/constants/llm';
  3. import { useTranslate } from '@/hooks/common-hooks';
  4. import { Form, Input, Modal } from 'antd';
  5. import { useEffect } from 'react';
  6. import { ApiKeyPostBody } from '../../interface';
  7. interface IProps extends Omit<IModalManagerChildrenProps, 'showModal'> {
  8. loading: boolean;
  9. initialValue: string;
  10. llmFactory: string;
  11. editMode?: boolean;
  12. onOk: (postBody: ApiKeyPostBody) => void;
  13. showModal?(): void;
  14. }
  15. type FieldType = {
  16. api_key?: string;
  17. base_url?: string;
  18. group_id?: string;
  19. };
  20. const modelsWithBaseUrl = [LLMFactory.OpenAI, LLMFactory.AzureOpenAI];
  21. const ApiKeyModal = ({
  22. visible,
  23. hideModal,
  24. llmFactory,
  25. loading,
  26. initialValue,
  27. editMode = false,
  28. onOk,
  29. }: IProps) => {
  30. const [form] = Form.useForm();
  31. const { t } = useTranslate('setting');
  32. const handleOk = async () => {
  33. const ret = await form.validateFields();
  34. return onOk(ret);
  35. };
  36. const handleKeyDown = async (e) => {
  37. if (e.key === 'Enter') {
  38. await handleOk();
  39. }
  40. };
  41. useEffect(() => {
  42. if (visible) {
  43. form.setFieldValue('api_key', initialValue);
  44. }
  45. }, [initialValue, form, visible]);
  46. return (
  47. <Modal
  48. title={editMode ? t('editModel') : t('modify')}
  49. open={visible}
  50. onOk={handleOk}
  51. onCancel={hideModal}
  52. okButtonProps={{ loading }}
  53. confirmLoading={loading}
  54. >
  55. <Form
  56. name="basic"
  57. labelCol={{ span: 6 }}
  58. wrapperCol={{ span: 18 }}
  59. style={{ maxWidth: 600 }}
  60. autoComplete="off"
  61. form={form}
  62. >
  63. <Form.Item<FieldType>
  64. label={t('apiKey')}
  65. name="api_key"
  66. tooltip={t('apiKeyTip')}
  67. rules={[{ required: true, message: t('apiKeyMessage') }]}
  68. >
  69. <Input onKeyDown={handleKeyDown} />
  70. </Form.Item>
  71. {modelsWithBaseUrl.some((x) => x === llmFactory) && (
  72. <Form.Item<FieldType>
  73. label={t('baseUrl')}
  74. name="base_url"
  75. tooltip={t('baseUrlTip')}
  76. >
  77. <Input
  78. placeholder="https://api.openai.com/v1"
  79. onKeyDown={handleKeyDown}
  80. />
  81. </Form.Item>
  82. )}
  83. {llmFactory?.toLowerCase() === 'Minimax'.toLowerCase() && (
  84. <Form.Item<FieldType> label={'Group ID'} name="group_id">
  85. <Input />
  86. </Form.Item>
  87. )}
  88. </Form>
  89. </Modal>
  90. );
  91. };
  92. export default ApiKeyModal;