From e8b972042553b27fbce73cb9377067ebaeebc965 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 2 Oct 2024 01:56:27 +0300 Subject: [PATCH] Budgeting fix --- public/scripts/openai.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 8b9c06048..96821eda4 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -727,18 +727,20 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul if (toolCalling && Array.isArray(chatPrompt.invocations)) { /** @type {import('./tool-calling.js').ToolInvocation[]} */ - const invocations = chatPrompt.invocations.slice().reverse(); + const invocations = chatPrompt.invocations; const toolCallMessage = new Message('assistant', undefined, 'toolCall-' + chatMessage.identifier); toolCallMessage.setToolCalls(invocations); if (chatCompletion.canAfford(toolCallMessage)) { - for (const invocation of invocations) { + chatCompletion.reserveBudget(toolCallMessage); + for (const invocation of invocations.slice().reverse()) { const toolResultMessage = new Message('tool', invocation.result, invocation.id); - const canAfford = chatCompletion.canAfford(toolResultMessage) && chatCompletion.canAfford(toolCallMessage); + const canAfford = chatCompletion.canAfford(toolResultMessage); if (!canAfford) { break; } chatCompletion.insertAtStart(toolResultMessage, 'chatHistory'); } + chatCompletion.freeBudget(toolCallMessage); chatCompletion.insertAtStart(toolCallMessage, 'chatHistory'); } }