SettingClaudeModel.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, 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. import Text from '@douyinfe/semi-ui/lib/es/typography/text';
  27. const CLAUDE_HEADER = {
  28. 'claude-3-7-sonnet-20250219-thinking': {
  29. 'anthropic-beta': [
  30. 'output-128k-2025-02-19',
  31. 'token-efficient-tools-2025-02-19',
  32. ],
  33. },
  34. };
  35. const CLAUDE_DEFAULT_MAX_TOKENS = {
  36. default: 8192,
  37. 'claude-3-haiku-20240307': 4096,
  38. 'claude-3-opus-20240229': 4096,
  39. 'claude-3-7-sonnet-20250219-thinking': 8192,
  40. };
  41. export default function SettingClaudeModel(props) {
  42. const { t } = useTranslation();
  43. const [loading, setLoading] = useState(false);
  44. const [inputs, setInputs] = useState({
  45. 'claude.model_headers_settings': '',
  46. 'claude.thinking_adapter_enabled': true,
  47. 'claude.default_max_tokens': '',
  48. 'claude.thinking_adapter_budget_tokens_percentage': 0.8,
  49. });
  50. const refForm = useRef();
  51. const [inputsRow, setInputsRow] = useState(inputs);
  52. function onSubmit() {
  53. const updateArray = compareObjects(inputs, inputsRow);
  54. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  55. const requestQueue = updateArray.map((item) => {
  56. let value = String(inputs[item.key]);
  57. return API.put('/api/option/', {
  58. key: item.key,
  59. value,
  60. });
  61. });
  62. setLoading(true);
  63. Promise.all(requestQueue)
  64. .then((res) => {
  65. if (requestQueue.length === 1) {
  66. if (res.includes(undefined)) return;
  67. } else if (requestQueue.length > 1) {
  68. if (res.includes(undefined))
  69. return showError(t('部分保存失败,请重试'));
  70. }
  71. showSuccess(t('保存成功'));
  72. props.refresh();
  73. })
  74. .catch(() => {
  75. showError(t('保存失败,请重试'));
  76. })
  77. .finally(() => {
  78. setLoading(false);
  79. });
  80. }
  81. useEffect(() => {
  82. const currentInputs = {};
  83. for (let key in props.options) {
  84. if (Object.keys(inputs).includes(key)) {
  85. currentInputs[key] = props.options[key];
  86. }
  87. }
  88. setInputs(currentInputs);
  89. setInputsRow(structuredClone(currentInputs));
  90. refForm.current.setValues(currentInputs);
  91. }, [props.options]);
  92. return (
  93. <>
  94. <Spin spinning={loading}>
  95. <Form
  96. values={inputs}
  97. getFormApi={(formAPI) => (refForm.current = formAPI)}
  98. style={{ marginBottom: 15 }}
  99. >
  100. <Form.Section text={t('Claude设置')}>
  101. <Row>
  102. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  103. <Form.TextArea
  104. label={t('Claude请求头覆盖')}
  105. field={'claude.model_headers_settings'}
  106. placeholder={
  107. t('为一个 JSON 文本,例如:') +
  108. '\n' +
  109. JSON.stringify(CLAUDE_HEADER, null, 2)
  110. }
  111. extraText={
  112. t('示例') + '\n' + JSON.stringify(CLAUDE_HEADER, null, 2)
  113. }
  114. autosize={{ minRows: 6, maxRows: 12 }}
  115. trigger='blur'
  116. stopValidateWithError
  117. rules={[
  118. {
  119. validator: (rule, value) => verifyJSON(value),
  120. message: t('不是合法的 JSON 字符串'),
  121. },
  122. ]}
  123. onChange={(value) =>
  124. setInputs({
  125. ...inputs,
  126. 'claude.model_headers_settings': value,
  127. })
  128. }
  129. />
  130. </Col>
  131. </Row>
  132. <Row>
  133. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  134. <Form.TextArea
  135. label={t('缺省 MaxTokens')}
  136. field={'claude.default_max_tokens'}
  137. placeholder={
  138. t('为一个 JSON 文本,例如:') +
  139. '\n' +
  140. JSON.stringify(CLAUDE_DEFAULT_MAX_TOKENS, null, 2)
  141. }
  142. extraText={
  143. t('示例') +
  144. '\n' +
  145. JSON.stringify(CLAUDE_DEFAULT_MAX_TOKENS, null, 2)
  146. }
  147. autosize={{ minRows: 6, maxRows: 12 }}
  148. trigger='blur'
  149. stopValidateWithError
  150. rules={[
  151. {
  152. validator: (rule, value) => verifyJSON(value),
  153. message: t('不是合法的 JSON 字符串'),
  154. },
  155. ]}
  156. onChange={(value) =>
  157. setInputs({ ...inputs, 'claude.default_max_tokens': value })
  158. }
  159. />
  160. </Col>
  161. </Row>
  162. <Row>
  163. <Col span={16}>
  164. <Form.Switch
  165. label={t('启用Claude思考适配(-thinking后缀)')}
  166. field={'claude.thinking_adapter_enabled'}
  167. onChange={(value) =>
  168. setInputs({
  169. ...inputs,
  170. 'claude.thinking_adapter_enabled': value,
  171. })
  172. }
  173. />
  174. </Col>
  175. </Row>
  176. <Row>
  177. <Col span={16}>
  178. {/*//展示MaxTokens和BudgetTokens的计算公式, 并展示实际数字*/}
  179. <Text>
  180. {t(
  181. 'Claude思考适配 BudgetTokens = MaxTokens * BudgetTokens 百分比',
  182. )}
  183. </Text>
  184. </Col>
  185. </Row>
  186. <Row>
  187. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  188. <Form.InputNumber
  189. label={t('思考适配 BudgetTokens 百分比')}
  190. field={'claude.thinking_adapter_budget_tokens_percentage'}
  191. initValue={''}
  192. extraText={t('0.1以上的小数')}
  193. min={0.1}
  194. onChange={(value) =>
  195. setInputs({
  196. ...inputs,
  197. 'claude.thinking_adapter_budget_tokens_percentage': value,
  198. })
  199. }
  200. />
  201. </Col>
  202. </Row>
  203. <Row>
  204. <Button size='default' onClick={onSubmit}>
  205. {t('保存')}
  206. </Button>
  207. </Row>
  208. </Form.Section>
  209. </Form>
  210. </Spin>
  211. </>
  212. );
  213. }