ModelSetting.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 { API, showError, showSuccess, toBoolean } from '../../helpers';
  18. import { useTranslation } from 'react-i18next';
  19. import SettingGeminiModel from '../../pages/Setting/Model/SettingGeminiModel';
  20. import SettingClaudeModel from '../../pages/Setting/Model/SettingClaudeModel';
  21. import SettingGlobalModel from '../../pages/Setting/Model/SettingGlobalModel';
  22. const ModelSetting = () => {
  23. const { t } = useTranslation();
  24. let [inputs, setInputs] = useState({
  25. 'gemini.safety_settings': '',
  26. 'gemini.version_settings': '',
  27. 'gemini.supported_imagine_models': '',
  28. 'gemini.remove_function_response_id_enabled': true,
  29. 'claude.model_headers_settings': '',
  30. 'claude.thinking_adapter_enabled': true,
  31. 'claude.default_max_tokens': '',
  32. 'claude.thinking_adapter_budget_tokens_percentage': 0.8,
  33. 'global.pass_through_request_enabled': false,
  34. 'global.thinking_model_blacklist': '[]',
  35. 'global.chat_completions_to_responses_policy': '{}',
  36. 'general_setting.ping_interval_enabled': false,
  37. 'general_setting.ping_interval_seconds': 60,
  38. 'gemini.thinking_adapter_enabled': false,
  39. 'gemini.thinking_adapter_budget_tokens_percentage': 0.6,
  40. });
  41. let [loading, setLoading] = useState(false);
  42. const getOptions = async () => {
  43. const res = await API.get('/api/option/');
  44. const { success, message, data } = res.data;
  45. if (success) {
  46. let newInputs = {};
  47. data.forEach((item) => {
  48. if (
  49. item.key === 'gemini.safety_settings' ||
  50. item.key === 'gemini.version_settings' ||
  51. item.key === 'claude.model_headers_settings' ||
  52. item.key === 'claude.default_max_tokens' ||
  53. item.key === 'gemini.supported_imagine_models' ||
  54. item.key === 'global.thinking_model_blacklist' ||
  55. item.key === 'global.chat_completions_to_responses_policy'
  56. ) {
  57. if (item.value !== '') {
  58. try {
  59. item.value = JSON.stringify(JSON.parse(item.value), null, 2);
  60. } catch (e) {
  61. // Keep raw value so user can fix it, and avoid crashing the page.
  62. console.error(`Invalid JSON for option ${item.key}:`, e);
  63. }
  64. }
  65. }
  66. // Keep boolean config keys ending with enabled/Enabled so UI parses correctly.
  67. if (item.key.endsWith('Enabled') || item.key.endsWith('enabled')) {
  68. newInputs[item.key] = toBoolean(item.value);
  69. } else {
  70. newInputs[item.key] = item.value;
  71. }
  72. });
  73. setInputs(newInputs);
  74. } else {
  75. showError(message);
  76. }
  77. };
  78. async function onRefresh() {
  79. try {
  80. setLoading(true);
  81. await getOptions();
  82. // showSuccess('刷新成功');
  83. } catch (error) {
  84. showError('刷新失败');
  85. console.error(error);
  86. } finally {
  87. setLoading(false);
  88. }
  89. }
  90. useEffect(() => {
  91. onRefresh();
  92. }, []);
  93. return (
  94. <>
  95. <Spin spinning={loading} size='large'>
  96. {/* OpenAI */}
  97. <Card style={{ marginTop: '10px' }}>
  98. <SettingGlobalModel options={inputs} refresh={onRefresh} />
  99. </Card>
  100. {/* Gemini */}
  101. <Card style={{ marginTop: '10px' }}>
  102. <SettingGeminiModel options={inputs} refresh={onRefresh} />
  103. </Card>
  104. {/* Claude */}
  105. <Card style={{ marginTop: '10px' }}>
  106. <SettingClaudeModel options={inputs} refresh={onRefresh} />
  107. </Card>
  108. </Spin>
  109. </>
  110. );
  111. };
  112. export default ModelSetting;