SettingsMagnification.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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,
  9. verifyJSON,
  10. } from '../../../helpers';
  11. export default function SettingsMagnification(props) {
  12. const [loading, setLoading] = useState(false);
  13. const [inputs, setInputs] = useState({
  14. ModelPrice: '',
  15. ModelRatio: '',
  16. CompletionRatio: '',
  17. GroupRatio: '',
  18. });
  19. const refForm = useRef();
  20. const [inputsRow, setInputsRow] = useState(inputs);
  21. async function onSubmit() {
  22. try {
  23. await refForm.current.validate();
  24. const updateArray = compareObjects(inputs, inputsRow);
  25. if (!updateArray.length) return showWarning('你似乎并没有修改什么');
  26. const requestQueue = updateArray.map((item) => {
  27. let value = '';
  28. if (typeof inputs[item.key] === 'boolean') {
  29. value = String(inputs[item.key]);
  30. } else {
  31. value = inputs[item.key];
  32. }
  33. return API.put('/api/option/', {
  34. key: item.key,
  35. value,
  36. });
  37. });
  38. setLoading(true);
  39. Promise.all(requestQueue)
  40. .then((res) => {
  41. if (requestQueue.length === 1) {
  42. if (res.includes(undefined)) return;
  43. } else if (requestQueue.length > 1) {
  44. if (res.includes(undefined)) return showError('部分更新失败');
  45. }
  46. showSuccess('更新成功');
  47. })
  48. .catch(() => {
  49. showError('更新失败');
  50. })
  51. .finally(() => {
  52. setLoading(false);
  53. props.refresh();
  54. });
  55. } catch (error) {
  56. showError('请检查输入');
  57. console.error(error);
  58. } finally {
  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={'倍率设置'}>
  81. <Row gutter={16}>
  82. <Col span={16}>
  83. <Form.TextArea
  84. label={'模型固定价格'}
  85. extraText={'一次调用消耗多少刀,优先级大于模型倍率'}
  86. placeholder={
  87. '为一个 JSON 文本,键为模型名称,值为一次调用消耗多少刀,比如 "gpt-4-gizmo-*": 0.1,一次消耗0.1刀'
  88. }
  89. field={'ModelPrice'}
  90. autosize={{ minRows: 6, maxRows: 12 }}
  91. trigger='blur'
  92. rules={[
  93. {
  94. validator: (rule, value) => verifyJSON(value),
  95. message: '不是合法的 JSON 字符串',
  96. },
  97. ]}
  98. onChange={(value) =>
  99. setInputs({
  100. ...inputs,
  101. ModelPrice: value,
  102. })
  103. }
  104. />
  105. </Col>
  106. </Row>
  107. <Row gutter={16}>
  108. <Col span={16}>
  109. <Form.TextArea
  110. label={'模型倍率'}
  111. extraText={''}
  112. placeholder={'为一个 JSON 文本,键为模型名称,值为倍率'}
  113. field={'ModelRatio'}
  114. autosize={{ minRows: 6, maxRows: 12 }}
  115. trigger='blur'
  116. rules={[
  117. {
  118. validator: (rule, value) => verifyJSON(value),
  119. message: '不是合法的 JSON 字符串',
  120. },
  121. ]}
  122. onChange={(value) =>
  123. setInputs({
  124. ...inputs,
  125. ModelRatio: value,
  126. })
  127. }
  128. />
  129. </Col>
  130. </Row>
  131. <Row gutter={16}>
  132. <Col span={16}>
  133. <Form.TextArea
  134. label={'模型补全倍率'}
  135. extraText={'仅对自定义模型有效'}
  136. placeholder={'为一个 JSON 文本,键为模型名称,值为倍率'}
  137. field={'CompletionRatio'}
  138. autosize={{ minRows: 6, maxRows: 12 }}
  139. trigger='blur'
  140. rules={[
  141. {
  142. validator: (rule, value) => verifyJSON(value),
  143. message: '不是合法的 JSON 字符串',
  144. },
  145. ]}
  146. onChange={(value) =>
  147. setInputs({
  148. ...inputs,
  149. CompletionRatio: value,
  150. })
  151. }
  152. />
  153. </Col>
  154. </Row>
  155. <Row gutter={16}>
  156. <Col span={16}>
  157. <Form.TextArea
  158. label={'分组倍率'}
  159. extraText={''}
  160. placeholder={'为一个 JSON 文本,键为分组名称,值为倍率'}
  161. field={'GroupRatio'}
  162. autosize={{ minRows: 6, maxRows: 12 }}
  163. trigger='blur'
  164. rules={[
  165. {
  166. validator: (rule, value) => verifyJSON(value),
  167. message: '不是合法的 JSON 字符串',
  168. },
  169. ]}
  170. onChange={(value) =>
  171. setInputs({
  172. ...inputs,
  173. GroupRatio: value,
  174. })
  175. }
  176. />
  177. </Col>
  178. </Row>
  179. <Row>
  180. <Button size='large' onClick={onSubmit}>
  181. 保存倍率设置
  182. </Button>
  183. </Row>
  184. </Form.Section>
  185. </Form>
  186. </Spin>
  187. </>
  188. );
  189. }