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

View File

@@ -556,6 +556,8 @@ function populateDialogueExamples(prompts, chatCompletion) {
if (openai_msgs_example.length) { if (openai_msgs_example.length) {
const newExampleChat = new Message('system', oai_settings.new_example_chat_prompt, 'newChat'); const newExampleChat = new Message('system', oai_settings.new_example_chat_prompt, 'newChat');
[...openai_msgs_example].forEach((dialogue, dialogueIndex) => { [...openai_msgs_example].forEach((dialogue, dialogueIndex) => {
let examplesAdded = 0;
if (chatCompletion.canAfford(newExampleChat)) chatCompletion.insert(newExampleChat, 'dialogueExamples'); if (chatCompletion.canAfford(newExampleChat)) chatCompletion.insert(newExampleChat, 'dialogueExamples');
dialogue.forEach((prompt, promptIndex) => { dialogue.forEach((prompt, promptIndex) => {
@@ -567,8 +569,13 @@ function populateDialogueExamples(prompts, chatCompletion) {
chatMessage.setName(prompt.name); chatMessage.setName(prompt.name);
if (chatCompletion.canAfford(chatMessage)) { if (chatCompletion.canAfford(chatMessage)) {
chatCompletion.insert(chatMessage, 'dialogueExamples'); 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. * Checks if the token budget can afford the tokens of the specified message.
* *