SettingsRequestRateLimit.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact [email protected]
  14. */
  15. import React, { useEffect, useState, useRef } from 'react';
  16. import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
  17. import {
  18. compareObjects,
  19. API,
  20. showError,
  21. showSuccess,
  22. showWarning,
  23. verifyJSON,
  24. } from '../../../helpers';
  25. import { useTranslation } from 'react-i18next';
  26. export default function RequestRateLimit(props) {
  27. const { t } = useTranslation();
  28. const [loading, setLoading] = useState(false);
  29. const [inputs, setInputs] = useState({
  30. ModelRequestRateLimitEnabled: false,
  31. ModelRequestRateLimitCount: -1,
  32. ModelRequestRateLimitSuccessCount: 1000,
  33. ModelRequestRateLimitDurationMinutes: 1,
  34. ModelRequestRateLimitGroup: '',
  35. });
  36. const refForm = useRef();
  37. const [inputsRow, setInputsRow] = useState(inputs);
  38. function onSubmit() {
  39. const updateArray = compareObjects(inputs, inputsRow);
  40. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  41. const requestQueue = updateArray.map((item) => {
  42. let value = '';
  43. if (typeof inputs[item.key] === 'boolean') {
  44. value = String(inputs[item.key]);
  45. } else {
  46. value = inputs[item.key];
  47. }
  48. return API.put('/api/option/', {
  49. key: item.key,
  50. value,
  51. });
  52. });
  53. setLoading(true);
  54. Promise.all(requestQueue)
  55. .then((res) => {
  56. if (requestQueue.length === 1) {
  57. if (res.includes(undefined)) return;
  58. } else if (requestQueue.length > 1) {
  59. if (res.includes(undefined))
  60. return showError(t('部分保存失败,请重试'));
  61. }
  62. for (let i = 0; i < res.length; i++) {
  63. if (!res[i].data.success) {
  64. return showError(res[i].data.message);
  65. }
  66. }
  67. showSuccess(t('保存成功'));
  68. props.refresh();
  69. })
  70. .catch(() => {
  71. showError(t('保存失败,请重试'));
  72. })
  73. .finally(() => {
  74. setLoading(false);
  75. });
  76. }
  77. useEffect(() => {
  78. const currentInputs = {};
  79. for (let key in props.options) {
  80. if (Object.keys(inputs).includes(key)) {
  81. currentInputs[key] = props.options[key];
  82. }
  83. }
  84. setInputs(currentInputs);
  85. setInputsRow(structuredClone(currentInputs));
  86. refForm.current.setValues(currentInputs);
  87. }, [props.options]);
  88. return (
  89. <>
  90. <Spin spinning={loading}>
  91. <Form
  92. values={inputs}
  93. getFormApi={(formAPI) => (refForm.current = formAPI)}
  94. style={{ marginBottom: 15 }}
  95. >
  96. <Form.Section text={t('模型请求速率限制')}>
  97. <Row gutter={16}>
  98. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  99. <Form.Switch
  100. field={'ModelRequestRateLimitEnabled'}
  101. label={t('启用用户模型请求速率限制(可能会影响高并发性能)')}
  102. size='default'
  103. checkedText='|'
  104. uncheckedText='〇'
  105. onChange={(value) => {
  106. setInputs({
  107. ...inputs,
  108. ModelRequestRateLimitEnabled: value,
  109. });
  110. }}
  111. />
  112. </Col>
  113. </Row>
  114. <Row>
  115. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  116. <Form.InputNumber
  117. label={t('限制周期')}
  118. step={1}
  119. min={0}
  120. suffix={t('分钟')}
  121. extraText={t('频率限制的周期(分钟)')}
  122. field={'ModelRequestRateLimitDurationMinutes'}
  123. onChange={(value) =>
  124. setInputs({
  125. ...inputs,
  126. ModelRequestRateLimitDurationMinutes: String(value),
  127. })
  128. }
  129. />
  130. </Col>
  131. </Row>
  132. <Row>
  133. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  134. <Form.InputNumber
  135. label={t('用户每周期最多请求次数')}
  136. step={1}
  137. min={0}
  138. max={100000000}
  139. suffix={t('次')}
  140. extraText={t('包括失败请求的次数,0代表不限制')}
  141. field={'ModelRequestRateLimitCount'}
  142. onChange={(value) =>
  143. setInputs({
  144. ...inputs,
  145. ModelRequestRateLimitCount: String(value),
  146. })
  147. }
  148. />
  149. </Col>
  150. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  151. <Form.InputNumber
  152. label={t('用户每周期最多请求完成次数')}
  153. step={1}
  154. min={1}
  155. max={100000000}
  156. suffix={t('次')}
  157. extraText={t('只包括请求成功的次数')}
  158. field={'ModelRequestRateLimitSuccessCount'}
  159. onChange={(value) =>
  160. setInputs({
  161. ...inputs,
  162. ModelRequestRateLimitSuccessCount: String(value),
  163. })
  164. }
  165. />
  166. </Col>
  167. </Row>
  168. <Row>
  169. <Col xs={24} sm={16}>
  170. <Form.TextArea
  171. label={t('分组速率限制')}
  172. placeholder={t(
  173. '{\n "default": [200, 100],\n "vip": [0, 1000]\n}',
  174. )}
  175. field={'ModelRequestRateLimitGroup'}
  176. autosize={{ minRows: 5, maxRows: 15 }}
  177. trigger='blur'
  178. stopValidateWithError
  179. rules={[
  180. {
  181. validator: (rule, value) => verifyJSON(value),
  182. message: t('不是合法的 JSON 字符串'),
  183. },
  184. ]}
  185. extraText={
  186. <div>
  187. <p>{t('说明:')}</p>
  188. <ul>
  189. <li>{t('使用 JSON 对象格式,格式为:{"组名": [最多请求次数, 最多请求完成次数]}')}</li>
  190. <li>{t('示例:{"default": [200, 100], "vip": [0, 1000]}。')}</li>
  191. <li>{t('[最多请求次数]必须大于等于0,[最多请求完成次数]必须大于等于1。')}</li>
  192. <li>{t('[最多请求次数]和[最多请求完成次数]的最大值为2147483647。')}</li>
  193. <li>{t('分组速率配置优先级高于全局速率限制。')}</li>
  194. <li>{t('限制周期统一使用上方配置的“限制周期”值。')}</li>
  195. </ul>
  196. </div>
  197. }
  198. onChange={(value) => {
  199. setInputs({ ...inputs, ModelRequestRateLimitGroup: value });
  200. }}
  201. />
  202. </Col>
  203. </Row>
  204. <Row>
  205. <Button size='default' onClick={onSubmit}>
  206. {t('保存模型速率限制')}
  207. </Button>
  208. </Row>
  209. </Form.Section>
  210. </Form>
  211. </Spin>
  212. </>
  213. );
  214. }