diff --git a/src/execution.ts b/src/execution.ts index 1cfcaa8..63679fa 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -172,12 +172,12 @@ export async function queueRequest(request: apiRequest) { * Logs used tokens to the terminal and to the database * @param answer the response that OpenAI returned * @param message the message that initiated the execution - * @param functionRan counter of how many function have been ran + * @param functionRan counter of how many function have been ran (to distinct records in database) */ function logUsedTokens( answer: ChatCompletion, - message: RequestMessage, - functionRan: number, + message: RequestMessage | undefined = undefined, + functionRan: number = 0, ) { const usage = answer.usage; const functionNames = @@ -185,6 +185,12 @@ function logUsedTokens( v => v.type === "function" ? v.function.name : `[unknown type]` ); if (usage !== undefined) { + if (!message) { + // log usage to stdout even if we can't store it in database + console.warn(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens from unknown call`); + // it doesn't make sense to store usage in database if we don't know where it came from + return; + } const channelName: string = !message.channel.isDMBased() ? `${message.channel.name} (${message.guild?.name})` : `@${getAuthor(message).tag}`; console.log(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens for ${getAuthor(message).tag} (${getAuthor(message).id}) in #${channelName}${functionNames && functionNames.length > 0 ? " [Tools: " + functionNames.join(", ") + "]" : ""}`); @@ -197,7 +203,7 @@ function logUsedTokens( usageRequest: usage.prompt_tokens, usageResponse: usage.completion_tokens, functionName: functionNames?.join(", ") ?? null, - functionRan: functionNames ? functionRan : 0, + functionRan: functionRan, } }).catch((e => { console.error("Failed to push to a database");