adding prefill parameter to generateRaw() (#4266) * adding prefill parameter to generateRaw() * additional comments * Substitute params in prefill * Add prefill prompt for /genraw command * Fix param comment * fixing generateRaw param comment * fixing param comment --------- Co-authored-by: qvink <qvink@users.noreply.github.com> Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

556fb4ba087a432fea7667079194cbb7bc48aebc

qvink <191186569+qvink@users.noreply.github.com>

Signed
2 files changed, +31 -12Showing whitespace changes
public/script.js+17 -7
@@ -3058,10 +3058,11 @@ class StreamingProcessor {
30583058 * @param {string} api API to use.
30593059 * @param {boolean} instructOverride true to override instruct mode, false to use the default value
30603060 * @param {boolean} quietToLoud true to generate a message in system mode, false to generate a message in character mode
30613061 * @param {string} [systemPrompt] System prompt to use. Only Instruct mode or OpenAI.
3062+ * @param {string} [prefill] Prefill for the prompt.
30623063 * @returns {string | object[]} Prompt ready for use in generation. If using TC, this will be a string. If using CC, this will be an array of chat-style messages.
30633064 */
30643065export function createRawPrompt(prompt, api, instructOverride, quietToLoud, systemPrompt, prefill) {
30653066 const isInstruct = power_user.instruct.enabled && api !== 'openai' && api !== 'novel' && !instructOverride;
30663067
30673068 // If the prompt was given as a string, convert to a message-style object assuming user role
@@ -3074,6 +3075,9 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst
30743075 if (prompt.length === 0 && !systemPrompt) throw Error('No messages provided');
30753076 }
30763077
3078+ // Substitute the prefill if provided
3079+ prefill = substituteParams(prefill ?? '');
3080+
30773081 // Format each message in the prompt, accounting for the provided roles
30783082 for (const message of prompt) {
30793083 let name = '';
@@ -3096,12 +3100,17 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst
30963100 prompt.unshift({ role: 'system', content: systemPrompt });
30973101 }
30983102
30993103 // Ifwith textChat completionCompletion, convert tothe textprefill promptis byan concatenatingadditional allassistant message contentsat the end.
3104+ if (api === 'openai' && prefill) {
3105+ prompt.push({ role: 'assistant', content: prefill });
3106+ }
3107+
3108+ // if text completion, convert to text prompt by concatenating all message contents and adding the prefill as a promptBias.
31003109 if (api !== 'openai') {
31013110 const joiner = isInstruct ? '' : '\n';
31023111 prompt = prompt.map(message => message.content).join(joiner);
31033112 prompt = api === 'novel' ? adjustNovelInstructionPrompt(prompt) : prompt;
31043113 prompt = prompt + (isInstruct ? formatInstructModePrompt(name2, false, ''prefill, name1, name2, true, quietToLoud) : '`\n'${prefill}`); // add last line
31053114 }
31063115
31073116 return prompt;
@@ -3114,12 +3123,13 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst
31143123 * @param {string} api API to use. Main API is used if not specified.
31153124 * @param {boolean} instructOverride true to override instruct mode, false to use the default value
31163125 * @param {boolean} quietToLoud true to generate a message in system mode, false to generate a message in character mode
31173126 * @param {string} [systemPrompt] System prompt to use. Only Instruct mode or OpenAI.
31183127 * @param {number} [responseLength] Maximum response length. If unset, the global default value is used.
31193128 * @param {boolean} [trimNames] Whether to allow trimming "{{user}}:" and "{{char}}:" from the response.
3129+ * @param {string} [prefill] An optional prefill for the prompt.
31203130 * @returns {Promise<string>} Generated message
31213131 */
31223132export async function generateRaw(prompt, api, instructOverride, quietToLoud, systemPrompt, responseLength, trimNames = true, prefill = '') {
31233133 if (!api) {
31243134 api = main_api;
31253135 }
@@ -3129,7 +3139,7 @@ export async function generateRaw(prompt, api, instructOverride, quietToLoud, sy
31293139 let eventHook = () => { };
31303140
31313141 // construct final prompt from the input. Can either be a string or an array of chat-style messages.
31323142 prompt = createRawPrompt(prompt, api, instructOverride, quietToLoud, systemPrompt, prefill);
31333143
31343144 try {
31353145 if (responseLengthCustomized) {
public/scripts/slash-commands.js+14 -5
@@ -1672,17 +1672,18 @@ export function initDefaultSlashCommands() {
16721672 returns: 'generated text',
16731673 namedArgumentList: [
16741674 new SlashCommandNamedArgument(
16751675 'lock', 'lock user input during generation', [ARGUMENT_TYPE.BOOLEAN], false, false, null'off', commonEnumProviders.boolean('onOff')(),
16761676 ),
16771677 new SlashCommandNamedArgument(
16781678 'instruct', 'use instruct mode', [ARGUMENT_TYPE.BOOLEAN], false, false, 'on', commonEnumProviders.boolean('onOff')(),
16791679 ),
16801680 new SlashCommandNamedArgument(
16811681 'stop', 'one-time custom stop strings', [ARGUMENT_TYPE.LIST], false, false, '[]',
16821682 ),
16831683 SlashCommandNamedArgument.fromProps({
16841684 name: 'as',
16851685 description: 'role of the output prompt',
1686+ defaultValue: 'system',
16861687 typeList: [ARGUMENT_TYPE.STRING],
16871688 enumList: [
16881689 new SlashCommandEnumValue('system', null, enumTypes.enum, enumIcons.assistant),
@@ -1690,10 +1691,16 @@ export function initDefaultSlashCommands() {
16901691 ],
16911692 }),
16921693 new SlashCommandNamedArgument(
16931694 'system', 'system prompt at the start', [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.VARIABLE_NAME], false,
16941695 ),
16951696 new SlashCommandNamedArgument(
16961697 'lengthprefill', 'APIprefill responseprompt lengthat inthe tokensend', [ARGUMENT_TYPE.NUMBERSTRING, ARGUMENT_TYPE.VARIABLE_NAME], false,
1698+ ),
1699+ new SlashCommandNamedArgument(
1700+ 'length', 'API response length in tokens', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], false,
1701+ ),
1702+ new SlashCommandNamedArgument(
1703+ 'trim', 'trim {{user}} and {{char}} prefixes from the output', [ARGUMENT_TYPE.BOOLEAN], false, false, 'on', commonEnumProviders.boolean('onOff')(),
16971704 ),
16981705 ],
16991706 unnamedArgumentList: [
@@ -3628,7 +3635,9 @@ async function generateRawCallback(args, value) {
36283635 const as = args?.as || 'system';
36293636 const quietToLoud = as === 'char';
36303637 const systemPrompt = resolveVariable(args?.system) || '';
3638+ const prefillPrompt = resolveVariable(args?.prefill) || '';
36313639 const length = Number(resolveVariable(args?.length) ?? 0) || 0;
3640+ const trimNames = !isFalseBoolean(args?.trim);
36323641
36333642 try {
36343643 if (lock) {
@@ -3636,7 +3645,7 @@ async function generateRawCallback(args, value) {
36363645 }
36373646
36383647 setEphemeralStopStrings(resolveVariable(args?.stop));
36393648 const result = await generateRaw(value, '', isFalseBoolean(args?.instruct), quietToLoud, systemPrompt, length, trimNames, prefillPrompt);
36403649 return result;
36413650 } catch (err) {
36423651 console.error('Error on /genraw generation', err);