🐞 fix: Balance and Token Usage Improvements (#2350)

* fix(processModelData): handle `openrouter/auto` edge case

* fix(Tx.create): prevent negative multiplier edge case and prevent balance from becoming negative

* fix(NavLinks): render 0 balance properly

* refactor(NavLinks): show only up to 2 decimal places for balance

* fix(OpenAIClient/titleConvo): fix cohere condition and record token usage for `this.options.titleMethod === 'completion'`
This commit is contained in:
Danny Avila 2024-04-07 23:28:40 -04:00 committed by GitHub
parent 3411d7a543
commit 6f0eb35365
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 11 deletions

View file

@ -792,14 +792,18 @@ ${convo}
},
];
const promptTokens = this.getTokenCountForMessage(instructionsPayload[0]);
try {
let useChatCompletion = true;
if (CohereConstants.API_URL) {
if (this.options.reverseProxyUrl === CohereConstants.API_URL) {
useChatCompletion = false;
}
title = (
await this.sendPayload(instructionsPayload, { modelOptions, useChatCompletion })
).replaceAll('"', '');
const completionTokens = this.getTokenCount(title);
this.recordTokenUsage({ promptTokens, completionTokens, context: 'title' });
} catch (e) {
logger.error(
'[OpenAIClient] There was an issue generating the title with the completion method',
@ -951,12 +955,12 @@ ${convo}
}
}
async recordTokenUsage({ promptTokens, completionTokens }) {
async recordTokenUsage({ promptTokens, completionTokens, context = 'message' }) {
await spendTokens(
{
context,
user: this.user,
model: this.modelOptions.model,
context: 'message',
conversationId: this.conversationId,
endpointTokenConfig: this.options.endpointTokenConfig,
},