render.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { Tag } from '@douyinfe/semi-ui';
  2. export function renderText(text, limit) {
  3. if (text.length > limit) {
  4. return text.slice(0, limit - 3) + '...';
  5. }
  6. return text;
  7. }
  8. /**
  9. * Render group tags based on the input group string
  10. * @param {string} group - The input group string
  11. * @returns {JSX.Element} - The rendered group tags
  12. */
  13. export function renderGroup(group) {
  14. if (group === '') {
  15. return (
  16. <Tag size='large' key='default'>
  17. unknown
  18. </Tag>
  19. );
  20. }
  21. const tagColors = {
  22. vip: 'yellow',
  23. pro: 'yellow',
  24. svip: 'red',
  25. premium: 'red',
  26. };
  27. const groups = group.split(',').sort();
  28. return (
  29. <span key={group}>
  30. {groups.map((group) => (
  31. <Tag
  32. size='large'
  33. color={tagColors[group] || stringToColor(group)}
  34. key={group}
  35. >
  36. {group}
  37. </Tag>
  38. ))}
  39. </span>
  40. );
  41. }
  42. export function renderNumber(num) {
  43. if (num >= 1000000000) {
  44. return (num / 1000000000).toFixed(1) + 'B';
  45. } else if (num >= 1000000) {
  46. return (num / 1000000).toFixed(1) + 'M';
  47. } else if (num >= 10000) {
  48. return (num / 1000).toFixed(1) + 'k';
  49. } else {
  50. return num;
  51. }
  52. }
  53. export function renderQuotaNumberWithDigit(num, digits = 2) {
  54. let displayInCurrency = localStorage.getItem('display_in_currency');
  55. num = num.toFixed(digits);
  56. if (displayInCurrency) {
  57. return '$' + num;
  58. }
  59. return num;
  60. }
  61. export function renderNumberWithPoint(num) {
  62. num = num.toFixed(2);
  63. if (num >= 100000) {
  64. // Convert number to string to manipulate it
  65. let numStr = num.toString();
  66. // Find the position of the decimal point
  67. let decimalPointIndex = numStr.indexOf('.');
  68. let wholePart = numStr;
  69. let decimalPart = '';
  70. // If there is a decimal point, split the number into whole and decimal parts
  71. if (decimalPointIndex !== -1) {
  72. wholePart = numStr.slice(0, decimalPointIndex);
  73. decimalPart = numStr.slice(decimalPointIndex);
  74. }
  75. // Take the first two and last two digits of the whole number part
  76. let shortenedWholePart = wholePart.slice(0, 2) + '..' + wholePart.slice(-2);
  77. // Return the formatted number
  78. return shortenedWholePart + decimalPart;
  79. }
  80. // If the number is less than 100,000, return it unmodified
  81. return num;
  82. }
  83. export function getQuotaPerUnit() {
  84. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  85. quotaPerUnit = parseFloat(quotaPerUnit);
  86. return quotaPerUnit;
  87. }
  88. export function renderUnitWithQuota(quota) {
  89. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  90. quotaPerUnit = parseFloat(quotaPerUnit);
  91. quota = parseFloat(quota);
  92. return quotaPerUnit * quota;
  93. }
  94. export function getQuotaWithUnit(quota, digits = 6) {
  95. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  96. quotaPerUnit = parseFloat(quotaPerUnit);
  97. return (quota / quotaPerUnit).toFixed(digits);
  98. }
  99. export function renderQuotaWithAmount(amount) {
  100. let displayInCurrency = localStorage.getItem('display_in_currency');
  101. displayInCurrency = displayInCurrency === 'true';
  102. if (displayInCurrency) {
  103. return '$' + amount;
  104. } else {
  105. return renderUnitWithQuota(amount);
  106. }
  107. }
  108. export function renderQuota(quota, digits = 2) {
  109. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  110. let displayInCurrency = localStorage.getItem('display_in_currency');
  111. quotaPerUnit = parseFloat(quotaPerUnit);
  112. displayInCurrency = displayInCurrency === 'true';
  113. if (displayInCurrency) {
  114. return '$' + (quota / quotaPerUnit).toFixed(digits);
  115. }
  116. return renderNumber(quota);
  117. }
  118. export function renderModelPrice(
  119. inputTokens,
  120. completionTokens,
  121. modelRatio,
  122. modelPrice = -1,
  123. completionRatio,
  124. groupRatio,
  125. ) {
  126. // 1 ratio = $0.002 / 1K tokens
  127. if (modelPrice !== -1) {
  128. return '模型价格:$' + modelPrice * groupRatio;
  129. } else {
  130. if (completionRatio === undefined) {
  131. completionRatio = 0;
  132. }
  133. let inputRatioPrice = modelRatio * 2.0 * groupRatio;
  134. let completionRatioPrice = modelRatio * completionRatio * 2.0 * groupRatio;
  135. let price =
  136. (inputTokens / 1000000) * inputRatioPrice +
  137. (completionTokens / 1000000) * completionRatioPrice;
  138. return (
  139. <>
  140. <article>
  141. <p>提示 ${inputRatioPrice} / 1M tokens</p>
  142. <p>补全 ${completionRatioPrice} / 1M tokens</p>
  143. <p>计算过程:</p>
  144. <p>
  145. 提示 {inputTokens} tokens / 1M tokens * ${inputRatioPrice} + 补全{' '}
  146. {completionTokens} tokens / 1M tokens * ${completionRatioPrice} = $
  147. {price.toFixed(6)}
  148. </p>
  149. </article>
  150. </>
  151. );
  152. }
  153. }
  154. export function renderQuotaWithPrompt(quota, digits) {
  155. let displayInCurrency = localStorage.getItem('display_in_currency');
  156. displayInCurrency = displayInCurrency === 'true';
  157. if (displayInCurrency) {
  158. return `(等价金额:${renderQuota(quota, digits)})`;
  159. }
  160. return '';
  161. }
  162. const colors = [
  163. 'amber',
  164. 'blue',
  165. 'cyan',
  166. 'green',
  167. 'grey',
  168. 'indigo',
  169. 'light-blue',
  170. 'lime',
  171. 'orange',
  172. 'pink',
  173. 'purple',
  174. 'red',
  175. 'teal',
  176. 'violet',
  177. 'yellow',
  178. ];
  179. export const modelColorMap = {
  180. 'dall-e': 'rgb(147,112,219)', // 深紫色
  181. // 'dall-e-2': 'rgb(147,112,219)', // 介于紫色和蓝色之间的色调
  182. 'dall-e-3': 'rgb(153,50,204)', // 介于紫罗兰和洋红之间的色调
  183. 'gpt-3.5-turbo': 'rgb(184,227,167)', // 浅绿色
  184. // 'gpt-3.5-turbo-0301': 'rgb(131,220,131)', // 亮绿色
  185. 'gpt-3.5-turbo-0613': 'rgb(60,179,113)', // 海洋绿
  186. 'gpt-3.5-turbo-1106': 'rgb(32,178,170)', // 浅海洋绿
  187. 'gpt-3.5-turbo-16k': 'rgb(149,252,206)', // 淡橙色
  188. 'gpt-3.5-turbo-16k-0613': 'rgb(119,255,214)', // 淡桃色
  189. 'gpt-3.5-turbo-instruct': 'rgb(175,238,238)', // 粉蓝色
  190. 'gpt-4': 'rgb(135,206,235)', // 天蓝色
  191. // 'gpt-4-0314': 'rgb(70,130,180)', // 钢蓝色
  192. 'gpt-4-0613': 'rgb(100,149,237)', // 矢车菊蓝
  193. 'gpt-4-1106-preview': 'rgb(30,144,255)', // 道奇蓝
  194. 'gpt-4-0125-preview': 'rgb(2,177,236)', // 深天蓝
  195. 'gpt-4-turbo-preview': 'rgb(2,177,255)', // 深天蓝
  196. 'gpt-4-32k': 'rgb(104,111,238)', // 中紫色
  197. // 'gpt-4-32k-0314': 'rgb(90,105,205)', // 暗灰蓝色
  198. 'gpt-4-32k-0613': 'rgb(61,71,139)', // 暗蓝灰色
  199. 'gpt-4-all': 'rgb(65,105,225)', // 皇家蓝
  200. 'gpt-4-gizmo-*': 'rgb(0,0,255)', // 纯蓝色
  201. 'gpt-4-vision-preview': 'rgb(25,25,112)', // 午夜蓝
  202. 'text-ada-001': 'rgb(255,192,203)', // 粉红色
  203. 'text-babbage-001': 'rgb(255,160,122)', // 浅珊瑚色
  204. 'text-curie-001': 'rgb(219,112,147)', // 苍紫罗兰色
  205. // 'text-davinci-002': 'rgb(199,21,133)', // 中紫罗兰红色
  206. 'text-davinci-003': 'rgb(219,112,147)', // 苍紫罗兰色(与Curie相同,表示同一个系列)
  207. 'text-davinci-edit-001': 'rgb(255,105,180)', // 热粉色
  208. 'text-embedding-ada-002': 'rgb(255,182,193)', // 浅粉红
  209. 'text-embedding-v1': 'rgb(255,174,185)', // 浅粉红色(略有区别)
  210. 'text-moderation-latest': 'rgb(255,130,171)', // 强粉色
  211. 'text-moderation-stable': 'rgb(255,160,122)', // 浅珊瑚色(与Babbage相同,表示同一类功能)
  212. 'tts-1': 'rgb(255,140,0)', // 深橙色
  213. 'tts-1-1106': 'rgb(255,165,0)', // 橙色
  214. 'tts-1-hd': 'rgb(255,215,0)', // 金色
  215. 'tts-1-hd-1106': 'rgb(255,223,0)', // 金黄色(略有区别)
  216. 'whisper-1': 'rgb(245,245,220)', // 米色
  217. 'claude-3-opus-20240229': 'rgb(255,132,31)', // 橙红色
  218. 'claude-3-sonnet-20240229': 'rgb(253,135,93)', // 橙色
  219. 'claude-3-haiku-20240307': 'rgb(255,175,146)', // 浅橙色
  220. 'claude-2.1': 'rgb(255,209,190)', // 浅橙色(略有区别)
  221. };
  222. export function stringToColor(str) {
  223. let sum = 0;
  224. // 对字符串中的每个字符进行操作
  225. for (let i = 0; i < str.length; i++) {
  226. // 将字符的ASCII值加到sum中
  227. sum += str.charCodeAt(i);
  228. }
  229. // 使用模运算得到个位数
  230. let i = sum % colors.length;
  231. return colors[i];
  232. }