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>

7418d272a78c12d86afbd0f941c7590ba95ca0e5

Kristy Aurelia <120577588+KrsityKu@users.noreply.github.com>

Signed
2 files changed, +39 -17Showing whitespace changes
public/script.js+37 -17
@@ -3829,8 +3829,6 @@ export function createRawPrompt(prompt, api, instructOverride, quietToLoud, syst
3829}3829}
38303830
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 * @typedef {object} GenerateRawParams3832 * @typedef {object} GenerateRawParams
3835 * @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: ''}, ...]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 * @prop {string} [api] API to use. Main API is used if not specified.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 * @prop {boolean} [trimNames] Whether to allow trimming "{{user}}:" and "{{char}}:" from the response.3839 * @prop {boolean} [trimNames] Whether to allow trimming "{{user}}:" and "{{char}}:" from the response.
3842 * @prop {string} [prefill] An optional prefill for the prompt.3840 * @prop {string} [prefill] An optional prefill for the prompt.
3843 * @prop {object} [jsonSchema] JSON schema to use for the structured generation. Usually requires a special instruction.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 */
3847export 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 }
38523843
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 */
3850export async function generateRawData({ prompt = '', api = null, instructOverride = false, quietToLoud = false, systemPrompt = '', responseLength = null, prefill = '', jsonSchema = null } = {}) {
3853 if (!api) {3851 if (!api) {
3854 api = main_api;3852 api = main_api;
3855 }3853 }
@@ -3952,9 +3950,38 @@ export async function generateRaw({ prompt = '', api = null, instructOverride =
3952 return extractJsonFromData(data, { mainApi: api });3950 return extractJsonFromData(data, { mainApi: api });
3953 }3951 }
39543952
3953 return data;
3954 } finally {
3955 eventSource.removeListener(event_types.GENERATION_STOPPED, abortHook);
3956 if (responseLengthCustomized && TempResponseLength.isCustomized()) {
3957 TempResponseLength.restore(api);
3958 TempResponseLength.removeEventHook(api, eventHook);
3959 }
3960 }
3961}
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 */
3969export 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
3955 // format result, exclude user prompt bias3982 // format result, exclude user prompt bias
3956 const message = cleanUpMessage({3983 const message = cleanUpMessage({
3957 getMessage: extractMessageFromData(data),3984 getMessage: extractMessageFromData(data, api),
3958 isImpersonate: false,3985 isImpersonate: false,
3959 isContinue: false,3986 isContinue: false,
3960 displayIncompleteSentences: true,3987 displayIncompleteSentences: true,
@@ -3968,13 +3995,6 @@ export async function generateRaw({ prompt = '', api = null, instructOverride =
3968 }3995 }
39693996
3970 return message;3997 return message;
3971 } finally {
3972 eventSource.removeListener(event_types.GENERATION_STOPPED, abortHook);
3973 if (responseLengthCustomized && TempResponseLength.isCustomized()) {
3974 TempResponseLength.restore(api);
3975 TempResponseLength.removeEventHook(api, eventHook);
3976 }
3977 }
3978}3998}
39793999
3980class TempResponseLength {4000class TempResponseLength {
public/scripts/st-context.js+2 -0
@@ -53,6 +53,7 @@ import {
53 swipe_right,53 swipe_right,
54 swipe_left,54 swipe_left,
55 generateRaw,55 generateRaw,
56 generateRawData,
56 showSwipeButtons,57 showSwipeButtons,
57 hideSwipeButtons,58 hideSwipeButtons,
58 deleteMessage,59 deleteMessage,
@@ -195,6 +196,7 @@ export function getContext() {
195 getTokenizerModel,196 getTokenizerModel,
196 generateQuietPrompt,197 generateQuietPrompt,
197 generateRaw,198 generateRaw,
199 generateRawData,
198 writeExtensionField,200 writeExtensionField,
199 getThumbnailUrl,201 getThumbnailUrl,
200 selectCharacterById,202 selectCharacterById,