diff --git a/src/app.tsx b/src/app.tsx index 47c3154..462345c 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -4,7 +4,7 @@ import "./global.css"; import { calculate_token_length, Message } from "./chatgpt"; import getDefaultParams from "./getDefaultParam"; import ChatBOX from "./chatbox"; -import { options } from "./settings"; +import models from "./models"; import CHATGPT_API_WEB_VERSION from "./CHATGPT_API_WEB_VERSION"; @@ -26,6 +26,7 @@ export interface ChatStore { streamMode: boolean; model: string; responseModelName: string; + cost: number; } const _defaultAPIEndpoint = "https://api.openai.com/v1/chat/completions"; @@ -43,12 +44,13 @@ const newChatStore = ( postBeginIndex: 0, tokenMargin: 1024, totalTokens: 0, - maxTokens: options[getDefaultParams("model", model)], + maxTokens: models[getDefaultParams("model", model)].maxToken, apiKey: getDefaultParams("key", apiKey), apiEndpoint: getDefaultParams("api", apiEndpoint), streamMode: getDefaultParams("mode", streamMode), model: getDefaultParams("model", model), responseModelName: "", + cost: 0, }; }; @@ -99,6 +101,7 @@ export function App() { if (message.token === undefined) message.token = calculate_token_length(message.content); } + if (ret.cost === undefined) ret.cost = 0; return ret; }; diff --git a/src/chatbox.tsx b/src/chatbox.tsx index fa52423..585b9a0 100644 --- a/src/chatbox.tsx +++ b/src/chatbox.tsx @@ -7,6 +7,7 @@ import ChatGPT, { FetchResponse, } from "./chatgpt"; import Message from "./message"; +import models from "./models"; import Settings from "./settings"; export default function ChatBOX(props: { @@ -102,6 +103,13 @@ export default function ChatBOX(props: { chatStore.streamMode = false; const data = (await response.json()) as FetchResponse; chatStore.responseModelName = data.model ?? ""; + if (data.model) { + chatStore.cost += + (data.usage.prompt_tokens ?? 0) * models[data.model].price.prompt; + chatStore.cost += + (data.usage.completion_tokens ?? 0) * + models[data.model].price.completion; + } const content = client.processFetchResponse(data); chatStore.history.push({ role: "assistant", @@ -239,9 +247,10 @@ export default function ChatBOX(props: { {" "} {chatStore.model}{" "} - Messages: {chatStore.history.filter(({ hide }) => !hide).length} + Msg: {chatStore.history.filter(({ hide }) => !hide).length} {" "} - Cut: {chatStore.postBeginIndex} + Cut: {chatStore.postBeginIndex}{" "} + ${chatStore.cost.toFixed(4)}
+ Total cost in this section ${props.chatStore.cost.toFixed(4)} +
+ chatgpt-api-web ChatStore Version{" "} + {props.chatStore.chatgpt_api_web_version} +