Refactor documentation and make code more readable

No functional changes
This commit is contained in:
maver 2023-07-17 19:57:43 +02:00
parent 19ee831d82
commit b2acc9eb0f
1 changed files with 33 additions and 32 deletions

View File

@ -295,8 +295,8 @@ function setOpenAIMessageExamples(mesExamplesArray) {
function setupOpenAIPromptManager(openAiSettings) { function setupOpenAIPromptManager(openAiSettings) {
// Do not set up prompt manager more than once // Do not set up prompt manager more than once
if (promptManager) return true; if (promptManager) return true;
promptManager = new PromptManager(); promptManager = new PromptManager();
const configuration = { const configuration = {
@ -507,6 +507,7 @@ function populateDialogueExamples(prompts, chatCompletion) {
function populateChatCompletion (prompts, chatCompletion, {bias, quietPrompt, type, cyclePrompt} = {}) { function populateChatCompletion (prompts, chatCompletion, {bias, quietPrompt, type, cyclePrompt} = {}) {
//Helper function for the recurring task of preparing a prompt for the chat completion //Helper function for the recurring task of preparing a prompt for the chat completion
const addToChatCompletion = (source, target = null) => { const addToChatCompletion = (source, target = null) => {
// We need the prompts array to determine a position for the source.
if (false === prompts.has(source)) return; if (false === prompts.has(source)) return;
const prompt = prompts.get(source); const prompt = prompts.get(source);
@ -527,7 +528,7 @@ function populateChatCompletion (prompts, chatCompletion, {bias, quietPrompt, ty
if (type === "impersonate") addToChatCompletion('impersonate', 'main'); if (type === "impersonate") addToChatCompletion('impersonate', 'main');
else addToChatCompletion('main'); else addToChatCompletion('main');
// Add managed system and user prompts // Add ordered system and user prompts
const systemPrompts = ['nsfw', 'jailbreak']; const systemPrompts = ['nsfw', 'jailbreak'];
const userPrompts = prompts.collection const userPrompts = prompts.collection
.filter((prompt) => false === prompt.system_prompt) .filter((prompt) => false === prompt.system_prompt)
@ -611,17 +612,17 @@ function populateChatCompletion (prompts, chatCompletion, {bias, quietPrompt, ty
* Take a configuration object and prepares messages for a chat with OpenAI's chat completion API. * Take a configuration object and prepares messages for a chat with OpenAI's chat completion API.
* Handles prompts, prepares chat history, manages token budget, and processes various user settings. * Handles prompts, prepares chat history, manages token budget, and processes various user settings.
* *
* @param {Object} options - The options for the function. * @param {Object} content - System prompts provided by SillyTavern
* @param {string} options.name2 - The second name to be used in the messages. * @param {string} content.name2 - The second name to be used in the messages.
* @param {string} options.charDescription - Description of the character. * @param {string} content.charDescription - Description of the character.
* @param {string} options.charPersonality - Description of the character's personality. * @param {string} content.charPersonality - Description of the character's personality.
* @param {string} options.Scenario - The scenario or context of the dialogue. * @param {string} content.Scenario - The scenario or context of the dialogue.
* @param {string} options.worldInfoBefore - The world info to be added before the main conversation. * @param {string} content.worldInfoBefore - The world info to be added before the main conversation.
* @param {string} options.worldInfoAfter - The world info to be added after the main conversation. * @param {string} content.worldInfoAfter - The world info to be added after the main conversation.
* @param {string} options.bias - The bias to be added in the conversation. * @param {string} content.bias - The bias to be added in the conversation.
* @param {string} options.type - The type of the chat, can be 'impersonate'. * @param {string} content.type - The type of the chat, can be 'impersonate'.
* @param {string} options.quietPrompt - The quiet prompt to be used in the conversation. * @param {string} content.quietPrompt - The quiet prompt to be used in the conversation.
* @param {Array} options.extensionPrompts - An array of additional prompts. * @param {Array} content.extensionPrompts - An array of additional prompts.
* @param dryRun - Whether this is a live call or not. * @param dryRun - Whether this is a live call or not.
* @returns {(*[]|boolean)[]} An array where the first element is the prepared chat and the second element is a boolean flag. * @returns {(*[]|boolean)[]} An array where the first element is the prepared chat and the second element is a boolean flag.
*/ */
@ -641,21 +642,11 @@ function prepareOpenAIMessages({
// Without a character selected, there is no way to accurately calculate tokens // Without a character selected, there is no way to accurately calculate tokens
if (!promptManager.activeCharacter && dryRun) return [null, false]; if (!promptManager.activeCharacter && dryRun) return [null, false];
const prompts = promptManager.getPromptCollection();
const chatCompletion = new ChatCompletion();
const userSettings = promptManager.serviceSettings;
chatCompletion.setTokenBudget(userSettings.openai_max_context, userSettings.openai_max_tokens);
if (power_user.console_log_prompts) chatCompletion.enableLogging();
const scenarioText = Scenario ? `Circumstances and context of the dialogue: ${Scenario}` : ''; const scenarioText = Scenario ? `Circumstances and context of the dialogue: ${Scenario}` : '';
const charPersonalityText = charPersonality ? `${name2}'s personality: ${charPersonality}` : ''; const charPersonalityText = charPersonality ? `${name2}'s personality: ${charPersonality}` : '';
// Merge items to send, whose are managed by the prompt manager, with items from other places in silly tavern // Create entries for system prompts
// While the position in this array matters for positioning items inside the chat completion, elements const systemPrompts = [
// may be appended for later reference, as long as the initial order is not altered.
const mappedPrompts = [
// Ordered prompts for which a marker should exist // Ordered prompts for which a marker should exist
{role: 'system', content: formatWorldInfo(worldInfoBefore), identifier: 'worldInfoBefore'}, {role: 'system', content: formatWorldInfo(worldInfoBefore), identifier: 'worldInfoBefore'},
{role: 'system', content: formatWorldInfo(worldInfoAfter), identifier: 'worldInfoAfter'}, {role: 'system', content: formatWorldInfo(worldInfoAfter), identifier: 'worldInfoAfter'},
@ -671,19 +662,22 @@ function prepareOpenAIMessages({
// Tavern Extras - Summary // Tavern Extras - Summary
const summary = extensionPrompts['1_memory']; const summary = extensionPrompts['1_memory'];
if (summary && summary.content) mappedPrompts.push({role: 'system', content: summary.content, identifier: 'summary'}); if (summary && summary.content) systemPrompts.push({role: 'system', content: summary.content, identifier: 'summary'});
// Authors Note // Authors Note
const authorsNote = extensionPrompts['2_floating_prompt']; const authorsNote = extensionPrompts['2_floating_prompt'];
if (authorsNote && authorsNote.value) mappedPrompts.push({role: 'system', content: authorsNote.value, identifier: 'authorsNote'}); if (authorsNote && authorsNote.value) systemPrompts.push({role: 'system', content: authorsNote.value, identifier: 'authorsNote'});
// Persona Description // Persona Description
if (power_user.persona_description) { if (power_user.persona_description) {
mappedPrompts.push({role: 'system', content: power_user.persona_description, identifier: 'personaDescription'}); systemPrompts.push({role: 'system', content: power_user.persona_description, identifier: 'personaDescription'});
} }
// Create prompt objects and substitute markers // This is the prompt order defined by the user
mappedPrompts.forEach(prompt => { const prompts = promptManager.getPromptCollection();
// Merge system prompts with prompt manager prompts
systemPrompts.forEach(prompt => {
const newPrompt = promptManager.preparePrompt(prompt); const newPrompt = promptManager.preparePrompt(prompt);
const markerIndex = prompts.index(prompt.identifier); const markerIndex = prompts.index(prompt.identifier);
@ -691,7 +685,7 @@ function prepareOpenAIMessages({
else prompts.add(newPrompt); else prompts.add(newPrompt);
}); });
// Replace original-placeholder for supported prompts // Replace {{original}} placeholder for supported prompts
const originalReplacements = { const originalReplacements = {
main: default_main_prompt, main: default_main_prompt,
nsfw: default_nsfw_prompt, nsfw: default_nsfw_prompt,
@ -708,7 +702,14 @@ function prepareOpenAIMessages({
// Allow subscribers to manipulate the prompts object // Allow subscribers to manipulate the prompts object
eventSource.emit(event_types.OAI_BEFORE_CHATCOMPLETION, prompts); eventSource.emit(event_types.OAI_BEFORE_CHATCOMPLETION, prompts);
const chatCompletion = new ChatCompletion();
if (power_user.console_log_prompts) chatCompletion.enableLogging();
const userSettings = promptManager.serviceSettings;
chatCompletion.setTokenBudget(userSettings.openai_max_context, userSettings.openai_max_tokens);
try { try {
// Fill the chat completion with as much context as the budget allows
populateChatCompletion(prompts, chatCompletion, {bias, quietPrompt, type, cyclePrompt}); populateChatCompletion(prompts, chatCompletion, {bias, quietPrompt, type, cyclePrompt});
} catch (error) { } catch (error) {
if (error instanceof TokenBudgetExceededError) { if (error instanceof TokenBudgetExceededError) {