feat: add price of tokens

This commit is contained in:
Jing Hua 2023-03-26 10:23:09 +08:00
parent 2d42616070
commit 7a35e20fbb
2 changed files with 17 additions and 2 deletions

View file

@ -1,8 +1,9 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useMemo, useState } from 'react';
import useStore from '@store/store'; import useStore from '@store/store';
import { shallow } from 'zustand/shallow'; import { shallow } from 'zustand/shallow';
import countTokens from '@utils/messageUtils'; import countTokens from '@utils/messageUtils';
import { modelCost } from '@constants/chat';
const TokenCount = React.memo(() => { const TokenCount = React.memo(() => {
const [tokenCount, setTokenCount] = useState<number>(0); const [tokenCount, setTokenCount] = useState<number>(0);
@ -19,6 +20,11 @@ const TokenCount = React.memo(() => {
: 'gpt-3.5-turbo' : 'gpt-3.5-turbo'
); );
const cost = useMemo(() => {
const price = modelCost[model].price * (tokenCount / modelCost[model].unit);
return price.toPrecision(3);
}, [model, tokenCount]);
useEffect(() => { useEffect(() => {
if (!generating) setTokenCount(countTokens(messages, model)); if (!generating) setTokenCount(countTokens(messages, model));
}, [messages, generating]); }, [messages, generating]);
@ -26,7 +32,7 @@ const TokenCount = React.memo(() => {
return ( return (
<div className='absolute top-[-16px] right-0'> <div className='absolute top-[-16px] right-0'>
<div className='text-xs italic text-gray-900 dark:text-gray-300'> <div className='text-xs italic text-gray-900 dark:text-gray-300'>
Tokens: {tokenCount} Tokens: {tokenCount} (${cost})
</div> </div>
</div> </div>
); );

View file

@ -34,6 +34,15 @@ export const modelMaxToken = {
'gpt-4-32k-0314': 32768, 'gpt-4-32k-0314': 32768,
}; };
export const modelCost = {
'gpt-3.5-turbo': { price: 0.002, unit: 1000 },
'gpt-3.5-turbo-0301': { price: 0.002, unit: 1000 },
'gpt-4': { price: 0.03, unit: 1000 },
'gpt-4-0314': { price: 0.03, unit: 1000 },
'gpt-4-32k': { price: 0.06, unit: 1000 },
'gpt-4-32k-0314': { price: 0.06, unit: 1000 },
};
export const defaultUserMaxToken = 4000; export const defaultUserMaxToken = 4000;
export const _defaultChatConfig: ConfigInterface = { export const _defaultChatConfig: ConfigInterface = {