Split generateRaw into two functions exposing generateRawData (#5249) * Split generateRaw into two functions exposing generateRawData * Concise types * Improve comments * Update public/script.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update public/script.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update public/script.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add GenerateRawParams typedef for improved documentation and clarity --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed| @@ -3829,8 +3829,6 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst | ||
| 3829 | 3829 | } |
| 3830 | 3830 | |
| 3831 | 3831 | /** |
| 3832 | - * Generates a message using the provided prompt. | |
| 3833 | - * If the prompt is an array of chat-style messages and not using chat completion, it will be converted to a text prompt. | |
| 3834 | 3832 | * @typedef {object} GenerateRawParams |
| 3835 | 3833 | * @prop {string | object[]} [prompt] Prompt to generate a message from. Can be a string or an array of chat-style messages, i.e. [{role: '', content: ''}, ...] |
| 3836 | 3834 | * @prop {string} [api] API to use. Main API is used if not specified. |
| @@ -3841,15 +3839,15 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst | ||
| 3841 | 3839 | * @prop {boolean} [trimNames] Whether to allow trimming "{{user}}:" and "{{char}}:" from the response. |
| 3842 | 3840 | * @prop {string} [prefill] An optional prefill for the prompt. |
| 3843 | 3841 | * @prop {object} [jsonSchema] JSON schema to use for the structured generation. Usually requires a special instruction. |
| 3844 | - * @param {GenerateRawParams} params Parameters for generating a message | |
| 3845 | - * @returns {Promise<string>} Generated message | |
| 3846 | 3842 | */ |
| 3847 | -export async function generateRaw({ prompt = '', api = null, instructOverride = false, quietToLoud = false, systemPrompt = '', responseLength = null, trimNames = true, prefill = '', jsonSchema = null } = {}) { | |
| 3848 | - if (arguments.length > 0 && typeof arguments[0] !== 'object') { | |
| 3849 | - console.trace('generateRaw called with positional arguments. Please use an object instead.'); | |
| 3850 | - [prompt, api, instructOverride, quietToLoud, systemPrompt, responseLength, trimNames, prefill, jsonSchema] = arguments; | |
| 3851 | - } | |
| 3852 | 3843 | |
| 3844 | +/** | |
| 3845 | + * Generates a raw data object using the provided prompt. | |
| 3846 | + * This used to be part of `generateRaw`, but separating it out allows extensions to access other data such as reasoning message. | |
| 3847 | + * @param {GenerateRawParams} params Parameters for generating a message | |
| 3848 | + * @returns {Promise<object | string>} Raw API response data, or a JSON string extracted from the response when `jsonSchema` is provided. | |
| 3849 | + */ | |
| 3850 | +export async function generateRawData({ prompt = '', api = null, instructOverride = false, quietToLoud = false, systemPrompt = '', responseLength = null, prefill = '', jsonSchema = null } = {}) { | |
| 3853 | 3851 | if (!api) { |
| 3854 | 3852 | api = main_api; |
| 3855 | 3853 | } |
| @@ -3952,22 +3950,7 @@ export async function generateRaw({ prompt = '', api = null, instructOverride = | ||
| 3952 | 3950 | return extractJsonFromData(data, { mainApi: api }); |
| 3953 | 3951 | } |
| 3954 | 3952 | |
| 3955 | - // format result, exclude user prompt bias | |
| 3953 | + return data; | |
| 3956 | - const message = cleanUpMessage({ | |
| 3957 | - getMessage: extractMessageFromData(data), | |
| 3958 | - isImpersonate: false, | |
| 3959 | - isContinue: false, | |
| 3960 | - displayIncompleteSentences: true, | |
| 3961 | - includeUserPromptBias: false, | |
| 3962 | - trimNames: trimNames, | |
| 3963 | - trimWrongNames: trimNames, | |
| 3964 | - }); | |
| 3965 | - | |
| 3966 | - if (!message) { | |
| 3967 | - throw new Error('No message generated'); | |
| 3968 | - } | |
| 3969 | - | |
| 3970 | - return message; | |
| 3971 | 3954 | } finally { |
| 3972 | 3955 | eventSource.removeListener(event_types.GENERATION_STOPPED, abortHook); |
| 3973 | 3956 | if (responseLengthCustomized && TempResponseLength.isCustomized()) { |
| @@ -3977,6 +3960,43 @@ export async function generateRaw({ prompt = '', api = null, instructOverride = | ||
| 3977 | 3960 | } |
| 3978 | 3961 | } |
| 3979 | 3962 | |
| 3963 | +/** | |
| 3964 | + * Generates a message using the provided prompt. | |
| 3965 | + * If the prompt is an array of chat-style messages and not using chat completion, it will be converted to a text prompt. | |
| 3966 | + * @param {GenerateRawParams} params Parameters for generating a message | |
| 3967 | + * @returns {Promise<string>} Generated output: a cleaned-up message string when `jsonSchema` is not provided, or an extracted JSON string conforming to `jsonSchema` when it is. | |
| 3968 | + */ | |
| 3969 | +export async function generateRaw({ prompt = '', api = null, instructOverride = false, quietToLoud = false, systemPrompt = '', responseLength = null, trimNames = true, prefill = '', jsonSchema = null } = {}) { | |
| 3970 | + if (arguments.length > 0 && typeof arguments[0] !== 'object') { | |
| 3971 | + console.trace('generateRaw called with positional arguments. Please use an object instead.'); | |
| 3972 | + [prompt, api, instructOverride, quietToLoud, systemPrompt, responseLength, trimNames, prefill, jsonSchema] = arguments; | |
| 3973 | + } | |
| 3974 | + | |
| 3975 | + const data = await generateRawData({ prompt, api, instructOverride, quietToLoud, systemPrompt, responseLength, prefill, jsonSchema }); | |
| 3976 | + | |
| 3977 | + // JSON string (matching the provided schema) will already be extracted. | |
| 3978 | + if (jsonSchema) { | |
| 3979 | + return data; | |
| 3980 | + } | |
| 3981 | + | |
| 3982 | + // format result, exclude user prompt bias | |
| 3983 | + const message = cleanUpMessage({ | |
| 3984 | + getMessage: extractMessageFromData(data, api), | |
| 3985 | + isImpersonate: false, | |
| 3986 | + isContinue: false, | |
| 3987 | + displayIncompleteSentences: true, | |
| 3988 | + includeUserPromptBias: false, | |
| 3989 | + trimNames: trimNames, | |
| 3990 | + trimWrongNames: trimNames, | |
| 3991 | + }); | |
| 3992 | + | |
| 3993 | + if (!message) { | |
| 3994 | + throw new Error('No message generated'); | |
| 3995 | + } | |
| 3996 | + | |
| 3997 | + return message; | |
| 3998 | +} | |
| 3999 | + | |
| 3980 | 4000 | class TempResponseLength { |
| 3981 | 4001 | static #originalResponseLength = -1; |
| 3982 | 4002 | static #lastApi = null; |
| @@ -53,6 +53,7 @@ import { | ||
| 53 | 53 | swipe_right, |
| 54 | 54 | swipe_left, |
| 55 | 55 | generateRaw, |
| 56 | + generateRawData, | |
| 56 | 57 | showSwipeButtons, |
| 57 | 58 | hideSwipeButtons, |
| 58 | 59 | deleteMessage, |
| @@ -195,6 +196,7 @@ export function getContext() { | ||
| 195 | 196 | getTokenizerModel, |
| 196 | 197 | generateQuietPrompt, |
| 197 | 198 | generateRaw, |
| 199 | + generateRawData, | |
| 198 | 200 | writeExtensionField, |
| 199 | 201 | getThumbnailUrl, |
| 200 | 202 | selectCharacterById, |