Skip to content

Commit

Permalink
calculate tokens for current branch only
Browse files Browse the repository at this point in the history
  • Loading branch information
Shackless committed Apr 11, 2023
1 parent 79659cf commit f486189
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/misc/openai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ChatCompletionRequestMessage } from 'openai';
import type { Chat, ChatCost } from './shared';
import GPT3Tokenizer from 'gpt3-tokenizer';
import { ChatStorekeeper } from './chatStorekeeper';

export enum OpenAiModel {
Gpt35Turbo = 'gpt-3.5-turbo',
Expand Down Expand Up @@ -73,10 +74,7 @@ export function estimateChatCost(chat: Chat): ChatCost {
let tokensPrompt = 0;
let tokensCompletion = 0;

// TODO: Calculate cost only for the current chat branch
const messages = chat.contextMessage?.content
? [chat.contextMessage, ...chat.messages]
: chat.messages;
const messages = ChatStorekeeper.getCurrentMessageBranch(chat);

for (const message of messages) {
if (message.role === 'assistant') {
Expand Down

0 comments on commit f486189

Please sign in to comment.