Don't split tool calls and tool results
This commit is contained in:
parent
7db85e7ed8
commit
591f61d354
|
@ -732,19 +732,15 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
|
||||||
/** @type {import('./tool-calling.js').ToolInvocation[]} */
|
/** @type {import('./tool-calling.js').ToolInvocation[]} */
|
||||||
const invocations = chatPrompt.invocations;
|
const invocations = chatPrompt.invocations;
|
||||||
const toolCallMessage = new Message(chatMessage.role, undefined, 'toolCall-' + chatMessage.identifier);
|
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);
|
toolCallMessage.setToolCalls(invocations);
|
||||||
if (chatCompletion.canAfford(toolCallMessage)) {
|
if (chatCompletion.canAffordAll([toolCallMessage, ...toolResultMessages])) {
|
||||||
chatCompletion.reserveBudget(toolCallMessage);
|
for (const resultMessage of toolResultMessages) {
|
||||||
for (const invocation of invocations.slice().reverse()) {
|
chatCompletion.insertAtStart(resultMessage, 'chatHistory');
|
||||||
const toolResultMessage = new Message('tool', invocation.result || '[No content]', invocation.id);
|
|
||||||
const canAfford = chatCompletion.canAfford(toolResultMessage);
|
|
||||||
if (!canAfford) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
chatCompletion.insertAtStart(toolResultMessage, 'chatHistory');
|
|
||||||
}
|
}
|
||||||
chatCompletion.freeBudget(toolCallMessage);
|
|
||||||
chatCompletion.insertAtStart(toolCallMessage, 'chatHistory');
|
chatCompletion.insertAtStart(toolCallMessage, 'chatHistory');
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
|
@ -2652,6 +2648,15 @@ export class ChatCompletion {
|
||||||
return 0 <= this.tokenBudget - message.getTokens();
|
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.
|
* Checks if a message with the specified identifier exists in the collection.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue