Don't split tool calls and tool results

This commit is contained in:
Cohee 2024-10-07 01:32:46 +03:00
parent 7db85e7ed8
commit 591f61d354
1 changed files with 15 additions and 10 deletions

View File

@ -732,19 +732,15 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
/** @type {import('./tool-calling.js').ToolInvocation[]} */
const invocations = chatPrompt.invocations;
const toolCallMessage = new Message(chatMessage.role, undefined, 'toolCall-' + chatMessage.identifier);
const toolResultMessages = invocations.slice().reverse().map((invocation) => new Message('tool', invocation.result || '[No content]', invocation.id));
toolCallMessage.setToolCalls(invocations);
if (chatCompletion.canAfford(toolCallMessage)) {
chatCompletion.reserveBudget(toolCallMessage);
for (const invocation of invocations.slice().reverse()) {
const toolResultMessage = new Message('tool', invocation.result || '[No content]', invocation.id);
const canAfford = chatCompletion.canAfford(toolResultMessage);
if (!canAfford) {
break;
}
chatCompletion.insertAtStart(toolResultMessage, 'chatHistory');
if (chatCompletion.canAffordAll([toolCallMessage, ...toolResultMessages])) {
for (const resultMessage of toolResultMessages) {
chatCompletion.insertAtStart(resultMessage, 'chatHistory');
}
chatCompletion.freeBudget(toolCallMessage);
chatCompletion.insertAtStart(toolCallMessage, 'chatHistory');
} else {
break;
}
continue;
@ -2652,6 +2648,15 @@ export class ChatCompletion {
return 0 <= this.tokenBudget - message.getTokens();
}
/**
* Checks if the token budget can afford the tokens of all the specified messages.
* @param {Message[]} messages - The messages to check for affordability.
* @returns {boolean} True if the budget can afford all the messages, false otherwise.
*/
canAffordAll(messages) {
return 0 <= this.tokenBudget - messages.reduce((total, message) => total + message.getTokens(), 0);
}
/**
* Checks if a message with the specified identifier exists in the collection.
*