| 3053 | 3053 | } |
| 3054 | 3054 | |
| 3055 | 3055 | /** |
| 3056 | + * Constructs a prompt to be used for either Text Completion or Chat Completion. Input is format-agnostic. |
| 3057 | + * @param {string | object[]} prompt Input prompt. Can be a string or an array of chat-style messages, i.e. [{role: '', content: ''}, ...] |
| 3058 | + * @param {string} api API to use. |
| 3059 | + * @param {boolean} instructOverride true to override instruct mode, false to use the default value |
| 3060 | + * @param {boolean} quietToLoud true to generate a message in system mode, false to generate a message in character mode |
| 3061 | + * @param {string} [systemPrompt] System prompt to use. Only Instruct mode or OpenAI. |
| 3062 | + * @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. |
| 3063 | + */ |
| 3064 | +export function createRawPrompt(prompt, api, instructOverride, quietToLoud, systemPrompt) { |
| 3065 | + const isInstruct = power_user.instruct.enabled && api !== 'openai' && api !== 'novel' && !instructOverride; |
| 3066 | + |
| 3067 | + // If the prompt was given as a string, convert to a message-style object assuming user role |
| 3068 | + if (typeof prompt === 'string') { |
| 3069 | + const message = api === 'openai' |
| 3070 | + ? { role: 'user', content: prompt.trim() } |
| 3071 | + : { role: 'system', content: prompt }; |
| 3072 | + prompt = [message]; |
| 3073 | + } else { // checks for message-style object |
| 3074 | + if (prompt.length === 0 && !systemPrompt) throw Error('No messages provided'); |
| 3075 | + } |
| 3076 | + |
| 3077 | + // Format each message in the prompt, accounting for the provided roles |
| 3078 | + for (const message of prompt) { |
| 3079 | + let name = ''; |
| 3080 | + if (message.role === 'user') name = message.name ?? name1; |
| 3081 | + if (message.role === 'assistant') name = message.name ?? name2; |
| 3082 | + if (message.role === 'system') name = message.name ?? ''; |
| 3083 | + const prefix = isInstruct || api === 'openai' ? '' : (name ? `${name}: ` : ''); |
| 3084 | + message.content = prefix + substituteParams(message.content ?? ''); |
| 3085 | + if (isInstruct) { // instruct formatting for text completion |
| 3086 | + const isUser = message.role === 'user'; |
| 3087 | + const isNarrator = message.role === 'system'; |
| 3088 | + message.content = formatInstructModeChat(name, message.content, isUser, isNarrator, '', name1, name2, false); |
| 3089 | + } |
| 3090 | + } |
| 3091 | + |
| 3092 | + // prepend system prompt, if provided |
| 3093 | + if (systemPrompt) { |
| 3094 | + systemPrompt = substituteParams(systemPrompt); |
| 3095 | + systemPrompt = isInstruct ? (formatInstructModeSystemPrompt(systemPrompt) + '\n') : systemPrompt.trim(); |
| 3096 | + prompt.unshift({ role: 'system', content: systemPrompt }); |
| 3097 | + } |
| 3098 | + |
| 3099 | + // If text completion, convert to text prompt by concatenating all message contents |
| 3100 | + if (api !== 'openai') { |
| 3101 | + const joiner = isInstruct ? '' : '\n'; |
| 3102 | + prompt = prompt.map(message => message.content).join(joiner); |
| 3103 | + prompt = api === 'novel' ? adjustNovelInstructionPrompt(prompt) : prompt; |
| 3104 | + prompt = prompt + (isInstruct ? formatInstructModePrompt(name2, false, '', name1, name2, true, quietToLoud) : '\n'); // add last line |
| 3105 | + } |
| 3106 | + |
| 3107 | + return prompt; |
| 3108 | +} |
| 3109 | + |
| 3110 | +/** |
| 3056 | 3111 | * Generates a message using the provided prompt. |
| 3057 | | - * @param {string} prompt Prompt to generate a message from |
| 3112 | + * If the prompt is an array of chat-style messages and not using chat completion, it will be converted to a text prompt. |
| 3113 | + * @param {string | object[]} prompt Prompt to generate a message from. Can be a string or an array of chat-style messages, i.e. [{role: '', content: ''}, ...] |
| 3058 | 3114 | * @param {string} api API to use. Main API is used if not specified. |
| 3059 | 3115 | * @param {boolean} instructOverride true to override instruct mode, false to use the default value |
| 3060 | 3116 | * @param {boolean} quietToLoud true to generate a message in system mode, false to generate a message in character mode |