Prevent new example chat message being added for no reason

This commit is contained in:
maver 2023-08-12 18:30:39 +02:00
parent a9fa42524d
commit 03b8bb64fa

View File

@ -479,28 +479,26 @@ function populateChatHistory(prompts, chatCompletion, type = null, cyclePrompt =
function populateDialogueExamples(prompts, chatCompletion) { function populateDialogueExamples(prompts, chatCompletion) {
chatCompletion.add(new MessageCollection('dialogueExamples'), prompts.index('dialogueExamples')); chatCompletion.add(new MessageCollection('dialogueExamples'), prompts.index('dialogueExamples'));
if (openai_msgs_example.length) { if (openai_msgs_example.length) {
// Insert chat message examples if there's enough budget if there is enough budget left for at least one example. const newExampleChat = new Message('system', oai_settings.new_example_chat_prompt, 'newChat');
const dialogueExampleChat = new Message('system', oai_settings.new_example_chat_prompt, 'newChat'); chatCompletion.reserveBudget(newExampleChat);
const prompt = openai_msgs_example[0];
const dialogueExample = new Message(prompt[0]?.role || 'system', prompt[0]?.content || '', 'dialogueExampleTest');
if (chatCompletion.canAfford(dialogueExampleChat) && [...openai_msgs_example].forEach((dialogue, dialogueIndex) => {
chatCompletion.canAfford(dialogueExample)) { dialogue.forEach((prompt, promptIndex) => {
chatCompletion.insert(dialogueExampleChat, 'dialogueExamples'); const role = prompt.name === 'example_assistant' ? 'assistant' : 'user';
const content = prompt.content || '';
const identifier = `dialogueExamples ${dialogueIndex}-${promptIndex}`;
[...openai_msgs_example].forEach((dialogue, dialogueIndex) => { const chatMessage = new Message(role, content, identifier);
dialogue.forEach((prompt, promptIndex) => { if (chatCompletion.canAfford(chatMessage)) {
const role = prompt.name === 'example_assistant' ? 'assistant' : 'user'; chatCompletion.insert(chatMessage, 'dialogueExamples');
const content = prompt.content || ''; }
const identifier = `dialogueExamples ${dialogueIndex}-${promptIndex}`;
const chatMessage = new Message(role, content, identifier);
if (chatCompletion.canAfford(chatMessage)) {
chatCompletion.insert(chatMessage, 'dialogueExamples');
}
});
}); });
} });
chatCompletion.freeBudget(newExampleChat);
const chatExamples = chatCompletion.getMessages().getItemByIdentifier('dialogueExamples').getCollection();
if(chatExamples.length) chatCompletion.insertAtStart(newExampleChat,'dialogueExamples');
} }
} }