Merge branch 'staging' into qr-rewrite

This commit is contained in:
LenAnderson
2023-12-23 22:05:10 +00:00
16 changed files with 501 additions and 116 deletions

View File

@@ -110,6 +110,7 @@ const extension_settings = {
sd: {
prompts: {},
character_prompts: {},
character_negative_prompts: {},
},
chromadb: {},
translate: {},

View File

@@ -351,6 +351,10 @@ async function loadSettings() {
extension_settings.sd.character_prompts = {};
}
if (extension_settings.sd.character_negative_prompts === undefined) {
extension_settings.sd.character_negative_prompts = {};
}
if (!Array.isArray(extension_settings.sd.styles)) {
extension_settings.sd.styles = defaultStyles;
}
@@ -575,6 +579,7 @@ function onChatChanged() {
$('#sd_character_prompt_block').show();
const key = getCharaFilename(this_chid);
$('#sd_character_prompt').val(key ? (extension_settings.sd.character_prompts[key] || '') : '');
$('#sd_character_negative_prompt').val(key ? (extension_settings.sd.character_negative_prompts[key] || '') : '');
}
function onCharacterPromptInput() {
@@ -584,6 +589,13 @@ function onCharacterPromptInput() {
saveSettingsDebounced();
}
function onCharacterNegativePromptInput() {
const key = getCharaFilename(this_chid);
extension_settings.sd.character_negative_prompts[key] = $('#sd_character_negative_prompt').val();
resetScrollHeight($(this));
saveSettingsDebounced();
}
function getCharacterPrefix() {
if (!this_chid || selected_group) {
return '';
@@ -598,6 +610,20 @@ function getCharacterPrefix() {
return '';
}
function getCharacterNegativePrefix() {
if (!this_chid || selected_group) {
return '';
}
const key = getCharaFilename(this_chid);
if (key) {
return extension_settings.sd.character_negative_prompts[key] || '';
}
return '';
}
/**
* Combines two prompt prefixes into one.
* @param {string} str1 Base string
@@ -1885,34 +1911,38 @@ async function sendGenerationRequest(generationType, prompt, characterName = nul
const prefixedPrompt = combinePrefixes(prefix, prompt, '{prompt}');
const negativePrompt = noCharPrefix.includes(generationType)
? extension_settings.sd.negative_prompt
: combinePrefixes(extension_settings.sd.negative_prompt, getCharacterNegativePrefix());
let result = { format: '', data: '' };
const currentChatId = getCurrentChatId();
try {
switch (extension_settings.sd.source) {
case sources.extras:
result = await generateExtrasImage(prefixedPrompt);
result = await generateExtrasImage(prefixedPrompt, negativePrompt);
break;
case sources.horde:
result = await generateHordeImage(prefixedPrompt);
result = await generateHordeImage(prefixedPrompt, negativePrompt);
break;
case sources.vlad:
result = await generateAutoImage(prefixedPrompt);
result = await generateAutoImage(prefixedPrompt, negativePrompt);
break;
case sources.auto:
result = await generateAutoImage(prefixedPrompt);
result = await generateAutoImage(prefixedPrompt, negativePrompt);
break;
case sources.novel:
result = await generateNovelImage(prefixedPrompt);
result = await generateNovelImage(prefixedPrompt, negativePrompt);
break;
case sources.openai:
result = await generateOpenAiImage(prefixedPrompt);
break;
case sources.comfy:
result = await generateComfyImage(prefixedPrompt);
result = await generateComfyImage(prefixedPrompt, negativePrompt);
break;
case sources.togetherai:
result = await generateTogetherAIImage(prefixedPrompt);
result = await generateTogetherAIImage(prefixedPrompt, negativePrompt);
break;
}
@@ -1936,13 +1966,13 @@ async function sendGenerationRequest(generationType, prompt, characterName = nul
callback ? callback(prompt, base64Image, generationType) : sendMessage(prompt, base64Image, generationType);
}
async function generateTogetherAIImage(prompt) {
async function generateTogetherAIImage(prompt, negativePrompt) {
const result = await fetch('/api/sd/together/generate', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({
prompt: prompt,
negative_prompt: extension_settings.sd.negative_prompt,
negative_prompt: negativePrompt,
model: extension_settings.sd.model,
steps: extension_settings.sd.steps,
width: extension_settings.sd.width,
@@ -1963,9 +1993,10 @@ async function generateTogetherAIImage(prompt) {
* Generates an "extras" image using a provided prompt and other settings.
*
* @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/
async function generateExtrasImage(prompt) {
async function generateExtrasImage(prompt, negativePrompt) {
const url = new URL(getApiUrl());
url.pathname = '/api/image';
const result = await doExtrasFetch(url, {
@@ -1980,7 +2011,7 @@ async function generateExtrasImage(prompt) {
scale: extension_settings.sd.scale,
width: extension_settings.sd.width,
height: extension_settings.sd.height,
negative_prompt: extension_settings.sd.negative_prompt,
negative_prompt: negativePrompt,
restore_faces: !!extension_settings.sd.restore_faces,
enable_hr: !!extension_settings.sd.enable_hr,
karras: !!extension_settings.sd.horde_karras,
@@ -2004,9 +2035,10 @@ async function generateExtrasImage(prompt) {
* Generates a "horde" image using the provided prompt and configuration settings.
*
* @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/
async function generateHordeImage(prompt) {
async function generateHordeImage(prompt, negativePrompt) {
const result = await fetch('/api/horde/generate-image', {
method: 'POST',
headers: getRequestHeaders(),
@@ -2017,7 +2049,7 @@ async function generateHordeImage(prompt) {
scale: extension_settings.sd.scale,
width: extension_settings.sd.width,
height: extension_settings.sd.height,
negative_prompt: extension_settings.sd.negative_prompt,
negative_prompt: negativePrompt,
model: extension_settings.sd.model,
nsfw: extension_settings.sd.horde_nsfw,
restore_faces: !!extension_settings.sd.restore_faces,
@@ -2039,16 +2071,17 @@ async function generateHordeImage(prompt) {
* Generates an image in SD WebUI API using the provided prompt and configuration settings.
*
* @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/
async function generateAutoImage(prompt) {
async function generateAutoImage(prompt, negativePrompt) {
const result = await fetch('/api/sd/generate', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({
...getSdRequestBody(),
prompt: prompt,
negative_prompt: extension_settings.sd.negative_prompt,
negative_prompt: negativePrompt,
sampler_name: extension_settings.sd.sampler,
steps: extension_settings.sd.steps,
cfg_scale: extension_settings.sd.scale,
@@ -2081,9 +2114,10 @@ async function generateAutoImage(prompt) {
* Generates an image in NovelAI API using the provided prompt and configuration settings.
*
* @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/
async function generateNovelImage(prompt) {
async function generateNovelImage(prompt, negativePrompt) {
const { steps, width, height } = getNovelParams();
const result = await fetch('/api/novelai/generate-image', {
@@ -2097,7 +2131,7 @@ async function generateNovelImage(prompt) {
scale: extension_settings.sd.scale,
width: width,
height: height,
negative_prompt: extension_settings.sd.negative_prompt,
negative_prompt: negativePrompt,
upscale_ratio: extension_settings.sd.novel_upscale_ratio,
}),
});
@@ -2225,11 +2259,11 @@ async function generateOpenAiImage(prompt) {
* Generates an image in ComfyUI using the provided prompt and configuration settings.
*
* @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/
async function generateComfyImage(prompt) {
async function generateComfyImage(prompt, negativePrompt) {
const placeholders = [
'negative_prompt',
'model',
'vae',
'sampler',
@@ -2252,6 +2286,7 @@ async function generateComfyImage(prompt) {
toastr.error(`Failed to load workflow.\n\n${text}`);
}
let workflow = (await workflowResponse.json()).replace('"%prompt%"', JSON.stringify(prompt));
workflow = (await workflowResponse.json()).replace('"%negative_prompt%"', JSON.stringify(negativePrompt));
workflow = workflow.replace('"%seed%"', JSON.stringify(Math.round(Math.random() * Number.MAX_SAFE_INTEGER)));
placeholders.forEach(ph => {
workflow = workflow.replace(`"%${ph}%"`, JSON.stringify(extension_settings.sd[ph]));
@@ -2629,6 +2664,7 @@ jQuery(async () => {
$('#sd_enable_hr').on('input', onHighResFixInput);
$('#sd_refine_mode').on('input', onRefineModeInput);
$('#sd_character_prompt').on('input', onCharacterPromptInput);
$('#sd_character_negative_prompt').on('input', onCharacterNegativePromptInput);
$('#sd_auto_validate').on('click', validateAutoUrl);
$('#sd_auto_url').on('input', onAutoUrlInput);
$('#sd_auto_auth').on('input', onAutoAuthInput);
@@ -2661,6 +2697,7 @@ jQuery(async () => {
initScrollHeight($('#sd_prompt_prefix'));
initScrollHeight($('#sd_negative_prompt'));
initScrollHeight($('#sd_character_prompt'));
initScrollHeight($('#sd_character_negative_prompt'));
});
for (const [key, value] of Object.entries(resolutionOptions)) {

View File

@@ -208,6 +208,9 @@
<label for="sd_character_prompt">Character-specific prompt prefix</label>
<small>Won't be used in groups.</small>
<textarea id="sd_character_prompt" class="text_pole textarea_compact" rows="3" placeholder="Any characteristics that describe the currently selected character. Will be added after a common prefix.&#10;Example: female, green eyes, brown hair, pink shirt"></textarea>
<label for="sd_character_negative_prompt">Character-specific negative prompt prefix</label>
<small>Won't be used in groups.</small>
<textarea id="sd_character_negative_prompt" class="text_pole textarea_compact" rows="3" placeholder="Any characteristics that should not appear for the selected character. Will be added after a negative common prefix.&#10;Example: jewellery, shoes, glasses"></textarea>
</div>
</div>
</div>

View File

@@ -2,7 +2,6 @@ import {
saveSettingsDebounced,
callPopup,
setGenerationProgress,
CLIENT_VERSION,
getRequestHeaders,
max_context,
amount_gen,
@@ -34,19 +33,96 @@ let horde_settings = {
const MAX_RETRIES = 480;
const CHECK_INTERVAL = 2500;
const MIN_LENGTH = 16;
const getRequestArgs = () => ({
method: 'GET',
headers: {
'Client-Agent': CLIENT_VERSION,
},
});
async function getWorkers(workerType) {
const response = await fetch('https://horde.koboldai.net/api/v2/workers?type=text', getRequestArgs());
/**
* Gets the available workers from Horde.
* @param {boolean} force Do a force refresh of the workers
* @returns {Promise<Array>} Array of workers
*/
async function getWorkers(force) {
const response = await fetch('/api/horde/text-workers', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ force }),
});
const data = await response.json();
return data;
}
/**
* Gets the available models from Horde.
* @param {boolean} force Do a force refresh of the models
* @returns {Promise<Array>} Array of models
*/
async function getModels(force) {
const response = await fetch('/api/horde/text-models', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ force }),
});
const data = await response.json();
return data;
}
/**
* Gets the status of a Horde task.
* @param {string} taskId Task ID
* @returns {Promise<Object>} Task status
*/
async function getTaskStatus(taskId) {
const response = await fetch('/api/horde/task-status', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ taskId }),
});
if (!response.ok) {
throw new Error(`Failed to get task status: ${response.statusText}`);
}
const data = await response.json();
return data;
}
/**
* Cancels a Horde task.
* @param {string} taskId Task ID
*/
async function cancelTask(taskId) {
const response = await fetch('/api/horde/cancel-task', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ taskId }),
});
if (!response.ok) {
throw new Error(`Failed to cancel task: ${response.statusText}`);
}
}
/**
* Checks if Horde is online.
* @returns {Promise<boolean>} True if Horde is online, false otherwise
*/
async function checkHordeStatus() {
try {
const response = await fetch('/api/horde/status', {
method: 'POST',
headers: getRequestHeaders(),
});
if (!response.ok) {
return false;
}
const data = await response.json();
return data.ok;
} catch (error) {
console.error(error);
return false;
}
}
function validateHordeModel() {
let selectedModels = models.filter(m => horde_settings.models.includes(m.name));
@@ -60,7 +136,7 @@ function validateHordeModel() {
async function adjustHordeGenerationParams(max_context_length, max_length) {
console.log(max_context_length, max_length);
const workers = await getWorkers();
const workers = await getWorkers(false);
let maxContextLength = max_context_length;
let maxLength = max_length;
let availableWorkers = [];
@@ -126,10 +202,7 @@ async function generateHorde(prompt, params, signal, reportProgress) {
const response = await fetch('/api/horde/generate-text', {
method: 'POST',
headers: {
...getRequestHeaders(),
'Client-Agent': CLIENT_VERSION,
},
headers: getRequestHeaders(),
body: JSON.stringify(payload),
});
@@ -146,24 +219,17 @@ async function generateHorde(prompt, params, signal, reportProgress) {
throw new Error(`Horde generation failed: ${reason}`);
}
const task_id = responseJson.id;
const taskId = responseJson.id;
let queue_position_first = null;
console.log(`Horde task id = ${task_id}`);
console.log(`Horde task id = ${taskId}`);
for (let retryNumber = 0; retryNumber < MAX_RETRIES; retryNumber++) {
if (signal.aborted) {
fetch(`https://horde.koboldai.net/api/v2/generate/text/status/${task_id}`, {
method: 'DELETE',
headers: {
'Client-Agent': CLIENT_VERSION,
},
});
cancelTask(taskId);
throw new Error('Request aborted');
}
const statusCheckResponse = await fetch(`https://horde.koboldai.net/api/v2/generate/text/status/${task_id}`, getRequestArgs());
const statusCheckJson = await statusCheckResponse.json();
const statusCheckJson = await getTaskStatus(taskId);
console.log(statusCheckJson);
if (statusCheckJson.faulted === true) {
@@ -202,18 +268,13 @@ async function generateHorde(prompt, params, signal, reportProgress) {
throw new Error('Horde timeout');
}
async function checkHordeStatus() {
const response = await fetch('https://horde.koboldai.net/api/v2/status/heartbeat', getRequestArgs());
return response.ok;
}
async function getHordeModels() {
/**
* Displays the available models in the Horde model selection dropdown.
* @param {boolean} force Force refresh of the models
*/
async function getHordeModels(force) {
$('#horde_model').empty();
const response = await fetch('https://horde.koboldai.net/api/v2/status/models?type=text', getRequestArgs());
models = await response.json();
models.sort((a, b) => {
return b.performance - a.performance;
});
models = (await getModels(force)).sort((a, b) => b.performance - a.performance);
for (const model of models) {
const option = document.createElement('option');
option.value = model.name;
@@ -299,7 +360,7 @@ jQuery(function () {
await writeSecret(SECRET_KEYS.HORDE, key);
});
$('#horde_refresh').on('click', getHordeModels);
$('#horde_refresh').on('click', () => getHordeModels(true));
$('#horde_kudos').on('click', showKudos);
// Not needed on mobile

View File

@@ -239,6 +239,7 @@ const default_settings = {
squash_system_messages: false,
image_inlining: false,
bypass_status_check: false,
continue_prefill: false,
seed: -1,
};
@@ -302,6 +303,7 @@ const oai_settings = {
squash_system_messages: false,
image_inlining: false,
bypass_status_check: false,
continue_prefill: false,
seed: -1,
};
@@ -660,12 +662,20 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
let continueMessage = null;
const instruct = isOpenRouterWithInstruct();
if (type === 'continue' && cyclePrompt && !instruct) {
const continuePrompt = new Prompt({
identifier: 'continueNudge',
role: 'system',
content: oai_settings.continue_nudge_prompt.replace('{{lastChatMessage}}', cyclePrompt),
system_prompt: true,
});
const promptObject = oai_settings.continue_prefill ?
{
identifier: 'continueNudge',
role: 'assistant',
content: cyclePrompt,
system_prompt: true,
} :
{
identifier: 'continueNudge',
role: 'system',
content: oai_settings.continue_nudge_prompt.replace('{{lastChatMessage}}', cyclePrompt),
system_prompt: true,
};
const continuePrompt = new Prompt(promptObject);
const preparedPrompt = promptManager.preparePrompt(continuePrompt);
continueMessage = Message.fromPrompt(preparedPrompt);
chatCompletion.reserveBudget(continueMessage);
@@ -2376,6 +2386,7 @@ function loadOpenAISettings(data, settings) {
oai_settings.new_example_chat_prompt = settings.new_example_chat_prompt ?? default_settings.new_example_chat_prompt;
oai_settings.continue_nudge_prompt = settings.continue_nudge_prompt ?? default_settings.continue_nudge_prompt;
oai_settings.squash_system_messages = settings.squash_system_messages ?? default_settings.squash_system_messages;
oai_settings.continue_prefill = settings.continue_prefill ?? default_settings.continue_prefill;
if (settings.wrap_in_quotes !== undefined) oai_settings.wrap_in_quotes = !!settings.wrap_in_quotes;
if (settings.names_in_completion !== undefined) oai_settings.names_in_completion = !!settings.names_in_completion;
@@ -2428,6 +2439,7 @@ function loadOpenAISettings(data, settings) {
$('#openrouter_force_instruct').prop('checked', oai_settings.openrouter_force_instruct);
$('#openrouter_group_models').prop('checked', oai_settings.openrouter_group_models);
$('#squash_system_messages').prop('checked', oai_settings.squash_system_messages);
$('#continue_prefill').prop('checked', oai_settings.continue_prefill);
if (settings.impersonation_prompt !== undefined) oai_settings.impersonation_prompt = settings.impersonation_prompt;
$('#impersonation_prompt_textarea').val(oai_settings.impersonation_prompt);
@@ -2593,6 +2605,10 @@ async function saveOpenAIPreset(name, settings, triggerUi = true) {
ai21_model: settings.ai21_model,
mistralai_model: settings.mistralai_model,
custom_model: settings.custom_model,
custom_url: settings.custom_url,
custom_include_body: settings.custom_include_body,
custom_exclude_body: settings.custom_exclude_body,
custom_include_headers: settings.custom_include_headers,
google_model: settings.google_model,
temperature: settings.temp_openai,
frequency_penalty: settings.freq_pen_openai,
@@ -2634,6 +2650,8 @@ async function saveOpenAIPreset(name, settings, triggerUi = true) {
use_alt_scale: settings.use_alt_scale,
squash_system_messages: settings.squash_system_messages,
image_inlining: settings.image_inlining,
bypass_status_check: settings.bypass_status_check,
continue_prefill: settings.continue_prefill,
seed: settings.seed,
};
@@ -3004,6 +3022,7 @@ function onSettingsPresetChange() {
use_alt_scale: ['#use_alt_scale', 'use_alt_scale', true],
squash_system_messages: ['#squash_system_messages', 'squash_system_messages', true],
image_inlining: ['#openai_image_inlining', 'image_inlining', true],
continue_prefill: ['#continue_prefill', 'continue_prefill', true],
seed: ['#seed_openai', 'seed', false],
};
@@ -3584,17 +3603,17 @@ function onCustomizeParametersClick() {
</div>
</div>`);
template.find('#custom_include_body').val(oai_settings.custom_include_body).on('input', function() {
template.find('#custom_include_body').val(oai_settings.custom_include_body).on('input', function () {
oai_settings.custom_include_body = String($(this).val());
saveSettingsDebounced();
});
template.find('#custom_exclude_body').val(oai_settings.custom_exclude_body).on('input', function() {
template.find('#custom_exclude_body').val(oai_settings.custom_exclude_body).on('input', function () {
oai_settings.custom_exclude_body = String($(this).val());
saveSettingsDebounced();
});
template.find('#custom_include_headers').val(oai_settings.custom_include_headers).on('input', function() {
template.find('#custom_include_headers').val(oai_settings.custom_include_headers).on('input', function () {
oai_settings.custom_include_headers = String($(this).val());
saveSettingsDebounced();
});
@@ -3928,6 +3947,11 @@ $(document).ready(async function () {
saveSettingsDebounced();
});
$('#continue_prefill').on('input', function () {
oai_settings.continue_prefill = !!$(this).prop('checked');
saveSettingsDebounced();
});
$('#seed_openai').on('input', function () {
oai_settings.seed = Number($(this).val());
saveSettingsDebounced();