From 67a6e4d4864dd699dcfe14fb7634662d6b03fd2f Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Thu, 25 Apr 2024 01:10:30 +0200 Subject: [PATCH] execution+configDefault: retrofit for tooll_calls api --- src/configDefault.ts | 2 +- src/execution.ts | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/configDefault.ts b/src/configDefault.ts index 12ce515..620a363 100644 --- a/src/configDefault.ts +++ b/src/configDefault.ts @@ -16,7 +16,7 @@ export interface IConfigRequired { /** Messages to append at the start of every chat history when sending to API */ systemPrompt(context: Message): OpenAIMessage[]; /** OpenAI model config */ - readonly chatCompletionParams: Omit; + readonly chatCompletionParams: Omit; /** Limits for message selection */ readonly readLimits: { /** Maximum message age to include (in miliseconds) */ diff --git a/src/execution.ts b/src/execution.ts index d68f7c0..1cfcaa8 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -180,10 +180,13 @@ function logUsedTokens( functionRan: number, ) { const usage = answer.usage; - const functionName = answer.choices[0].message?.function_call?.name; + const functionNames = + answer.choices[0].message.tool_calls?.map( + v => v.type === "function" ? v.function.name : `[unknown type]` + ); if (usage !== undefined) { 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}${functionName ? " [Function: " + functionName + "]" : ""}`); + 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(", ") + "]" : ""}`); database.usage.create({ data: { @@ -193,8 +196,8 @@ function logUsedTokens( guild: message.guildId ? BigInt(message.guildId) : null, usageRequest: usage.prompt_tokens, usageResponse: usage.completion_tokens, - functionName: functionName ?? null, - functionRan: functionName ? functionRan : 0, + functionName: functionNames?.join(", ") ?? null, + functionRan: functionNames ? functionRan : 0, } }).catch((e => { console.error("Failed to push to a database"); @@ -249,6 +252,7 @@ async function executeFromQueue(channel: string) { tools: new FunctionManager().getToolsForOpenAi(), }); + functionRanCounter += answer.choices[0].message?.tool_calls?.length ?? 0; logUsedTokens(answer, message, ++functionRanCounter); generatedMessage = answer.choices[0].message; @@ -260,7 +264,7 @@ async function executeFromQueue(channel: string) { // FIXME: don't use new instance of FunctionManager OpenAImessages.push(...(await new FunctionManager().handleToolCalls(generatedMessage.tool_calls))); } - } while (generatedMessage.function_call); + } while (generatedMessage.tool_calls !== undefined && generatedMessage.tool_calls.length > 0); channelQueue.stopTyping();