Budgeting fix

This commit is contained in:
Cohee 2024-10-02 01:56:27 +03:00
parent 63724a2b38
commit e8b9720425
1 changed files with 5 additions and 3 deletions

View File

@ -727,18 +727,20 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
if (toolCalling && Array.isArray(chatPrompt.invocations)) { if (toolCalling && Array.isArray(chatPrompt.invocations)) {
/** @type {import('./tool-calling.js').ToolInvocation[]} */ /** @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); const toolCallMessage = new Message('assistant', undefined, 'toolCall-' + chatMessage.identifier);
toolCallMessage.setToolCalls(invocations); toolCallMessage.setToolCalls(invocations);
if (chatCompletion.canAfford(toolCallMessage)) { 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 toolResultMessage = new Message('tool', invocation.result, invocation.id);
const canAfford = chatCompletion.canAfford(toolResultMessage) && chatCompletion.canAfford(toolCallMessage); const canAfford = chatCompletion.canAfford(toolResultMessage);
if (!canAfford) { if (!canAfford) {
break; break;
} }
chatCompletion.insertAtStart(toolResultMessage, 'chatHistory'); chatCompletion.insertAtStart(toolResultMessage, 'chatHistory');
} }
chatCompletion.freeBudget(toolCallMessage);
chatCompletion.insertAtStart(toolCallMessage, 'chatHistory'); chatCompletion.insertAtStart(toolCallMessage, 'chatHistory');
} }
} }