SettingGeminiModel.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
  3. import {
  4. compareObjects,
  5. API,
  6. showError,
  7. showSuccess,
  8. showWarning, verifyJSON
  9. } from '../../../helpers';
  10. import { useTranslation } from 'react-i18next';
  11. const GEMINI_SETTING_EXAMPLE = {
  12. 'default': 'OFF',
  13. 'HARM_CATEGORY_CIVIC_INTEGRITY': 'BLOCK_NONE',
  14. };
  15. const GEMINI_VERSION_EXAMPLE = {
  16. 'default': 'v1beta',
  17. };
  18. export default function SettingGeminiModel(props) {
  19. const { t } = useTranslation();
  20. const [loading, setLoading] = useState(false);
  21. const [inputs, setInputs] = useState({
  22. 'gemini.safety_settings': '',
  23. 'gemini.version_settings': '',
  24. 'gemini.supported_imagine_models': [],
  25. });
  26. const refForm = useRef();
  27. const [inputsRow, setInputsRow] = useState(inputs);
  28. function onSubmit() {
  29. const updateArray = compareObjects(inputs, inputsRow);
  30. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  31. const requestQueue = updateArray.map((item) => {
  32. let value = '';
  33. if (typeof inputs[item.key] === 'boolean') {
  34. value = String(inputs[item.key]);
  35. } else {
  36. value = inputs[item.key];
  37. }
  38. return API.put('/api/option/', {
  39. key: item.key,
  40. value,
  41. });
  42. });
  43. setLoading(true);
  44. Promise.all(requestQueue)
  45. .then((res) => {
  46. if (requestQueue.length === 1) {
  47. if (res.includes(undefined)) return;
  48. } else if (requestQueue.length > 1) {
  49. if (res.includes(undefined)) return showError(t('部分保存失败,请重试'));
  50. }
  51. showSuccess(t('保存成功'));
  52. props.refresh();
  53. })
  54. .catch(() => {
  55. showError(t('保存失败,请重试'));
  56. })
  57. .finally(() => {
  58. setLoading(false);
  59. });
  60. }
  61. useEffect(() => {
  62. const currentInputs = {};
  63. for (let key in props.options) {
  64. if (Object.keys(inputs).includes(key)) {
  65. currentInputs[key] = props.options[key];
  66. }
  67. }
  68. setInputs(currentInputs);
  69. setInputsRow(structuredClone(currentInputs));
  70. refForm.current.setValues(currentInputs);
  71. }, [props.options]);
  72. return (
  73. <>
  74. <Spin spinning={loading}>
  75. <Form
  76. values={inputs}
  77. getFormApi={(formAPI) => (refForm.current = formAPI)}
  78. style={{ marginBottom: 15 }}
  79. >
  80. <Form.Section text={t('Gemini设置')}>
  81. <Row>
  82. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  83. <Form.TextArea
  84. label={t('Gemini安全设置')}
  85. placeholder={t('为一个 JSON 文本,例如:') + '\n' + JSON.stringify(GEMINI_SETTING_EXAMPLE, null, 2)}
  86. field={'gemini.safety_settings'}
  87. extraText={t('default为默认设置,可单独设置每个分类的安全等级')}
  88. autosize={{ minRows: 6, maxRows: 12 }}
  89. trigger='blur'
  90. stopValidateWithError
  91. rules={[
  92. {
  93. validator: (rule, value) => verifyJSON(value),
  94. message: t('不是合法的 JSON 字符串')
  95. }
  96. ]}
  97. onChange={(value) => setInputs({ ...inputs, 'gemini.safety_settings': value })}
  98. />
  99. </Col>
  100. </Row>
  101. <Row>
  102. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  103. <Form.TextArea
  104. label={t('Gemini版本设置')}
  105. placeholder={t('为一个 JSON 文本,例如:') + '\n' + JSON.stringify(GEMINI_VERSION_EXAMPLE, null, 2)}
  106. field={'gemini.version_settings'}
  107. extraText={t('default为默认设置,可单独设置每个模型的版本')}
  108. autosize={{ minRows: 6, maxRows: 12 }}
  109. trigger='blur'
  110. stopValidateWithError
  111. rules={[
  112. {
  113. validator: (rule, value) => verifyJSON(value),
  114. message: t('不是合法的 JSON 字符串')
  115. }
  116. ]}
  117. onChange={(value) => setInputs({ ...inputs, 'gemini.version_settings': value })}
  118. />
  119. </Col>
  120. </Row>
  121. <Row>
  122. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  123. <Form.TextArea
  124. field={'gemini.supported_imagine_models'}
  125. label={t('支持的图像模型')}
  126. placeholder={t('例如:') + '\n' + JSON.stringify(['gemini-2.0-flash-exp-image-generation'], null, 2)}
  127. onChange={(value) => setInputs({ ...inputs, 'gemini.supported_imagine_models': value })}
  128. />
  129. </Col>
  130. </Row>
  131. <Row>
  132. <Button size='default' onClick={onSubmit}>
  133. {t('保存')}
  134. </Button>
  135. </Row>
  136. </Form.Section>
  137. </Form>
  138. </Spin>
  139. </>
  140. );
  141. }