You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

136 rivejä
4.6 KiB

  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 GroupRatioSettings from '../../pages/Setting/Ratio/GroupRatioSettings';
  19. import ModelRatioSettings from '../../pages/Setting/Ratio/ModelRatioSettings';
  20. import ModelSettingsVisualEditor from '../../pages/Setting/Ratio/ModelSettingsVisualEditor';
  21. import ModelRatioNotSetEditor from '../../pages/Setting/Ratio/ModelRationNotSetEditor';
  22. import UpstreamRatioSync from '../../pages/Setting/Ratio/UpstreamRatioSync';
  23. import PricingTagManager from '../../pages/Setting/Ratio/PricingTagManager';
  24. import ModelPricing from '../../pages/Setting/Operation/ModelPricing';
  25. import { API, showError, toBoolean } from '../../helpers';
  26. const RatioSetting = () => {
  27. const { t } = useTranslation();
  28. let [inputs, setInputs] = useState({
  29. ModelPrice: '',
  30. ModelRatio: '',
  31. CacheRatio: '',
  32. CreateCacheRatio: '',
  33. CompletionRatio: '',
  34. GroupRatio: '',
  35. GroupGroupRatio: '',
  36. ImageRatio: '',
  37. AudioRatio: '',
  38. AudioCompletionRatio: '',
  39. AutoGroups: '',
  40. DefaultUseAutoGroup: false,
  41. ExposeRatioEnabled: false,
  42. UserUsableGroups: '',
  43. 'group_ratio_setting.group_special_usable_group': '',
  44. });
  45. const [loading, setLoading] = useState(false);
  46. const getOptions = async () => {
  47. const res = await API.get('/api/option/');
  48. const { success, message, data } = res.data;
  49. if (success) {
  50. let newInputs = {};
  51. data.forEach((item) => {
  52. if (item.value.startsWith('{') || item.value.startsWith('[')) {
  53. try {
  54. item.value = JSON.stringify(JSON.parse(item.value), null, 2);
  55. } catch (e) {
  56. // 如果后端返回的不是合法 JSON,直接展示
  57. }
  58. }
  59. if (['DefaultUseAutoGroup', 'ExposeRatioEnabled'].includes(item.key)) {
  60. newInputs[item.key] = toBoolean(item.value);
  61. } else {
  62. newInputs[item.key] = item.value;
  63. }
  64. });
  65. setInputs(newInputs);
  66. } else {
  67. showError(message);
  68. }
  69. };
  70. const onRefresh = async () => {
  71. try {
  72. setLoading(true);
  73. await getOptions();
  74. } catch (error) {
  75. showError(t('刷新失败'));
  76. } finally {
  77. setLoading(false);
  78. }
  79. };
  80. useEffect(() => {
  81. onRefresh();
  82. // eslint-disable-next-line react-hooks/exhaustive-deps
  83. }, []);
  84. return (
  85. <Spin spinning={loading} size='large'>
  86. {/* 模型倍率设置以及可视化编辑器 */}
  87. <Card style={{ marginTop: '10px' }}>
  88. <Tabs type='card'>
  89. <Tabs.TabPane tab={t('模型倍率设置')} itemKey='model'>
  90. <ModelRatioSettings options={inputs} refresh={onRefresh} />
  91. </Tabs.TabPane>
  92. <Tabs.TabPane tab={t('分组倍率设置')} itemKey='group'>
  93. <GroupRatioSettings options={inputs} refresh={onRefresh} />
  94. </Tabs.TabPane>
  95. <Tabs.TabPane tab={t('可视化倍率设置')} itemKey='visual'>
  96. <ModelSettingsVisualEditor options={inputs} refresh={onRefresh} />
  97. </Tabs.TabPane>
  98. <Tabs.TabPane tab={t('未设置倍率模型')} itemKey='unset_models'>
  99. <ModelRatioNotSetEditor options={inputs} refresh={onRefresh} />
  100. </Tabs.TabPane>
  101. <Tabs.TabPane tab={t('模型多维计费')} itemKey='model_pricing'>
  102. <div data-testid='model-pricing-tab'>
  103. <ModelPricing />
  104. </div>
  105. </Tabs.TabPane>
  106. <Tabs.TabPane tab={t('上游倍率同步')} itemKey='upstream_sync'>
  107. <UpstreamRatioSync options={inputs} refresh={onRefresh} />
  108. </Tabs.TabPane>
  109. <Tabs.TabPane
  110. tab={t('标签管理')}
  111. itemKey='pricing_tags'
  112. >
  113. <PricingTagManager />
  114. </Tabs.TabPane>
  115. </Tabs>
  116. </Card>
  117. </Spin>
  118. );
  119. };
  120. export default RatioSetting;