RatioSetting.jsx 4.0 KB

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