SettingGeminiModel.js 4.4 KB

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