Make sure new example chat is not added without messages

This commit is contained in:
maver 2023-08-20 15:53:42 +02:00
parent 5fee1f6f96
commit 58ab266365
1 changed files with 22 additions and 0 deletions

View File

@ -556,6 +556,8 @@ function populateDialogueExamples(prompts, chatCompletion) {
if (openai_msgs_example.length) {
const newExampleChat = new Message('system', oai_settings.new_example_chat_prompt, 'newChat');
[...openai_msgs_example].forEach((dialogue, dialogueIndex) => {
let examplesAdded = 0;
if (chatCompletion.canAfford(newExampleChat)) chatCompletion.insert(newExampleChat, 'dialogueExamples');
dialogue.forEach((prompt, promptIndex) => {
@ -567,8 +569,13 @@ function populateDialogueExamples(prompts, chatCompletion) {
chatMessage.setName(prompt.name);
if (chatCompletion.canAfford(chatMessage)) {
chatCompletion.insert(chatMessage, 'dialogueExamples');
examplesAdded++;
}
});
if (0 === examplesAdded) {
chatCompletion.removeLastFrom('dialogueExamples');
}
});
}
}
@ -1673,6 +1680,21 @@ class ChatCompletion {
}
}
/**
* Remove the last item of the collection
*
* @param identifier
*/
removeLastFrom(identifier) {
const index = this.findMessageIndex(identifier);
const message = this.messages.collection[index].collection.pop();
this.increaseTokenBudgetBy(message.getTokens());
this.log(`Removed ${message.identifier} from ${identifier}. Remaining tokens: ${this.tokenBudget}`);
}
/**
* Checks if the token budget can afford the tokens of the specified message.
*