Merge branch 'staging' into sysprompt-divorce

0d294c5371a28bcd79abdf7977ef2c4b5e2f9045

Cohee <18619528+Cohee1207@users.noreply.github.com>

14 files changed, +164 -36Showing whitespace changes
Dockerfile+1 -1
@@ -19,7 +19,7 @@ ENV NODE_ENV=production
1919COPY package*.json post-install.js ./
2020RUN \
2121 echo "*** Install npm packages ***" && \
2222 npm i --no-audit --no-fund --quietloglevel=error --no-progress --omit=dev && npm cache clean --force
2323
2424# Bundle app source
2525COPY . ./
Start.bat+1 -1
@@ -1,7 +1,7 @@
11@echo off
22pushd %~dp0
33set NODE_ENV=production
44call npm install --no-audit --no-fund --quietloglevel=error --no-progress --omit=dev
55node server.js %*
66pause
77popd
UpdateAndStart.bat+1 -1
@@ -12,7 +12,7 @@ if %errorlevel% neq 0 (
1212 )
1313)
1414set NODE_ENV=production
1515call npm install --no-audit --no-fund --quietloglevel=error --no-progress --omit=dev
1616node server.js %*
1717pause
1818popd
UpdateForkAndStart.bat+1 -1
@@ -95,7 +95,7 @@ if %errorlevel% neq 0 (
9595
9696echo Installing npm packages and starting server
9797set NODE_ENV=production
9898call npm install --no-audit --no-fund --quietloglevel=error --no-progress --omit=dev
9999node server.js %*
100100
101101:end
public/css/promptmanager.css+12 -0
@@ -316,3 +316,15 @@
316316 margin-left: 0.5em;
317317 }
318318}
319+
320+.completion_prompt_manager_popup_entry_form_control:has(#completion_prompt_manager_popup_entry_form_prompt:disabled) > div:first-child::after {
321+ content: 'The content of this prompt is pulled from elsewhere and cannot be edited here.';
322+ display: block;
323+ width: 100%;
324+ font-weight: 600;
325+ text-align: center;
326+}
327+
328+.completion_prompt_manager_popup_entry_form_control #completion_prompt_manager_popup_entry_form_prompt:disabled {
329+ visibility: hidden;
330+}
public/index.html+2 -1
@@ -3297,7 +3297,8 @@
32973297 <option value="16">Command-R</option>
32983298 <option value="4">NerdStash (NovelAI Clio)</option>
32993299 <option value="5">NerdStash v2 (NovelAI Kayra)</option>
33003300 <option value="7">Mistral V1</option>
3301+ <option value="17">Mistral Nemo</option>
33013302 <option value="8">Yi</option>
33023303 <option value="11">Claude 1/2</option>
33033304 <option value="6">API (WebUI / koboldcpp)</option>
public/script.js+3 -5
@@ -6923,15 +6923,13 @@ export async function displayPastChats() {
69236923 }
69246924 // Check whether `text` {string} includes all of the `fragments` {string[]}.
69256925 function matchFragments(fragments, text) {
69266926 if (!text || !text.toLowerCase) {return false;
6927- return false;
6927+ return fragments.every(item => text.toLowerCase().includes(item));
6928- }
6929- return fragments.every(item => text.includes(item));
69306928 }
69316929 const fragments = makeQueryFragments(searchQuery);
69326930 // At least one chat message must match *all* the fragments.
69336931 // Currently, this doesn't match if the fragment matches are distributed across several chat messages.
69346932 return chatContent && Object.values(chatContent).some(message => matchFragments(fragments, message?.mes?.toLowerCase()));
69356933 });
69366934
69376935 console.debug(filteredData);
public/scripts/PromptManager.js+25 -4
@@ -427,12 +427,13 @@ class PromptManager {
427427
428428 document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_name').value = prompt.name;
429429 document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_role').value = 'system';
430430 document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt').value = prompt.content ?? '';
431431 document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position').value = prompt.injection_position ?? 0;
432432 document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth').value = prompt.injection_depth ?? DEFAULT_DEPTH;
433433 document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block').style.visibility = prompt.injection_position === INJECTION_POSITION.ABSOLUTE ? 'visible' : 'hidden';
434434 document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides').checked = prompt.forbid_overrides ?? false;
435435 document.getElementById(this.configuration.prefix + 'prompt_manager_forbid_overrides_block').style.visibility = this.overridablePrompts.includes(prompt.identifier) ? 'visible' : 'hidden';
436+ document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt').disabled = prompt.marker ?? false;
436437
437438 if (!this.systemPrompts.includes(promptId)) {
438439 document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position').removeAttribute('disabled');
@@ -920,7 +921,15 @@ class PromptManager {
920921 * @returns {boolean} True if the prompt can be edited, false otherwise.
921922 */
922923 isPromptEditAllowed(prompt) {
923- return !prompt.marker;
924+ const forceEditPrompts = [
925+ 'charDescription',
926+ 'charPersonality',
927+ 'scenario',
928+ 'personaDescription',
929+ 'worldInfoBefore',
930+ 'worldInfoAfter',
931+ ];
932+ return forceEditPrompts.includes(prompt.identifier) || !prompt.marker;
924933 }
925934
926935 /**
@@ -929,7 +938,17 @@ class PromptManager {
929938 * @returns {boolean} True if the prompt can be deleted, false otherwise.
930939 */
931940 isPromptToggleAllowed(prompt) {
932- const forceTogglePrompts = ['charDescription', 'charPersonality', 'scenario', 'personaDescription', 'worldInfoBefore', 'worldInfoAfter', 'main', 'chatHistory', 'dialogueExamples'];
941+ const forceTogglePrompts = [
942+ 'charDescription',
943+ 'charPersonality',
944+ 'scenario',
945+ 'personaDescription',
946+ 'worldInfoBefore',
947+ 'worldInfoAfter',
948+ 'main',
949+ 'chatHistory',
950+ 'dialogueExamples',
951+ ];
933952 return prompt.marker && !forceTogglePrompts.includes(prompt.identifier) ? false : !this.configuration.toggleDisabled.includes(prompt.identifier);
934953 }
935954
@@ -1182,8 +1201,9 @@ class PromptManager {
11821201 const forbidOverridesBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_forbid_overrides_block');
11831202
11841203 nameField.value = prompt.name ?? '';
11851204 roleField.value = prompt.role ?? 'system';
11861205 promptField.value = prompt.content ?? '';
1206+ promptField.disabled = prompt.marker ?? false;
11871207 injectionPositionField.value = prompt.injection_position ?? INJECTION_POSITION.RELATIVE;
11881208 injectionDepthField.value = prompt.injection_depth ?? DEFAULT_DEPTH;
11891209 injectionDepthBlock.style.visibility = prompt.injection_position === INJECTION_POSITION.ABSOLUTE ? 'visible' : 'hidden';
@@ -1279,6 +1299,7 @@ class PromptManager {
12791299 nameField.value = '';
12801300 roleField.selectedIndex = 0;
12811301 promptField.value = '';
1302+ promptField.disabled = false;
12821303 injectionPositionField.selectedIndex = 0;
12831304 injectionPositionField.removeAttribute('disabled');
12841305 injectionDepthField.value = DEFAULT_DEPTH;
public/scripts/openai.js+21 -3
@@ -970,6 +970,12 @@ async function populateChatCompletion(prompts, chatCompletion, { bias, quietProm
970970 }
971971
972972 const prompt = prompts.get(source);
973+
974+ if (prompt.injection_position === INJECTION_POSITION.ABSOLUTE) {
975+ promptManager.log(`Skipping prompt ${source} because it is an absolute prompt`);
976+ return;
977+ }
978+
973979 const index = target ? prompts.index(target) : prompts.index(source);
974980 const collection = new MessageCollection(source);
975981 collection.add(Message.fromPrompt(prompt));
@@ -1014,8 +1020,8 @@ async function populateChatCompletion(prompts, chatCompletion, { bias, quietProm
10141020 acc.push(prompt.identifier);
10151021 return acc;
10161022 }, []);
10171023 const userAbsolutePromptsabsolutePrompts = prompts.collection
10181024 .filter((prompt) => false === prompt.system_prompt && prompt.injection_position === INJECTION_POSITION.ABSOLUTE)
10191025 .reduce((acc, prompt) => {
10201026 acc.push(prompt);
10211027 return acc;
@@ -1080,7 +1086,7 @@ async function populateChatCompletion(prompts, chatCompletion, { bias, quietProm
10801086 }
10811087
10821088 // Add in-chat injections
10831089 messages = populationInjectionPrompts(userAbsolutePromptsabsolutePrompts, messages);
10841090
10851091 // Decide whether dialogue examples should always be added
10861092 if (power_user.pin_examples) {
@@ -1217,6 +1223,18 @@ function preparePromptsForChatCompletion({ Scenario, charPersonality, name2, wor
12171223
12181224 // Merge system prompts with prompt manager prompts
12191225 systemPrompts.forEach(prompt => {
1226+ const collectionPrompt = prompts.get(prompt.identifier);
1227+
1228+ // Apply system prompt role/depth overrides if they set in the prompt manager
1229+ if (collectionPrompt) {
1230+ // In-Chat / Relative
1231+ prompt.injection_position = collectionPrompt.injection_position ?? prompt.injection_position;
1232+ // Depth for In-Chat
1233+ prompt.injection_depth = collectionPrompt.injection_depth ?? prompt.injection_depth;
1234+ // Role (system, user, assistant)
1235+ prompt.role = collectionPrompt.role ?? prompt.role;
1236+ }
1237+
12201238 const newPrompt = promptManager.preparePrompt(prompt);
12211239 const markerIndex = prompts.index(prompt.identifier);
12221240
public/scripts/tokenizers.js+11 -0
@@ -30,6 +30,7 @@ export const tokenizers = {
3030 JAMBA: 14,
3131 QWEN2: 15,
3232 COMMAND_R: 16,
33+ NEMO: 17,
3334 BEST_MATCH: 99,
3435};
3536
@@ -43,6 +44,7 @@ export const ENCODE_TOKENIZERS = [
4344 tokenizers.JAMBA,
4445 tokenizers.QWEN2,
4546 tokenizers.COMMAND_R,
47+ tokenizers.NEMO,
4648 // uncomment when NovelAI releases Kayra and Clio weights, lol
4749 //tokenizers.NERD,
4850 //tokenizers.NERD2,
@@ -121,6 +123,11 @@ const TOKENIZER_URLS = {
121123 decode: '/api/tokenizers/command-r/decode',
122124 count: '/api/tokenizers/command-r/encode',
123125 },
126+ [tokenizers.NEMO]: {
127+ encode: '/api/tokenizers/nemo/encode',
128+ decode: '/api/tokenizers/nemo/decode',
129+ count: '/api/tokenizers/nemo/encode',
130+ },
124131 [tokenizers.API_TEXTGENERATIONWEBUI]: {
125132 encode: '/api/tokenizers/remote/textgenerationwebui/encode',
126133 count: '/api/tokenizers/remote/textgenerationwebui/encode',
@@ -535,6 +542,7 @@ export function getTokenizerModel() {
535542 const jambaTokenizer = 'jamba';
536543 const qwen2Tokenizer = 'qwen2';
537544 const commandRTokenizer = 'command-r';
545+ const nemoTokenizer = 'nemo';
538546
539547 // Assuming no one would use it for different models.. right?
540548 if (oai_settings.chat_completion_source == chat_completion_sources.SCALE) {
@@ -628,6 +636,9 @@ export function getTokenizerModel() {
628636 }
629637
630638 if (oai_settings.chat_completion_source == chat_completion_sources.MISTRALAI) {
639+ if (oai_settings.mistralai_model.includes('nemo') || oai_settings.mistralai_model.includes('pixtral')) {
640+ return nemoTokenizer;
641+ }
631642 return mistralTokenizer;
632643 }
633644
public/scripts/world-info.js+2 -2
@@ -4134,10 +4134,10 @@ export async function checkWorldInfo(chat, maxContext, isDryRun) {
41344134
41354135 switch (entry.position) {
41364136 case world_info_position.before:
41374137 WIBeforeEntries.unshift(substituteParams(content));
41384138 break;
41394139 case world_info_position.after:
41404140 WIAfterEntries.unshift(substituteParams(content));
41414141 break;
41424142 case world_info_position.EMTop:
41434143 EMEntries.unshift(
src/endpoints/chats.js+59 -16
@@ -92,8 +92,7 @@ function importOobaChat(userName, characterName, jsonData) {
9292 }
9393 }
9494
9595 const chatContent =return chat.map(obj => JSON.stringify(obj)).join('\n');
96- return chatContent;
9796}
9897
9998/**
@@ -121,8 +120,7 @@ function importAgnaiChat(userName, characterName, jsonData) {
121120 });
122121 }
123122
124123 const chatContent =return chat.map(obj => JSON.stringify(obj)).join('\n');
125- return chatContent;
126124}
127125
128126/**
@@ -159,6 +157,37 @@ function importCAIChat(userName, characterName, jsonData) {
159157 return newChats;
160158}
161159
160+/**
161+ * Flattens `msg` and `swipes` data from Chub Chat format.
162+ * Only changes enough to make it compatible with the standard chat serialization format.
163+ * @param {string} userName User name
164+ * @param {string} characterName Character name
165+ * @param {string[]} lines serialised JSONL data
166+ * @returns {string} Converted data
167+ */
168+function flattenChubChat(userName, characterName, lines) {
169+ function flattenSwipe(swipe) {
170+ return swipe.message ? swipe.message : swipe;
171+ }
172+
173+ function convert(line) {
174+ const lineData = tryParse(line);
175+ if (!lineData) return line;
176+
177+ if (lineData.mes && lineData.mes.message) {
178+ lineData.mes = lineData?.mes.message;
179+ }
180+
181+ if (lineData?.swipes && Array.isArray(lineData.swipes)) {
182+ lineData.swipes = lineData.swipes.map(swipe => flattenSwipe(swipe));
183+ }
184+
185+ return JSON.stringify(lineData);
186+ }
187+
188+ return (lines ?? []).map(convert).join('\n');
189+}
190+
162191const router = express.Router();
163192
164193router.post('/save', jsonParser, function (request, response) {
@@ -273,7 +302,7 @@ router.post('/export', jsonParser, async function (request, response) {
273302 }
274303 try {
275304 // Short path for JSONL files
276305 if (request.body.format === 'jsonl') {
277306 try {
278307 const rawFile = fs.readFileSync(filename, 'utf8');
279308 const successMessage = {
@@ -283,8 +312,7 @@ router.post('/export', jsonParser, async function (request, response) {
283312
284313 console.log(`Chat exported as ${exportfilename}`);
285314 return response.status(200).json(successMessage);
286- }
315+ } catch (err) {
287- catch (err) {
288316 console.error(err);
289317 const errorMessage = {
290318 message: `Could not read JSONL file to export. Source chat file: ${filename}.`,
@@ -319,8 +347,7 @@ router.post('/export', jsonParser, async function (request, response) {
319347 console.log(`Chat exported as ${exportfilename}`);
320348 return response.status(200).json(successMessage);
321349 });
322- }
350+ } catch (err) {
323- catch (err) {
324351 console.log('chat export failed.');
325352 console.log(err);
326353 return response.sendStatus(400);
@@ -396,20 +423,36 @@ router.post('/import', urlencodedParser, function (request, response) {
396423 }
397424
398425 if (format === 'jsonl') {
399426 constlet linelines = data.split('\n')[0];
427+ const header = lines[0];
428+
429+ const jsonData = JSON.parse(header);
430+
431+ if (!(jsonData.user_name !== undefined || jsonData.name !== undefined)) {
432+ console.log('Incorrect chat format .jsonl');
433+ return response.send({ error: true });
434+ }
400435
401- const jsonData = JSON.parse(line);
436+ // Do a tiny bit of work to import Chub Chat data
437+ // Processing the entire file is so fast that it's not worth checking if it's a Chub chat first
438+ let flattenedChat;
439+ try {
440+ // flattening is unlikely to break, but it's not worth failing to
441+ // import normal chats in an attempt to import a Chub chat
442+ flattenedChat = flattenChubChat(userName, characterName, lines);
443+ } catch (error) {
444+ console.warn('Failed to flatten Chub Chat data: ', error);
445+ }
402446
403- if (jsonData.user_name !== undefined || jsonData.name !== undefined) {
404447 const fileName = `${characterName} - ${humanizedISO8601DateTime()} imported.jsonl`;
405448 const filePath = path.join(request.user.directories.chats, avatarUrl, fileName);
449+ if (flattenedChat !== data) {
450+ writeFileAtomicSync(filePath, flattenedChat, 'utf8');
451+ } else {
406452 fs.copyFileSync(pathToUpload, filePath);
453+ }
407454 fs.unlinkSync(pathToUpload);
408455 response.send({ res: true });
409- } else {
410- console.log('Incorrect chat format .jsonl');
411- return response.send({ error: true });
412- }
413456 }
414457 } catch (error) {
415458 console.error(error);
src/endpoints/tokenizers.js+24 -0
@@ -221,6 +221,7 @@ const claude_tokenizer = new WebTokenizer('src/tokenizers/claude.json');
221221const llama3_tokenizer = new WebTokenizer('src/tokenizers/llama3.json');
222222const commandTokenizer = new WebTokenizer('https://github.com/SillyTavern/SillyTavern-Tokenizers/raw/main/command-r.json', 'src/tokenizers/llama3.json');
223223const qwen2Tokenizer = new WebTokenizer('https://github.com/SillyTavern/SillyTavern-Tokenizers/raw/main/qwen2.json', 'src/tokenizers/llama3.json');
224+const nemoTokenizer = new WebTokenizer('https://github.com/SillyTavern/SillyTavern-Tokenizers/raw/main/nemo.json', 'src/tokenizers/llama3.json');
224225
225226const sentencepieceTokenizers = [
226227 'llama',
@@ -418,6 +419,10 @@ function getTokenizerModel(requestModel) {
418419 return 'command-r';
419420 }
420421
422+ if (requestModel.includes('nemo')) {
423+ return 'nemo';
424+ }
425+
421426 // default
422427 return 'gpt-3.5-turbo';
423428}
@@ -645,6 +650,7 @@ router.post('/claude/encode', jsonParser, createWebTokenizerEncodingHandler(clau
645650router.post('/llama3/encode', jsonParser, createWebTokenizerEncodingHandler(llama3_tokenizer));
646651router.post('/qwen2/encode', jsonParser, createWebTokenizerEncodingHandler(qwen2Tokenizer));
647652router.post('/command-r/encode', jsonParser, createWebTokenizerEncodingHandler(commandTokenizer));
653+router.post('/nemo/encode', jsonParser, createWebTokenizerEncodingHandler(nemoTokenizer));
648654router.post('/llama/decode', jsonParser, createSentencepieceDecodingHandler(spp_llama));
649655router.post('/nerdstash/decode', jsonParser, createSentencepieceDecodingHandler(spp_nerd));
650656router.post('/nerdstash_v2/decode', jsonParser, createSentencepieceDecodingHandler(spp_nerd_v2));
@@ -657,6 +663,7 @@ router.post('/claude/decode', jsonParser, createWebTokenizerDecodingHandler(clau
657663router.post('/llama3/decode', jsonParser, createWebTokenizerDecodingHandler(llama3_tokenizer));
658664router.post('/qwen2/decode', jsonParser, createWebTokenizerDecodingHandler(qwen2Tokenizer));
659665router.post('/command-r/decode', jsonParser, createWebTokenizerDecodingHandler(commandTokenizer));
666+router.post('/nemo/decode', jsonParser, createWebTokenizerDecodingHandler(nemoTokenizer));
660667
661668router.post('/openai/encode', jsonParser, async function (req, res) {
662669 try {
@@ -707,6 +714,11 @@ router.post('/openai/encode', jsonParser, async function (req, res) {
707714 return handler(req, res);
708715 }
709716
717+ if (queryModel.includes('nemo')) {
718+ const handler = createWebTokenizerEncodingHandler(nemoTokenizer);
719+ return handler(req, res);
720+ }
721+
710722 const model = getTokenizerModel(queryModel);
711723 const handler = createTiktokenEncodingHandler(model);
712724 return handler(req, res);
@@ -765,6 +777,11 @@ router.post('/openai/decode', jsonParser, async function (req, res) {
765777 return handler(req, res);
766778 }
767779
780+ if (queryModel.includes('nemo')) {
781+ const handler = createWebTokenizerDecodingHandler(nemoTokenizer);
782+ return handler(req, res);
783+ }
784+
768785 const model = getTokenizerModel(queryModel);
769786 const handler = createTiktokenDecodingHandler(model);
770787 return handler(req, res);
@@ -835,6 +852,13 @@ router.post('/openai/count', jsonParser, async function (req, res) {
835852 return res.send({ 'token_count': num_tokens });
836853 }
837854
855+ if (model === 'nemo') {
856+ const instance = await nemoTokenizer.get();
857+ if (!instance) throw new Error('Failed to load the Nemo tokenizer');
858+ num_tokens = countWebTokenizerTokens(instance, req.body);
859+ return res.send({ 'token_count': num_tokens });
860+ }
861+
838862 const tokensPerName = queryModel.includes('gpt-3.5-turbo-0301') ? -1 : 1;
839863 const tokensPerMessage = queryModel.includes('gpt-3.5-turbo-0301') ? 4 : 3;
840864 const tokensPadding = 3;
start.sh+1 -1
@@ -26,7 +26,7 @@ fi
2626
2727echo "Installing Node Modules..."
2828export NODE_ENV=production
2929npm i --no-audit --no-fund --quietloglevel=error --no-progress --omit=dev
3030
3131echo "Entering SillyTavern..."
3232node "server.js" "$@"