RatioSetting.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 support@quantumnous.com
  14. */
  15. import React, { useEffect, useState } from 'react';
  16. import { Card, Spin, Tabs } from '@douyinfe/semi-ui';
  17. import { useTranslation } from 'react-i18next';
  18. import ModelPricingCombined from '../../pages/Setting/Ratio/ModelPricingCombined';
  19. import GroupRatioSettings from '../../pages/Setting/Ratio/GroupRatioSettings';
  20. import ModelRatioNotSetEditor from '../../pages/Setting/Ratio/ModelRationNotSetEditor';
  21. import UpstreamRatioSync from '../../pages/Setting/Ratio/UpstreamRatioSync';
  22. import { API, showError, toBoolean } from '../../helpers';
  23. const RatioSetting = () => {
  24. const { t } = useTranslation();
  25. let [inputs, setInputs] = useState({
  26. ModelPrice: '',
  27. ModelRatio: '',
  28. CacheRatio: '',
  29. CreateCacheRatio: '',
  30. CompletionRatio: '',
  31. GroupRatio: '',
  32. GroupGroupRatio: '',
  33. ImageRatio: '',
  34. AudioRatio: '',
  35. AudioCompletionRatio: '',
  36. AutoGroups: '',
  37. DefaultUseAutoGroup: false,
  38. ExposeRatioEnabled: false,
  39. UserUsableGroups: '',
  40. 'group_ratio_setting.group_special_usable_group': '',
  41. });
  42. const [loading, setLoading] = useState(false);
  43. const getOptions = async () => {
  44. const res = await API.get('/api/option/');
  45. const { success, message, data } = res.data;
  46. if (success) {
  47. let newInputs = {};
  48. data.forEach((item) => {
  49. if (item.value.startsWith('{') || item.value.startsWith('[')) {
  50. try {
  51. item.value = JSON.stringify(JSON.parse(item.value), null, 2);
  52. } catch (e) {
  53. // 如果后端返回的不是合法 JSON,直接展示
  54. }
  55. }
  56. if (['DefaultUseAutoGroup', 'ExposeRatioEnabled'].includes(item.key)) {
  57. newInputs[item.key] = toBoolean(item.value);
  58. } else {
  59. newInputs[item.key] = item.value;
  60. }
  61. });
  62. setInputs(newInputs);
  63. } else {
  64. showError(message);
  65. }
  66. };
  67. const onRefresh = async () => {
  68. try {
  69. setLoading(true);
  70. await getOptions();
  71. } catch (error) {
  72. showError('刷新失败');
  73. } finally {
  74. setLoading(false);
  75. }
  76. };
  77. useEffect(() => {
  78. onRefresh();
  79. // eslint-disable-next-line react-hooks/exhaustive-deps
  80. }, []);
  81. return (
  82. <Spin spinning={loading} size='large'>
  83. <Card style={{ marginTop: '10px' }}>
  84. <Tabs type='card' defaultActiveKey='pricing'>
  85. <Tabs.TabPane tab={t('模型定价设置')} itemKey='pricing'>
  86. <ModelPricingCombined options={inputs} refresh={onRefresh} />
  87. </Tabs.TabPane>
  88. <Tabs.TabPane tab={t('分组相关设置')} itemKey='group'>
  89. <GroupRatioSettings options={inputs} refresh={onRefresh} />
  90. </Tabs.TabPane>
  91. <Tabs.TabPane tab={t('未设置价格模型')} itemKey='unset_models'>
  92. <ModelRatioNotSetEditor options={inputs} refresh={onRefresh} />
  93. </Tabs.TabPane>
  94. <Tabs.TabPane tab={t('上游倍率同步')} itemKey='upstream_sync'>
  95. <UpstreamRatioSync options={inputs} refresh={onRefresh} />
  96. </Tabs.TabPane>
  97. </Tabs>
  98. </Card>
  99. </Spin>
  100. );
  101. };
  102. export default RatioSetting;