execution: handle undefined message in logUsedTokens

This commit is contained in:
Wroclaw 2024-04-26 04:02:09 +02:00
parent 67a6e4d486
commit d3567c3607

View file

@ -172,12 +172,12 @@ export async function queueRequest(request: apiRequest) {
* Logs used tokens to the terminal and to the database * Logs used tokens to the terminal and to the database
* @param answer the response that OpenAI returned * @param answer the response that OpenAI returned
* @param message the message that initiated the execution * @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( function logUsedTokens(
answer: ChatCompletion, answer: ChatCompletion,
message: RequestMessage, message: RequestMessage | undefined = undefined,
functionRan: number, functionRan: number = 0,
) { ) {
const usage = answer.usage; const usage = answer.usage;
const functionNames = const functionNames =
@ -185,6 +185,12 @@ function logUsedTokens(
v => v.type === "function" ? v.function.name : `[unknown type]` v => v.type === "function" ? v.function.name : `[unknown type]`
); );
if (usage !== undefined) { 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}`; 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(", ") + "]" : ""}`); 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, usageRequest: usage.prompt_tokens,
usageResponse: usage.completion_tokens, usageResponse: usage.completion_tokens,
functionName: functionNames?.join(", ") ?? null, functionName: functionNames?.join(", ") ?? null,
functionRan: functionNames ? functionRan : 0, functionRan: functionRan,
} }
}).catch((e => { }).catch((e => {
console.error("Failed to push to a database"); console.error("Failed to push to a database");