ModelPricing.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import React, { useContext, useEffect, useRef, useMemo, useState } from 'react';
  2. import { API, copy, showError, showSuccess } from '../helpers';
  3. import {
  4. Banner,
  5. Input,
  6. Layout,
  7. Modal,
  8. Space,
  9. Table,
  10. Tag,
  11. Tooltip,
  12. Popover,
  13. ImagePreview,
  14. Button,
  15. } from '@douyinfe/semi-ui';
  16. import {
  17. IconMore,
  18. IconVerify,
  19. IconUploadError,
  20. IconHelpCircle,
  21. } from '@douyinfe/semi-icons';
  22. import { UserContext } from '../context/User/index.js';
  23. import Text from '@douyinfe/semi-ui/lib/es/typography/text';
  24. function renderQuotaType(type) {
  25. // Ensure all cases are string literals by adding quotes.
  26. switch (type) {
  27. case 1:
  28. return (
  29. <Tag color='teal' size='large'>
  30. 按次计费
  31. </Tag>
  32. );
  33. case 0:
  34. return (
  35. <Tag color='violet' size='large'>
  36. 按量计费
  37. </Tag>
  38. );
  39. default:
  40. return '未知';
  41. }
  42. }
  43. function renderAvailable(available) {
  44. return available ? (
  45. <Popover
  46. content={
  47. <div style={{ padding: 8 }}>您的分组可以使用该模型</div>
  48. }
  49. position='top'
  50. key={available}
  51. style={{
  52. backgroundColor: 'rgba(var(--semi-blue-4),1)',
  53. borderColor: 'rgba(var(--semi-blue-4),1)',
  54. color: 'var(--semi-color-white)',
  55. borderWidth: 1,
  56. borderStyle: 'solid',
  57. }}
  58. >
  59. <IconVerify style={{ color: 'green' }} size="large" />
  60. </Popover>
  61. ) : (
  62. <Popover
  63. content={
  64. <div style={{ padding: 8 }}>您的分组无权使用该模型</div>
  65. }
  66. position='top'
  67. key={available}
  68. style={{
  69. backgroundColor: 'rgba(var(--semi-blue-4),1)',
  70. borderColor: 'rgba(var(--semi-blue-4),1)',
  71. color: 'var(--semi-color-white)',
  72. borderWidth: 1,
  73. borderStyle: 'solid',
  74. }}
  75. >
  76. <IconUploadError style={{ color: '#FFA54F' }} size="large" />
  77. </Popover>
  78. );
  79. }
  80. const ModelPricing = () => {
  81. const [filteredValue, setFilteredValue] = useState([]);
  82. const compositionRef = useRef({ isComposition: false });
  83. const [selectedRowKeys, setSelectedRowKeys] = useState([]);
  84. const [modalImageUrl, setModalImageUrl] = useState('');
  85. const [isModalOpenurl, setIsModalOpenurl] = useState(false);
  86. const rowSelection = useMemo(
  87. () => ({
  88. onChange: (selectedRowKeys, selectedRows) => {
  89. setSelectedRowKeys(selectedRowKeys);
  90. },
  91. }),
  92. []
  93. );
  94. const handleChange = (value) => {
  95. if (compositionRef.current.isComposition) {
  96. return;
  97. }
  98. const newFilteredValue = value ? [value] : [];
  99. setFilteredValue(newFilteredValue);
  100. };
  101. const handleCompositionStart = () => {
  102. compositionRef.current.isComposition = true;
  103. };
  104. const handleCompositionEnd = (event) => {
  105. compositionRef.current.isComposition = false;
  106. const value = event.target.value;
  107. const newFilteredValue = value ? [value] : [];
  108. setFilteredValue(newFilteredValue);
  109. };
  110. const columns = [
  111. {
  112. title: '可用性',
  113. dataIndex: 'available',
  114. render: (text, record, index) => {
  115. return renderAvailable(text);
  116. },
  117. sorter: (a, b) => a.available - b.available,
  118. },
  119. {
  120. title: (
  121. <Space>
  122. <span>模型名称</span>
  123. <Input
  124. placeholder='模糊搜索'
  125. style={{ width: 200 }}
  126. onCompositionStart={handleCompositionStart}
  127. onCompositionEnd={handleCompositionEnd}
  128. onChange={handleChange}
  129. showClear
  130. />
  131. </Space>
  132. ),
  133. dataIndex: 'model_name', // 以finish_time作为dataIndex
  134. render: (text, record, index) => {
  135. return (
  136. <>
  137. <Tag
  138. color='green'
  139. size='large'
  140. onClick={() => {
  141. copyText(text);
  142. }}
  143. >
  144. {text}
  145. </Tag>
  146. </>
  147. );
  148. },
  149. onFilter: (value, record) =>
  150. record.model_name.toLowerCase().includes(value.toLowerCase()),
  151. filteredValue,
  152. },
  153. {
  154. title: '计费类型',
  155. dataIndex: 'quota_type',
  156. render: (text, record, index) => {
  157. return renderQuotaType(parseInt(text));
  158. },
  159. sorter: (a, b) => a.quota_type - b.quota_type,
  160. },
  161. {
  162. title: () => (
  163. <span style={{'display':'flex','alignItems':'center'}}>
  164. 倍率
  165. <Popover
  166. content={
  167. <div style={{ padding: 8 }}>倍率是为了方便换算不同价格的模型<br/>点击查看倍率说明</div>
  168. }
  169. position='top'
  170. style={{
  171. backgroundColor: 'rgba(var(--semi-blue-4),1)',
  172. borderColor: 'rgba(var(--semi-blue-4),1)',
  173. color: 'var(--semi-color-white)',
  174. borderWidth: 1,
  175. borderStyle: 'solid',
  176. }}
  177. >
  178. <IconHelpCircle
  179. onClick={() => {
  180. setModalImageUrl('/ratio.png');
  181. setIsModalOpenurl(true);
  182. }}
  183. />
  184. </Popover>
  185. </span>
  186. ),
  187. dataIndex: 'model_ratio',
  188. render: (text, record, index) => {
  189. let content = text;
  190. let completionRatio = parseFloat(record.completion_ratio.toFixed(3));
  191. content = (
  192. <>
  193. <Text>模型:{record.quota_type === 0 ? text : '无'}</Text>
  194. <br />
  195. <Text>补全:{record.quota_type === 0 ? completionRatio : '无'}</Text>
  196. </>
  197. );
  198. return <div>{content}</div>;
  199. },
  200. },
  201. {
  202. title: '模型价格',
  203. dataIndex: 'model_price',
  204. render: (text, record, index) => {
  205. let content = text;
  206. if (record.quota_type === 0) {
  207. // 这里的 *2 是因为 1倍率=0.002刀,请勿删除
  208. let inputRatioPrice = record.model_ratio * 2 * record.group_ratio;
  209. let completionRatioPrice =
  210. record.model_ratio *
  211. record.completion_ratio * 2 *
  212. record.group_ratio;
  213. content = (
  214. <>
  215. <Text>提示 ${inputRatioPrice} / 1M tokens</Text>
  216. <br />
  217. <Text>补全 ${completionRatioPrice} / 1M tokens</Text>
  218. </>
  219. );
  220. } else {
  221. let price = parseFloat(text) * record.group_ratio;
  222. content = <>模型价格:${price}</>;
  223. }
  224. return <div>{content}</div>;
  225. },
  226. },
  227. ];
  228. const [models, setModels] = useState([]);
  229. const [loading, setLoading] = useState(true);
  230. const [userState, userDispatch] = useContext(UserContext);
  231. const [groupRatio, setGroupRatio] = useState(1);
  232. const setModelsFormat = (models, groupRatio) => {
  233. for (let i = 0; i < models.length; i++) {
  234. models[i].key = models[i].model_name;
  235. models[i].group_ratio = groupRatio;
  236. }
  237. // sort by quota_type
  238. models.sort((a, b) => {
  239. return a.quota_type - b.quota_type;
  240. });
  241. // sort by model_name, start with gpt is max, other use localeCompare
  242. models.sort((a, b) => {
  243. if (a.model_name.startsWith('gpt') && !b.model_name.startsWith('gpt')) {
  244. return -1;
  245. } else if (
  246. !a.model_name.startsWith('gpt') &&
  247. b.model_name.startsWith('gpt')
  248. ) {
  249. return 1;
  250. } else {
  251. return a.model_name.localeCompare(b.model_name);
  252. }
  253. });
  254. setModels(models);
  255. };
  256. const loadPricing = async () => {
  257. setLoading(true);
  258. let url = '';
  259. url = `/api/pricing`;
  260. const res = await API.get(url);
  261. const { success, message, data, group_ratio } = res.data;
  262. if (success) {
  263. setGroupRatio(group_ratio);
  264. setModelsFormat(data, group_ratio);
  265. } else {
  266. showError(message);
  267. }
  268. setLoading(false);
  269. };
  270. const refresh = async () => {
  271. await loadPricing();
  272. };
  273. const copyText = async (text) => {
  274. if (await copy(text)) {
  275. showSuccess('已复制:' + text);
  276. } else {
  277. // setSearchKeyword(text);
  278. Modal.error({ title: '无法复制到剪贴板,请手动复制', content: text });
  279. }
  280. };
  281. useEffect(() => {
  282. refresh().then();
  283. }, []);
  284. return (
  285. <>
  286. <Layout>
  287. {userState.user ? (
  288. <Banner
  289. type="success"
  290. fullMode={false}
  291. closeIcon="null"
  292. description={`您的分组为:${userState.user.group},分组倍率为:${groupRatio}`}
  293. />
  294. ) : (
  295. <Banner
  296. type='warning'
  297. fullMode={false}
  298. closeIcon="null"
  299. description={`您还未登陆,显示的价格为默认分组倍率: ${groupRatio}`}
  300. />
  301. )}
  302. <br/>
  303. <Banner
  304. type="info"
  305. fullMode={false}
  306. description={<div>按量计费费用 = 分组倍率 × 模型倍率 × (提示token数 + 补全token数 × 补全倍率)/ 500000 (单位:美元)</div>}
  307. closeIcon="null"
  308. />
  309. <br/>
  310. <Button
  311. theme='light'
  312. type='tertiary'
  313. style={{width: 150}}
  314. onClick={() => {
  315. copyText(selectedRowKeys);
  316. }}
  317. disabled={selectedRowKeys == ""}
  318. >
  319. 复制选中模型
  320. </Button>
  321. <Table
  322. style={{ marginTop: 5 }}
  323. columns={columns}
  324. dataSource={models}
  325. loading={loading}
  326. pagination={{
  327. pageSize: models.length,
  328. showSizeChanger: false,
  329. }}
  330. rowSelection={rowSelection}
  331. />
  332. <ImagePreview
  333. src={modalImageUrl}
  334. visible={isModalOpenurl}
  335. onVisibleChange={(visible) => setIsModalOpenurl(visible)}
  336. />
  337. </Layout>
  338. </>
  339. );
  340. };
  341. export default ModelPricing;