execution+configDefault: retrofit for tooll_calls api

This commit is contained in:
Wroclaw 2024-04-25 01:10:30 +02:00
parent 370b7623b5
commit 67a6e4d486
2 changed files with 10 additions and 6 deletions

View file

@ -16,7 +16,7 @@ export interface IConfigRequired {
/** Messages to append at the start of every chat history when sending to API */ /** Messages to append at the start of every chat history when sending to API */
systemPrompt(context: Message): OpenAIMessage[]; systemPrompt(context: Message): OpenAIMessage[];
/** OpenAI model config */ /** OpenAI model config */
readonly chatCompletionParams: Omit<ChatCompletionRequestData, "messages" | "function_call" | "functions" | "n">; readonly chatCompletionParams: Omit<ChatCompletionRequestData, "messages" | "function_call" | "tool_call" | "functions" | "n">;
/** Limits for message selection */ /** Limits for message selection */
readonly readLimits: { readonly readLimits: {
/** Maximum message age to include (in miliseconds) */ /** Maximum message age to include (in miliseconds) */

View file

@ -180,10 +180,13 @@ function logUsedTokens(
functionRan: number, functionRan: number,
) { ) {
const usage = answer.usage; 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) { if (usage !== undefined) {
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}${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({ database.usage.create({
data: { data: {
@ -193,8 +196,8 @@ function logUsedTokens(
guild: message.guildId ? BigInt(message.guildId) : null, guild: message.guildId ? BigInt(message.guildId) : null,
usageRequest: usage.prompt_tokens, usageRequest: usage.prompt_tokens,
usageResponse: usage.completion_tokens, usageResponse: usage.completion_tokens,
functionName: functionName ?? null, functionName: functionNames?.join(", ") ?? null,
functionRan: functionName ? functionRan : 0, functionRan: functionNames ? functionRan : 0,
} }
}).catch((e => { }).catch((e => {
console.error("Failed to push to a database"); console.error("Failed to push to a database");
@ -249,6 +252,7 @@ async function executeFromQueue(channel: string) {
tools: new FunctionManager().getToolsForOpenAi(), tools: new FunctionManager().getToolsForOpenAi(),
}); });
functionRanCounter += answer.choices[0].message?.tool_calls?.length ?? 0;
logUsedTokens(answer, message, ++functionRanCounter); logUsedTokens(answer, message, ++functionRanCounter);
generatedMessage = answer.choices[0].message; generatedMessage = answer.choices[0].message;
@ -260,7 +264,7 @@ async function executeFromQueue(channel: string) {
// FIXME: don't use new instance of FunctionManager // FIXME: don't use new instance of FunctionManager
OpenAImessages.push(...(await new FunctionManager().handleToolCalls(generatedMessage.tool_calls))); 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(); channelQueue.stopTyping();