Facillitate extension use of ConnectionManagerRequestService (#4841) * Separate prompt-building functionality from request-sending functionality * removing logs and clarifying comments * separating parameter construction functionality to allow ConnectionManagerRequestService to use all other preset parameters * fixing chat completion issues, adding documentation to new functions. * Improving ConnectionManagerRequestService errors. Adding parseReasoningFromString option to override reasoning template. * Adjusting TextCompletionService prompt formatting * linting * Use settingsToUpdate to convert from OAI preset to OAI settings. * lint * throw errors when profile ID not found * Fix missed instances of global completion settings being used (CC and TC), replaced with optional argument. Specified typing for ChatCompletionSettings and TextCompletionSettings. * Adjusting parameters of parseReasoningFromString and adding getReasoningTemplateByName * using messages.role as a fallback for custom requests, fixing newline removal. * parameters => settings I like how it sounds better * ditto * You know I had to do it to 'em * Update getCustomTokenBans * Fix calculateLogitBias * Fix param attributes * Fix type checks * Less strict role type on ChatCompletionMessage * Add missing space * fixing getChatCompletionModel to use an arbitrary chat completion settings object * Fixing issues with preset overriding custom data passed. * Pass model to createGenerationParameters externally * Unify seed param handling for CHUTES * Fix non-existing CC source * Use strict comparison * Use global settings as a base for generation parameters creation * removing unnecessary handling of preset fields * don't pass preset prompts, use the passed payload override messages * refactoring text generation prompt building of last line * Pass model to getReasoningEffort * Pass model name to canPerformToolCalls * Pass model to createTextGenGenerationData --------- Co-authored-by: qvink <qvink@users.noreply.github.com> Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
Signed| @@ -146,6 +146,12 @@ declare global { | ||
| 146 | 146 | paused: boolean; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | + interface ChatCompletionMessage { | |
| 150 | + name?: string; | |
| 151 | + role: string; | |
| 152 | + content: string; | |
| 153 | + } | |
| 154 | + | |
| 149 | 155 | // Global namespace modules |
| 150 | 156 | interface Window { |
| 151 | 157 | ai: any; |
| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | import { getPresetManager } from './preset-manager.js'; |
| 2 | 2 | import { extractJsonFromData, extractMessageFromData, getGenerateUrl, getRequestHeaders, name1, name2 } from '../script.js'; |
| 3 | 3 | import { getTextGenServer, createTextGenGenerationData, setting_names, textgenerationwebui_settings } from './textgen-settings.js'; |
| 4 | 4 | import { extractReasoningFromData } from './reasoning.js'; |
| 5 | 5 | import { formatInstructModeChat, formatInstructModePrompt, getInstructStoppingSequences, names_behavior_types } from './instruct-mode.js'; |
| 6 | 6 | import { getStreamingReply, tryParseStreamingError, createGenerationParameters, settingsToUpdate, oai_settings } from './openai.js'; |
| 7 | 7 | import EventSourceStream from './sse-stream.js'; |
| 8 | 8 | |
| 9 | 9 | // #region Type Definitions |
| @@ -34,6 +34,7 @@ import EventSourceStream from './sse-stream.js'; | ||
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | 36 | * @typedef {Object} ChatCompletionMessage |
| 37 | + * @property {string} [name] - The name of the message author (optional) | |
| 37 | 38 | * @property {string} role - The role of the message author (e.g., "user", "assistant", "system") |
| 38 | 39 | * @property {string} content - The content of the message |
| 39 | 40 | */ |
| @@ -125,7 +126,7 @@ export class TextCompletionService { | ||
| 125 | 126 | |
| 126 | 127 | const json = await response.json(); |
| 127 | 128 | if (!response.ok || json.error) { |
| 128 | - throw json; | |
| 129 | + throw new Error(String(json.error?.message || 'Response not OK')); | |
| 129 | 130 | } |
| 130 | 131 | |
| 131 | 132 | if (!extractData) { |
| @@ -188,8 +189,87 @@ export class TextCompletionService { | ||
| 188 | 189 | } |
| 189 | 190 | |
| 190 | 191 | /** |
| 192 | + * Return a formatted prompt string given an array of messages, a chosen instruct preset, and instruct settings. | |
| 193 | + * @param {(ChatCompletionMessage & {ignoreInstruct?: boolean})[]} prompt An array of messages | |
| 194 | + * @param {InstructSettings|string} instructPreset Either the name of an instruct preset or the instruct preset object itself. | |
| 195 | + * @param {Partial<InstructSettings>} instructSettings Optional instruct settings | |
| 196 | + */ | |
| 197 | + static constructPrompt(prompt, instructPreset, instructSettings) { | |
| 198 | + // InstructPreset may either be a name or itself a preset | |
| 199 | + if (typeof instructPreset === 'string') { | |
| 200 | + const instructPresetManager = getPresetManager('instruct'); | |
| 201 | + instructPreset = instructPresetManager?.getCompletionPresetByName(instructPreset); | |
| 202 | + } | |
| 203 | + | |
| 204 | + // Clone the preset to avoid modifying the original | |
| 205 | + instructPreset = structuredClone(instructPreset); | |
| 206 | + if (instructSettings) { // apply any additional settings | |
| 207 | + Object.assign(instructPreset, instructSettings); | |
| 208 | + } | |
| 209 | + | |
| 210 | + // Make the type check shut up. We 100% don't have a string here. | |
| 211 | + if (typeof instructPreset === 'string') { | |
| 212 | + return; | |
| 213 | + } | |
| 214 | + | |
| 215 | + // Format messages using instruct formatting | |
| 216 | + const formattedMessages = []; | |
| 217 | + const prefillActive = prompt.length > 0 ? prompt[prompt.length - 1].role === 'assistant' : false; | |
| 218 | + for (const message of prompt) { | |
| 219 | + let messageContent = message.content; | |
| 220 | + if (!message.ignoreInstruct) { | |
| 221 | + const isLastMessage = message === prompt[prompt.length - 1]; | |
| 222 | + | |
| 223 | + // This complicated logic means: | |
| 224 | + // 1. If prefill is not active, format all messages | |
| 225 | + // 2. If prefill is active, format all messages except the last one | |
| 226 | + if (!isLastMessage || !prefillActive) { | |
| 227 | + messageContent = formatInstructModeChat( | |
| 228 | + message.name ?? message.role, | |
| 229 | + message.content, | |
| 230 | + message.role === 'user', | |
| 231 | + message.role === 'system', | |
| 232 | + undefined, | |
| 233 | + name1, // for macros | |
| 234 | + name2, // for macros | |
| 235 | + undefined, | |
| 236 | + instructPreset, | |
| 237 | + ); | |
| 238 | + } | |
| 239 | + | |
| 240 | + // Add prompt formatting for the last message. | |
| 241 | + // e.g. "<|im_start|>assistant" | |
| 242 | + if (isLastMessage) { | |
| 243 | + let last_line = formatInstructModePrompt( | |
| 244 | + 'assistant', // for sequences using {{name}} | |
| 245 | + false, // not an impersonation | |
| 246 | + prefillActive ? message.content : undefined, // if using prefill, last message is the prefill | |
| 247 | + name1, // for macros | |
| 248 | + name2, // for macros | |
| 249 | + true, // quiet | |
| 250 | + false, | |
| 251 | + instructPreset, | |
| 252 | + ); | |
| 253 | + | |
| 254 | + if (prefillActive) { // content is the prefilled message | |
| 255 | + if (last_line.endsWith('\n') && !message.content.endsWith('\n')) { | |
| 256 | + last_line = last_line.slice(0, -1); // remove newline after prefill if it's not in the prefill itself | |
| 257 | + } | |
| 258 | + messageContent = last_line; | |
| 259 | + } else { // append last line to content (e.g. "<|im_start|>assistant:") | |
| 260 | + messageContent += last_line; | |
| 261 | + } | |
| 262 | + } | |
| 263 | + } | |
| 264 | + formattedMessages.push(messageContent); | |
| 265 | + } | |
| 266 | + return formattedMessages.join(''); | |
| 267 | + } | |
| 268 | + | |
| 269 | + | |
| 270 | + /** | |
| 191 | 271 | * Process and send a text completion request with optional preset & instruct |
| 192 | - * @param {Record<string, any> & TextCompletionRequestBase & {prompt: (ChatCompletionMessage & {ignoreInstruct?: boolean})[] |string}} custom | |
| 272 | + * @param {TextCompletionPayload} requestData | |
| 193 | 273 | * @param {Object} options - Configuration options |
| 194 | 274 | * @param {string?} [options.presetName] - Name of the preset to use for generation settings |
| 195 | 275 | * @param {string?} [options.instructName] - Name of instruct preset for message formatting |
| @@ -199,15 +279,35 @@ export class TextCompletionService { | ||
| 199 | 279 | * @returns {Promise<ExtractedData | (() => AsyncGenerator<StreamResponse>)>} If not streaming, returns extracted data; if streaming, returns a function that creates an AsyncGenerator |
| 200 | 280 | * @throws {Error} |
| 201 | 281 | */ |
| 202 | - static async processRequest( | |
| 282 | + static async processRequest(requestData, options = {}, extractData = true, signal = null) { | |
| 203 | - custom, | |
| 204 | - options = {}, | |
| 205 | - extractData = true, | |
| 206 | - signal = null, | |
| 207 | - ) { | |
| 208 | 283 | const { presetName, instructName } = options; |
| 209 | - let requestData = { ...custom }; | |
| 284 | + | |
| 210 | - const prompt = custom.prompt; | |
| 285 | + // remove any undefined params in given request data | |
| 286 | + requestData = this.createRequestData(requestData); | |
| 287 | + | |
| 288 | + /** @type {InstructSettings | undefined} */ | |
| 289 | + let instructPreset; | |
| 290 | + const prompt = requestData.prompt; | |
| 291 | + // Handle instruct formatting if requested | |
| 292 | + if (Array.isArray(prompt)) { | |
| 293 | + if (instructName) { | |
| 294 | + const instructPresetManager = getPresetManager('instruct'); | |
| 295 | + instructPreset = instructPresetManager?.getCompletionPresetByName(instructName); | |
| 296 | + if (instructPreset) { | |
| 297 | + requestData.prompt = this.constructPrompt(prompt, instructPreset, options.instructSettings); | |
| 298 | + const stoppingStrings = getInstructStoppingSequences({ customInstruct: instructPreset, useStopStrings: false }); | |
| 299 | + requestData.stop = stoppingStrings; | |
| 300 | + requestData.stopping_strings = stoppingStrings; | |
| 301 | + } else { | |
| 302 | + console.warn(`Instruct preset "${instructName}" not found, using basic formatting`); | |
| 303 | + requestData.prompt = prompt.map(x => x.content).join('\n\n'); | |
| 304 | + } | |
| 305 | + } else { | |
| 306 | + requestData.prompt = prompt.map(x => x.content).join('\n\n'); | |
| 307 | + } | |
| 308 | + } else if (typeof prompt === 'string') { | |
| 309 | + requestData.prompt = prompt; | |
| 310 | + } | |
| 211 | 311 | |
| 212 | 312 | // Apply generation preset if specified |
| 213 | 313 | if (presetName) { |
| @@ -215,9 +315,8 @@ export class TextCompletionService { | ||
| 215 | 315 | if (presetManager) { |
| 216 | 316 | const preset = presetManager.getCompletionPresetByName(presetName); |
| 217 | 317 | if (preset) { |
| 218 | 318 | // Convert preset to payload and merge with custom parametersdata |
| 219 | 319 | const presetPayloadrequestData = this.presetToGeneratePayload(preset, {}, requestData); |
| 220 | - requestData = { ...presetPayload, ...requestData }; | |
| 221 | 320 | } else { |
| 222 | 321 | console.warn(`Preset "${presetName}" not found, continuing with default settings`); |
| 223 | 322 | } |
| @@ -226,99 +325,10 @@ export class TextCompletionService { | ||
| 226 | 325 | } |
| 227 | 326 | } |
| 228 | 327 | |
| 328 | + const response = await this.sendRequest(requestData, extractData, signal); | |
| 229 | 329 | |
| 230 | - /** @type {InstructSettings | undefined} */ | |
| 231 | - let instructPreset; | |
| 232 | - // Handle instruct formatting if requested | |
| 233 | - if (Array.isArray(prompt) && instructName) { | |
| 234 | - const instructPresetManager = getPresetManager('instruct'); | |
| 235 | - instructPreset = instructPresetManager?.getCompletionPresetByName(instructName); | |
| 236 | - if (instructPreset) { | |
| 237 | - // Clone the preset to avoid modifying the original | |
| 238 | - instructPreset = structuredClone(instructPreset); | |
| 239 | - instructPreset.names_behavior = names_behavior_types.NONE; | |
| 240 | - if (options.instructSettings) { | |
| 241 | - Object.assign(instructPreset, options.instructSettings); | |
| 242 | - } | |
| 243 | - | |
| 244 | - // Format messages using instruct formatting | |
| 245 | - const formattedMessages = []; | |
| 246 | - const prefillActive = prompt.length > 0 ? prompt[prompt.length - 1].role === 'assistant' : false; | |
| 247 | - for (const message of prompt) { | |
| 248 | - let messageContent = message.content; | |
| 249 | - if (!message.ignoreInstruct) { | |
| 250 | - const isLastMessage = message === prompt[prompt.length - 1]; | |
| 251 | - | |
| 252 | - // This complicated logic means: | |
| 253 | - // 1. If prefill is not active, format all messages | |
| 254 | - // 2. If prefill is active, format all messages except the last one | |
| 255 | - if (!isLastMessage || !prefillActive) { | |
| 256 | - messageContent = formatInstructModeChat( | |
| 257 | - message.role, | |
| 258 | - message.content, | |
| 259 | - message.role === 'user', | |
| 260 | - message.role === 'system', | |
| 261 | - undefined, | |
| 262 | - undefined, | |
| 263 | - undefined, | |
| 264 | - undefined, | |
| 265 | - instructPreset, | |
| 266 | - ); | |
| 267 | - } | |
| 268 | - | |
| 269 | - // Add prompt formatting for the last message. | |
| 270 | - if (isLastMessage) { | |
| 271 | - if (!prefillActive) { // e.g. "<|im_start|>user:" | |
| 272 | - messageContent += formatInstructModePrompt( | |
| 273 | - undefined, | |
| 274 | - false, | |
| 275 | - undefined, | |
| 276 | - undefined, | |
| 277 | - undefined, | |
| 278 | - false, | |
| 279 | - false, | |
| 280 | - instructPreset, | |
| 281 | - ); | |
| 282 | - } else { // e.g. "<|im_start|>assistant: Hello, my name is" | |
| 283 | - const overriddenInstructPreset = structuredClone(instructPreset); | |
| 284 | - overriddenInstructPreset.output_suffix = ''; | |
| 285 | - overriddenInstructPreset.wrap = false; | |
| 286 | - messageContent = formatInstructModeChat( | |
| 287 | - message.role, | |
| 288 | - message.content, | |
| 289 | - false, // since it is assistant | |
| 290 | - false, | |
| 291 | - undefined, | |
| 292 | - undefined, | |
| 293 | - undefined, | |
| 294 | - undefined, | |
| 295 | - overriddenInstructPreset, | |
| 296 | - ); | |
| 297 | - } | |
| 298 | - } | |
| 299 | - } | |
| 300 | - formattedMessages.push(messageContent); | |
| 301 | - } | |
| 302 | - requestData.prompt = formattedMessages.join(''); | |
| 303 | - const stoppingStrings = getInstructStoppingSequences({ customInstruct: instructPreset, useStopStrings: false }); | |
| 304 | - requestData.stop = stoppingStrings; | |
| 305 | - requestData.stopping_strings = stoppingStrings; | |
| 306 | - } else { | |
| 307 | - console.warn(`Instruct preset "${instructName}" not found, using basic formatting`); | |
| 308 | - requestData.prompt = prompt.map(x => x.content).join('\n\n'); | |
| 309 | - } | |
| 310 | - } else if (typeof prompt === 'string') { | |
| 311 | - requestData.prompt = prompt; | |
| 312 | - } else { | |
| 313 | - requestData.prompt = prompt.map(x => x.content).join('\n\n'); | |
| 314 | - } | |
| 315 | - | |
| 316 | - // @ts-ignore | |
| 317 | - const data = this.createRequestData(requestData); | |
| 318 | - | |
| 319 | - const response = await this.sendRequest(data, extractData, signal); | |
| 320 | 330 | // Remove stopping strings from the end |
| 321 | 331 | if (!datarequestData.stream && extractData) { |
| 322 | 332 | /** @type {ExtractedData} */ |
| 323 | 333 | // @ts-ignore |
| 324 | 334 | const extractedData = response; |
| @@ -377,31 +387,30 @@ export class TextCompletionService { | ||
| 377 | 387 | * Converts a preset to a valid text completion payload. |
| 378 | 388 | * Only supports temperature. |
| 379 | 389 | * @param {Object} preset - The preset configuration |
| 380 | 390 | * @param {Object} customPresetoverridePreset - Additional parameters to override preset values |
| 391 | + * @param {Object} overridePayload - Additional parameters to override payload values | |
| 381 | 392 | * @returns {Object} - Formatted payload for text completion API |
| 382 | 393 | */ |
| 383 | 394 | static presetToGeneratePayload(preset, customPresetoverridePreset = {}, overridePayload = {}) { |
| 384 | 395 | if (!preset || typeof preset !== 'object') { |
| 385 | 396 | throw new Error('Invalid preset: must be an object'); |
| 386 | 397 | } |
| 387 | 398 | |
| 388 | 399 | // Mergeapply preset with custom parametersoverrides |
| 389 | 400 | const settingspreset = { ...preset, ...customPresetoverridePreset }; |
| 390 | 401 | |
| 391 | - // Initialize base payload with common parameters | |
| 402 | + // Only take fields from the preset specified in setting_names to use as TextCompletionSettings | |
| 392 | 403 | letconst payloadsettings = {structuredClone(textgenerationwebui_settings); |
| 393 | - 'temperature': settings.temp >= 0 ? Number(settings.temp) : undefined, | |
| 404 | + for (const [key, value] of Object.entries(preset)) { | |
| 394 | - 'min_p': settings.min_p >= 0 ? Number(settings.min_p) : undefined, | |
| 405 | + if (!setting_names.includes(key)) continue; | |
| 395 | - }; | |
| 406 | + settings[key] = value; | |
| 407 | + } | |
| 396 | 408 | |
| 397 | 409 | // Remove undefined valuesconvert to avoida APIgeneration errorspayload |
| 398 | - Object.keys(payload).forEach(key => { | |
| 410 | + const payload = createTextGenGenerationData(settings, overridePayload.model, overridePayload.prompt, preset.genamt); | |
| 399 | - if (payload[key] === undefined) { | |
| 400 | - delete payload[key]; | |
| 401 | - } | |
| 402 | - }); | |
| 403 | 411 | |
| 404 | - return payload; | |
| 412 | + // apply overrides | |
| 413 | + return this.createRequestData({ ...payload, ...overridePayload }); | |
| 405 | 414 | } |
| 406 | 415 | } |
| 407 | 416 | |
| @@ -461,7 +470,7 @@ export class ChatCompletionService { | ||
| 461 | 470 | if (!data.stream) { |
| 462 | 471 | const json = await response.json(); |
| 463 | 472 | if (!response.ok || json.error) { |
| 464 | - throw json; | |
| 473 | + throw new Error(String(json.error?.message || 'Response not OK')); | |
| 465 | 474 | } |
| 466 | 475 | |
| 467 | 476 | if (!extractData) { |
| @@ -523,7 +532,7 @@ export class ChatCompletionService { | ||
| 523 | 532 | |
| 524 | 533 | /** |
| 525 | 534 | * Process and send a chat completion request with optional preset |
| 526 | 535 | * @param {ChatCompletionPayload} customrequestData - payload data, overriding preset if given |
| 527 | 536 | * @param {Object} options - Configuration options |
| 528 | 537 | * @param {string?} [options.presetName] - Name of the preset to use for generation settings |
| 529 | 538 | * @param {boolean} [extractData=true] - Whether to extract structured data from response |
| @@ -531,9 +540,9 @@ export class ChatCompletionService { | ||
| 531 | 540 | * @returns {Promise<ExtractedData | (() => AsyncGenerator<StreamResponse>)>} If not streaming, returns extracted data; if streaming, returns a function that creates an AsyncGenerator |
| 532 | 541 | * @throws {Error} |
| 533 | 542 | */ |
| 534 | 543 | static async processRequest(customrequestData, options, extractData = true, signal = null) { |
| 535 | 544 | const { presetName } = options; |
| 536 | 545 | let requestData = { ..this.custom }createRequestData(requestData); |
| 537 | 546 | |
| 538 | 547 | // Apply generation preset if specified |
| 539 | 548 | if (presetName) { |
| @@ -542,8 +551,7 @@ export class ChatCompletionService { | ||
| 542 | 551 | const preset = presetManager.getCompletionPresetByName(presetName); |
| 543 | 552 | if (preset) { |
| 544 | 553 | // Convert preset to payload and merge with custom parameters |
| 545 | 554 | const presetPayloadrequestData = await this.presetToGeneratePayload(preset, {}, requestData); |
| 546 | - requestData = { ...presetPayload, ...requestData }; | |
| 547 | 555 | } else { |
| 548 | 556 | console.warn(`Preset "${presetName}" not found, continuing with default settings`); |
| 549 | 557 | } |
| @@ -552,38 +560,41 @@ export class ChatCompletionService { | ||
| 552 | 560 | } |
| 553 | 561 | } |
| 554 | 562 | |
| 555 | 563 | const datareturn =await this.createRequestDatasendRequest(requestData, extractData, signal); |
| 556 | - | |
| 557 | - return await this.sendRequest(data, extractData, signal); | |
| 558 | 564 | } |
| 559 | 565 | |
| 560 | 566 | /** |
| 561 | 567 | * Converts a preset to a valid chat completion payload |
| 562 | 568 | * Only supports temperature. |
| 563 | 569 | * @param {Object} preset - The preset configuration |
| 564 | 570 | * @param {Object} customParamsoverridePreset - Additional parameters to override preset values |
| 565 | 571 | * @returnsparam {Object} overridePayload - FormattedAdditional payloadparameters forto chatoverride completionpayload APIvalues |
| 572 | + * @returns {Promise<any>} - Formatted payload for chat completion API | |
| 566 | 573 | */ |
| 567 | 574 | static async presetToGeneratePayload(preset, customParamsoverridePreset = {}, overridePayload = {}) { |
| 568 | 575 | if (!preset || typeof preset !== 'object') { |
| 569 | 576 | throw new Error('Invalid preset: must be an object'); |
| 570 | 577 | } |
| 571 | 578 | |
| 572 | 579 | // Mergeapply preset with custom parametersoverrides |
| 573 | 580 | const settingspreset = { ...preset, ...customParamsoverridePreset }; |
| 574 | 581 | |
| 575 | 582 | // InitializeFix baseany payloadfields withbefore commonconverting parametersto settings |
| 576 | - const payload = { | |
| 583 | + preset.bias_preset_selected = preset.bias_presets !== undefined ? preset.bias_preset_selected : undefined; // presets might have bias_preset_selected but not bias_presets, but settings need both or neither. | |
| 577 | - temperature: settings.temperature >= 0 ? Number(settings.temperature) : undefined, | |
| 578 | - }; | |
| 579 | 584 | |
| 580 | 585 | // RemoveConvert undefinedfrom valuespreset to avoid API errorsChatCompletionSettings |
| 581 | - Object.keys(payload).forEach(key => { | |
| 586 | + const settings = structuredClone(oai_settings); | |
| 582 | - if (payload[key] === undefined) { | |
| 587 | + for (const [key, value] of Object.entries(preset)) { | |
| 583 | 588 | delete const payloadsettingToUpdate = settingsToUpdate[key]; |
| 584 | - } | |
| 589 | + if (!settingToUpdate) continue; | |
| 585 | - }); | |
| 590 | + settings[settingToUpdate[1]] = value; | |
| 591 | + } | |
| 586 | 592 | |
| 587 | - return payload; | |
| 593 | + // Convert from settings to generation payload | |
| 594 | + const data = await createGenerationParameters(settings, overridePayload.model, 'quiet', overridePayload.messages); | |
| 595 | + const payload = data.generate_data; | |
| 596 | + | |
| 597 | + // apply overrides | |
| 598 | + return this.createRequestData({ ...payload, ...overridePayload }); | |
| 588 | 599 | } |
| 589 | 600 | } |
| @@ -397,7 +397,7 @@ export class ConnectionManagerRequestService { | ||
| 397 | 397 | throw new Error('Connection Manager is not available'); |
| 398 | 398 | } |
| 399 | 399 | |
| 400 | - const profile = context.extensionSettings.connectionManager.profiles.find((p) => p.id === profileId); | |
| 400 | + const profile = this.getProfile(profileId); | |
| 401 | 401 | const selectedApiMap = this.validateProfile(profile); |
| 402 | 402 | |
| 403 | 403 | try { |
| @@ -454,6 +454,38 @@ export class ConnectionManagerRequestService { | ||
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | /** |
| 457 | + * If using text completion, return a formatted prompt string given an array of messages, a given profile ID, and optional instruct settings. | |
| 458 | + * If using chat completion, simply return the given prompt as-is. | |
| 459 | + * @param {ChatCompletionMessage[]} prompt An array of prompt messages. | |
| 460 | + * @param {string} profileId ID of a given connection profile (from which to infer a completion preset). | |
| 461 | + * @param {InstructSettings} instructSettings optional instruct settings | |
| 462 | + */ | |
| 463 | + static constructPrompt(prompt, profileId, instructSettings = null) { | |
| 464 | + const context = SillyTavern.getContext(); | |
| 465 | + const profile = this.getProfile(profileId); | |
| 466 | + const selectedApiMap = this.validateProfile(profile); | |
| 467 | + const instructName = profile.instruct; | |
| 468 | + | |
| 469 | + switch (selectedApiMap.selected) { | |
| 470 | + case 'openai': { | |
| 471 | + if (!selectedApiMap.source) { | |
| 472 | + throw new Error(`API type ${selectedApiMap.selected} does not support chat completions`); | |
| 473 | + } | |
| 474 | + return prompt; | |
| 475 | + } | |
| 476 | + case 'textgenerationwebui': { | |
| 477 | + if (!selectedApiMap.type) { | |
| 478 | + throw new Error(`API type ${selectedApiMap.selected} does not support text completions`); | |
| 479 | + } | |
| 480 | + return context.TextCompletionService.constructPrompt(prompt, instructName, instructSettings); | |
| 481 | + } | |
| 482 | + default: { | |
| 483 | + throw new Error(`Unknown API type ${selectedApiMap.selected}`); | |
| 484 | + } | |
| 485 | + } | |
| 486 | + } | |
| 487 | + | |
| 488 | + /** | |
| 457 | 489 | * Respects allowed types. |
| 458 | 490 | * @returns {import('./connection-manager/index.js').ConnectionProfile[]} |
| 459 | 491 | */ |
| @@ -468,6 +500,18 @@ export class ConnectionManagerRequestService { | ||
| 468 | 500 | } |
| 469 | 501 | |
| 470 | 502 | /** |
| 503 | + * Return profile data given the profile ID | |
| 504 | + * @param {string} profileId | |
| 505 | + * @returns {import('./connection-manager/index.js').ConnectionProfile?} [profile] | |
| 506 | + * @throws {Error} | |
| 507 | + */ | |
| 508 | + static getProfile(profileId) { | |
| 509 | + const profile = SillyTavern.getContext().extensionSettings.connectionManager.profiles.find((p) => p.id === profileId); | |
| 510 | + if (!profile) throw new Error(`Profile not found (ID: ${profileId})`); | |
| 511 | + return profile; | |
| 512 | + } | |
| 513 | + | |
| 514 | + /** | |
| 471 | 515 | * @param {import('./connection-manager/index.js').ConnectionProfile?} [profile] |
| 472 | 516 | * @returns {boolean} |
| 473 | 517 | */ |
| @@ -18,7 +18,6 @@ import { | ||
| 18 | 18 | getMediaDisplay, |
| 19 | 19 | getMediaIndex, |
| 20 | 20 | getRequestHeaders, |
| 21 | - getStoppingStrings, | |
| 22 | 21 | is_send_press, |
| 23 | 22 | main_api, |
| 24 | 23 | name1, |
| @@ -1565,62 +1564,63 @@ function checkModerationError(data, { quiet = false } = {}) { | ||
| 1565 | 1564 | |
| 1566 | 1565 | /** |
| 1567 | 1566 | * Gets the API model for the selected chat completion source. |
| 1568 | 1567 | * @param {stringChatCompletionSettings} source If it's set,settings ignoresChat activecompletion sourcesettings |
| 1569 | 1568 | * @returns {string} API model |
| 1570 | 1569 | */ |
| 1571 | 1570 | export function getChatCompletionModel(sourcesettings = null) { |
| 1572 | 1571 | const activeSourcesettings = sourcesettings ?? oai_settings.chat_completion_source; |
| 1573 | - switch (activeSource) { | |
| 1572 | + const source = settings.chat_completion_source; | |
| 1573 | + switch (source) { | |
| 1574 | 1574 | case chat_completion_sources.CLAUDE: |
| 1575 | 1575 | return oai_settingssettings.claude_model; |
| 1576 | 1576 | case chat_completion_sources.OPENAI: |
| 1577 | 1577 | return oai_settingssettings.openai_model; |
| 1578 | 1578 | case chat_completion_sources.MAKERSUITE: |
| 1579 | 1579 | return oai_settingssettings.google_model; |
| 1580 | 1580 | case chat_completion_sources.VERTEXAI: |
| 1581 | 1581 | return oai_settingssettings.vertexai_model; |
| 1582 | 1582 | case chat_completion_sources.OPENROUTER: |
| 1583 | 1583 | return oai_settingssettings.openrouter_model !== openrouter_website_model ? oai_settingssettings.openrouter_model : null; |
| 1584 | 1584 | case chat_completion_sources.AI21: |
| 1585 | 1585 | return oai_settingssettings.ai21_model; |
| 1586 | 1586 | case chat_completion_sources.MISTRALAI: |
| 1587 | 1587 | return oai_settingssettings.mistralai_model; |
| 1588 | 1588 | case chat_completion_sources.CUSTOM: |
| 1589 | 1589 | return oai_settingssettings.custom_model; |
| 1590 | 1590 | case chat_completion_sources.COHERE: |
| 1591 | 1591 | return oai_settingssettings.cohere_model; |
| 1592 | 1592 | case chat_completion_sources.PERPLEXITY: |
| 1593 | 1593 | return oai_settingssettings.perplexity_model; |
| 1594 | 1594 | case chat_completion_sources.GROQ: |
| 1595 | 1595 | return oai_settingssettings.groq_model; |
| 1596 | 1596 | case chat_completion_sources.SILICONFLOW: |
| 1597 | 1597 | return oai_settingssettings.siliconflow_model; |
| 1598 | 1598 | case chat_completion_sources.ELECTRONHUB: |
| 1599 | 1599 | return oai_settingssettings.electronhub_model; |
| 1600 | 1600 | case chat_completion_sources.CHUTES: |
| 1601 | 1601 | return oai_settingssettings.chutes_model; |
| 1602 | 1602 | case chat_completion_sources.NANOGPT: |
| 1603 | 1603 | return oai_settingssettings.nanogpt_model; |
| 1604 | 1604 | case chat_completion_sources.DEEPSEEK: |
| 1605 | 1605 | return oai_settingssettings.deepseek_model; |
| 1606 | 1606 | case chat_completion_sources.AIMLAPI: |
| 1607 | 1607 | return oai_settingssettings.aimlapi_model; |
| 1608 | 1608 | case chat_completion_sources.XAI: |
| 1609 | 1609 | return oai_settingssettings.xai_model; |
| 1610 | 1610 | case chat_completion_sources.POLLINATIONS: |
| 1611 | 1611 | return oai_settingssettings.pollinations_model; |
| 1612 | 1612 | case chat_completion_sources.COMETAPI: |
| 1613 | 1613 | return oai_settingssettings.cometapi_model; |
| 1614 | 1614 | case chat_completion_sources.MOONSHOT: |
| 1615 | 1615 | return oai_settingssettings.moonshot_model; |
| 1616 | 1616 | case chat_completion_sources.FIREWORKS: |
| 1617 | 1617 | return oai_settingssettings.fireworks_model; |
| 1618 | 1618 | case chat_completion_sources.AZURE_OPENAI: |
| 1619 | 1619 | return oai_settingssettings.azure_openai_model; |
| 1620 | 1620 | case chat_completion_sources.ZAI: |
| 1621 | 1621 | return oai_settingssettings.zai_model; |
| 1622 | 1622 | default: |
| 1623 | 1623 | console.error(`Unknown chat completion source: ${activeSourcesource}`); |
| 1624 | 1624 | return ''; |
| 1625 | 1625 | } |
| 1626 | 1626 | } |
| @@ -2300,7 +2300,16 @@ function getAimlapiModelTemplate(option) { | ||
| 2300 | 2300 | `)); |
| 2301 | 2301 | } |
| 2302 | 2302 | |
| 2303 | -function getReasoningEffort() { | |
| 2303 | +/** | |
| 2304 | + * Get the reasoning effort from chat completion settings | |
| 2305 | + * @param {ChatCompletionSettings} settings Chat completion settings | |
| 2306 | + * @param {string} model Model name (optional, used for ElectronHub) | |
| 2307 | + * @returns {string} Reasoning effort, if present | |
| 2308 | + */ | |
| 2309 | +function getReasoningEffort(settings = null, model = null) { | |
| 2310 | + settings = settings ?? oai_settings; | |
| 2311 | + model = model ?? getChatCompletionModel(settings); | |
| 2312 | + | |
| 2304 | 2313 | // These sources expect the effort as string. |
| 2305 | 2314 | const reasoningEffortSources = [ |
| 2306 | 2315 | chat_completion_sources.OPENAI, |
| @@ -2316,31 +2325,31 @@ function getReasoningEffort() { | ||
| 2316 | 2325 | chat_completion_sources.CHUTES, |
| 2317 | 2326 | ]; |
| 2318 | 2327 | |
| 2319 | 2328 | if (!reasoningEffortSources.includes(oai_settingssettings.chat_completion_source)) { |
| 2320 | 2329 | return oai_settingssettings.reasoning_effort; |
| 2321 | 2330 | } |
| 2322 | 2331 | |
| 2323 | 2332 | function resolveReasoningEffort() { |
| 2324 | 2333 | switch (oai_settingssettings.reasoning_effort) { |
| 2325 | 2334 | case reasoning_effort_types.auto: |
| 2326 | 2335 | return undefined; |
| 2327 | 2336 | case reasoning_effort_types.min: |
| 2328 | 2337 | return [chat_completion_sources.OPENAI, chat_completion_sources.AZURE_OPENAI].includes(oai_settingssettings.chat_completion_source) && /^gpt-5/.test(getChatCompletionModel()model) |
| 2329 | 2338 | ? reasoning_effort_types.min |
| 2330 | 2339 | : reasoning_effort_types.low; |
| 2331 | 2340 | case reasoning_effort_types.max: |
| 2332 | 2341 | return reasoning_effort_types.high; |
| 2333 | 2342 | default: |
| 2334 | 2343 | return oai_settingssettings.reasoning_effort; |
| 2335 | 2344 | } |
| 2336 | 2345 | } |
| 2337 | 2346 | |
| 2338 | 2347 | const reasoningEffort = resolveReasoningEffort(); |
| 2339 | 2348 | |
| 2340 | 2349 | // Check if the resolved effort supported by the model |
| 2341 | 2350 | if (oai_settingssettings.chat_completion_source === chat_completion_sources.ELECTRONHUB) { |
| 2342 | 2351 | if (Array.isArray(model_list) && reasoningEffort) { |
| 2343 | 2352 | const currentModel = model_list.find(m => m.id === oai_settings.electronhub_modelmodel); |
| 2344 | 2353 | const supportedEfforts = currentModel?.metadata?.supported_reasoning_efforts; |
| 2345 | 2354 | if (Array.isArray(supportedEfforts) && supportedEfforts.includes(reasoningEffort)) { |
| 2346 | 2355 | return reasoningEffort; |
| @@ -2352,72 +2361,117 @@ function getReasoningEffort() { | ||
| 2352 | 2361 | return reasoningEffort; |
| 2353 | 2362 | } |
| 2354 | 2363 | |
| 2355 | -function getVerbosity() { | |
| 2364 | +/** | |
| 2356 | - if (oai_settings.verbosity === verbosity_levels.auto) { | |
| 2365 | + * Get the verbosity from chat completion settings | |
| 2366 | + * @param {ChatCompletionSettings} settings Chat completion settings | |
| 2367 | + * @returns {string} Verbosity level, if present | |
| 2368 | + */ | |
| 2369 | +function getVerbosity(settings = null) { | |
| 2370 | + settings = settings ?? oai_settings; | |
| 2371 | + | |
| 2372 | + if (settings.verbosity === verbosity_levels.auto) { | |
| 2357 | 2373 | return undefined; |
| 2358 | 2374 | } |
| 2359 | 2375 | |
| 2360 | 2376 | // TODO: Adjust verbosity based on model capabilities |
| 2361 | 2377 | return oai_settingssettings.verbosity; |
| 2362 | 2378 | } |
| 2363 | 2379 | |
| 2364 | 2380 | /** |
| 2365 | - * Send a chat completion request to backend | |
| 2381 | + * Build the generation parameter object for an OAI request. | |
| 2366 | 2382 | * @param {stringChatCompletionSettings} typesettings (impersonate,Initial quiet,chat continue,completion etc)settings |
| 2367 | 2383 | * @param {Arraystring} messagesmodel Model name |
| 2368 | - * @param {AbortSignal?} signal | |
| 2384 | + * @param {string} type Request type (impersonate, quiet, continue, etc) | |
| 2369 | - * @param {import('../script.js').AdditionalRequestOptions} options | |
| 2385 | + * @param {ChatCompletionMessage[]} messages Array of chat completion messages | |
| 2370 | - * @returns {Promise<unknown>} | |
| 2386 | + * @param {import('../script.js').AdditionalRequestOptions} options Additional request options | |
| 2371 | - * @throws {Error} | |
| 2387 | + * @returns {Promise<object>} Final generation parameters object appropriate for the chat completion source | |
| 2372 | 2388 | */ |
| 2373 | - | |
| 2389 | +export async function createGenerationParameters(settings, model, type, messages, { jsonSchema = null } = {}) { | |
| 2374 | -async function sendOpenAIRequest(type, messages, signal, { jsonSchema = null } = {}) { | |
| 2375 | - // Provide default abort signal | |
| 2376 | - if (!signal) { | |
| 2377 | - signal = new AbortController().signal; | |
| 2378 | - } | |
| 2379 | - | |
| 2380 | 2390 | // HACK: Filter out null and non-object messages |
| 2381 | 2391 | if (!Array.isArray(messages)) { |
| 2382 | 2392 | throw new Error('messages must be an array'); |
| 2383 | 2393 | } |
| 2384 | - | |
| 2385 | 2394 | messages = messages.filter(msg => msg && typeof msg === 'object'); |
| 2386 | 2395 | |
| 2387 | - let logit_bias = {}; | |
| 2396 | + // "OpenAI-like" sources | |
| 2388 | - const isClaude = oai_settings.chat_completion_source == chat_completion_sources.CLAUDE; | |
| 2397 | + const gptSources = [ | |
| 2389 | - const isOpenRouter = oai_settings.chat_completion_source == chat_completion_sources.OPENROUTER; | |
| 2398 | + chat_completion_sources.OPENAI, | |
| 2390 | - const isGoogle = oai_settings.chat_completion_source == chat_completion_sources.MAKERSUITE; | |
| 2399 | + chat_completion_sources.AZURE_OPENAI, | |
| 2391 | - const isVertexAI = oai_settings.chat_completion_source == chat_completion_sources.VERTEXAI; | |
| 2400 | + chat_completion_sources.OPENROUTER, | |
| 2392 | - const isOAI = oai_settings.chat_completion_source == chat_completion_sources.OPENAI; | |
| 2401 | + ]; | |
| 2393 | - const isMistral = oai_settings.chat_completion_source == chat_completion_sources.MISTRALAI; | |
| 2402 | + | |
| 2394 | - const isCustom = oai_settings.chat_completion_source == chat_completion_sources.CUSTOM; | |
| 2403 | + // Sources that support the "seed" parameter | |
| 2395 | - const isCohere = oai_settings.chat_completion_source == chat_completion_sources.COHERE; | |
| 2404 | + const seedSupportedSources = [ | |
| 2396 | - const isPerplexity = oai_settings.chat_completion_source == chat_completion_sources.PERPLEXITY; | |
| 2405 | + chat_completion_sources.OPENAI, | |
| 2397 | - const isGroq = oai_settings.chat_completion_source == chat_completion_sources.GROQ; | |
| 2406 | + chat_completion_sources.AZURE_OPENAI, | |
| 2398 | - const isDeepSeek = oai_settings.chat_completion_source == chat_completion_sources.DEEPSEEK; | |
| 2407 | + chat_completion_sources.OPENROUTER, | |
| 2399 | - const isAimlapi = oai_settings.chat_completion_source == chat_completion_sources.AIMLAPI; | |
| 2408 | + chat_completion_sources.MISTRALAI, | |
| 2400 | - const isElectronHub = oai_settings.chat_completion_source == chat_completion_sources.ELECTRONHUB; | |
| 2409 | + chat_completion_sources.CUSTOM, | |
| 2401 | - const isChutes = oai_settings.chat_completion_source == chat_completion_sources.CHUTES; | |
| 2410 | + chat_completion_sources.COHERE, | |
| 2402 | - const isXAI = oai_settings.chat_completion_source == chat_completion_sources.XAI; | |
| 2411 | + chat_completion_sources.GROQ, | |
| 2403 | - const isPollinations = oai_settings.chat_completion_source == chat_completion_sources.POLLINATIONS; | |
| 2412 | + chat_completion_sources.ELECTRONHUB, | |
| 2404 | - const isMoonshot = oai_settings.chat_completion_source == chat_completion_sources.MOONSHOT; | |
| 2413 | + chat_completion_sources.NANOGPT, | |
| 2405 | - const isAzureOpenAI = oai_settings.chat_completion_source == chat_completion_sources.AZURE_OPENAI; | |
| 2414 | + chat_completion_sources.XAI, | |
| 2406 | - const isZai = oai_settings.chat_completion_source == chat_completion_sources.ZAI; | |
| 2415 | + chat_completion_sources.POLLINATIONS, | |
| 2407 | - const isNanoGPT = oai_settings.chat_completion_source == chat_completion_sources.NANOGPT; | |
| 2416 | + chat_completion_sources.AIMLAPI, | |
| 2408 | - const isTextCompletion = isOAI && textCompletionModels.includes(oai_settings.openai_model); | |
| 2417 | + chat_completion_sources.VERTEXAI, | |
| 2409 | - const isQuiet = type === 'quiet'; | |
| 2418 | + chat_completion_sources.MAKERSUITE, | |
| 2410 | - const isImpersonate = type === 'impersonate'; | |
| 2419 | + chat_completion_sources.CHUTES, | |
| 2411 | - const isContinue = type === 'continue'; | |
| 2420 | + ]; | |
| 2412 | - const stream = oai_settings.stream_openai && !isQuiet && !((isOAI || isAzureOpenAI) && ['o1-2024-12-17', 'o1'].includes(getChatCompletionModel())); | |
| 2421 | + | |
| 2413 | - const useLogprobs = !!power_user.request_token_probabilities; | |
| 2422 | + // Sources that support proxying | |
| 2414 | - const canMultiSwipe = oai_settings.n > 1 && !isContinue && !isImpersonate && !isQuiet && (isOAI || isAzureOpenAI || isCustom || isXAI || isAimlapi || isMoonshot); | |
| 2423 | + const proxySupportedSources = [ | |
| 2424 | + chat_completion_sources.CLAUDE, | |
| 2425 | + chat_completion_sources.OPENAI, | |
| 2426 | + chat_completion_sources.MISTRALAI, | |
| 2427 | + chat_completion_sources.MAKERSUITE, | |
| 2428 | + chat_completion_sources.VERTEXAI, | |
| 2429 | + chat_completion_sources.DEEPSEEK, | |
| 2430 | + chat_completion_sources.XAI, | |
| 2431 | + ]; | |
| 2432 | + | |
| 2433 | + // Sources that support logprobs | |
| 2434 | + const logprobsSupportedSources = [ | |
| 2435 | + chat_completion_sources.OPENAI, | |
| 2436 | + chat_completion_sources.AZURE_OPENAI, | |
| 2437 | + chat_completion_sources.CUSTOM, | |
| 2438 | + chat_completion_sources.DEEPSEEK, | |
| 2439 | + chat_completion_sources.XAI, | |
| 2440 | + chat_completion_sources.AIMLAPI, | |
| 2441 | + chat_completion_sources.CHUTES, | |
| 2442 | + ]; | |
| 2443 | + | |
| 2444 | + // Sources that support logit bias | |
| 2445 | + const logitBiasSources = [ | |
| 2446 | + chat_completion_sources.OPENAI, | |
| 2447 | + chat_completion_sources.AZURE_OPENAI, | |
| 2448 | + chat_completion_sources.OPENROUTER, | |
| 2449 | + chat_completion_sources.ELECTRONHUB, | |
| 2450 | + chat_completion_sources.CHUTES, | |
| 2451 | + chat_completion_sources.CUSTOM, | |
| 2452 | + ]; | |
| 2453 | + | |
| 2454 | + // Sources that support "n" parameter for multi-swipe | |
| 2455 | + const multiswipeSources = [ | |
| 2456 | + chat_completion_sources.OPENAI, | |
| 2457 | + chat_completion_sources.AZURE_OPENAI, | |
| 2458 | + chat_completion_sources.CUSTOM, | |
| 2459 | + chat_completion_sources.XAI, | |
| 2460 | + chat_completion_sources.AIMLAPI, | |
| 2461 | + chat_completion_sources.MOONSHOT, | |
| 2462 | + ]; | |
| 2463 | + | |
| 2464 | + const isO1 = gptSources.includes(settings.chat_completion_source) && ['o1-2024-12-17', 'o1'].includes(model); | |
| 2465 | + const stream = settings.stream_openai && type !== 'quiet' && !isO1; | |
| 2466 | + | |
| 2467 | + const noMultiSwipeTypes = ['quiet', 'impersonate', 'continue']; | |
| 2468 | + const canMultiSwipe = settings.n > 1 && !noMultiSwipeTypes.includes(type) && multiswipeSources.includes(settings.chat_completion_source); | |
| 2415 | 2469 | |
| 2416 | - const logitBiasSources = [chat_completion_sources.OPENAI, chat_completion_sources.AZURE_OPENAI, chat_completion_sources.OPENROUTER, chat_completion_sources.ELECTRONHUB, chat_completion_sources.CHUTES, chat_completion_sources.CUSTOM]; | |
| 2470 | + let logit_bias = {}; | |
| 2417 | 2471 | if (oai_settingssettings.bias_preset_selected |
| 2418 | 2472 | && logitBiasSources.includes(oai_settingssettings.chat_completion_source) |
| 2419 | 2473 | && Array.isArray(oai_settingssettings.bias_presets[oai_settingssettings.bias_preset_selected]) |
| 2420 | 2474 | && oai_settingssettings.bias_presets[oai_settingssettings.bias_preset_selected].length) { |
| 2421 | 2475 | logit_bias = biasCache || await calculateLogitBias(); |
| 2422 | 2476 | biasCache = logit_bias; |
| 2423 | 2477 | } |
| @@ -2426,45 +2480,44 @@ async function sendOpenAIRequest(type, messages, signal, { jsonSchema = null } = | ||
| 2426 | 2480 | logit_bias = undefined; |
| 2427 | 2481 | } |
| 2428 | 2482 | |
| 2429 | - const model = getChatCompletionModel(); | |
| 2430 | 2483 | const generate_data = { |
| 2431 | 2484 | 'type': type, |
| 2432 | 2485 | 'messages': messages, |
| 2433 | 2486 | 'model': model, |
| 2434 | 2487 | 'temperature': Number(oai_settingssettings.temp_openai), |
| 2435 | 2488 | 'frequency_penalty': Number(oai_settingssettings.freq_pen_openai), |
| 2436 | 2489 | 'presence_penalty': Number(oai_settingssettings.pres_pen_openai), |
| 2437 | 2490 | 'top_p': Number(oai_settingssettings.top_p_openai), |
| 2438 | 2491 | 'max_tokens': oai_settingssettings.openai_max_tokens, |
| 2439 | 2492 | 'stream': stream, |
| 2440 | 2493 | 'logit_bias': logit_bias, |
| 2441 | 2494 | 'stop': getCustomStoppingStrings(openai_max_stop_strings), |
| 2442 | 2495 | 'chat_completion_source': oai_settingssettings.chat_completion_source, |
| 2443 | 2496 | 'n': canMultiSwipe ? oai_settingssettings.n : undefined, |
| 2444 | 2497 | 'user_name': name1, |
| 2445 | 2498 | 'char_name': name2, |
| 2446 | 2499 | 'group_names': getGroupNames(), |
| 2447 | 2500 | 'include_reasoning': Boolean(oai_settingssettings.show_thoughts), |
| 2448 | 2501 | 'reasoning_effort': getReasoningEffort(settings, model), |
| 2449 | 2502 | 'enable_web_search': Boolean(oai_settingssettings.enable_web_search), |
| 2450 | 2503 | 'request_images': Boolean(oai_settingssettings.request_images), |
| 2451 | 2504 | 'request_image_resolution': String(oai_settingssettings.request_image_resolution), |
| 2452 | 2505 | 'request_image_aspect_ratio': String(oai_settingssettings.request_image_aspect_ratio), |
| 2453 | 2506 | 'custom_prompt_post_processing': oai_settingssettings.custom_prompt_post_processing, |
| 2454 | 2507 | 'verbosity': getVerbosity(settings), |
| 2455 | 2508 | }; |
| 2456 | 2509 | |
| 2457 | 2510 | if (isAzureOpenAIsettings.chat_completion_source === chat_completion_sources.AZURE_OPENAI) { |
| 2458 | 2511 | generate_data.azure_base_url = oai_settingssettings.azure_base_url; |
| 2459 | 2512 | generate_data.azure_deployment_name = oai_settingssettings.azure_deployment_name; |
| 2460 | 2513 | generate_data.azure_api_version = oai_settingssettings.azure_api_version; |
| 2461 | 2514 | // Reasoning effort is not supported on some Azure models (e.g. GPT-3.x, GPT-4.x) |
| 2462 | 2515 | if (/^gpt-[34]/.test(oai_settings.azure_openai_modelmodel)) { |
| 2463 | 2516 | delete generate_data.reasoning_effort; |
| 2464 | 2517 | } |
| 2465 | 2518 | } |
| 2466 | 2519 | |
| 2467 | 2520 | if (!canMultiSwipe && ToolManager.canPerformToolCalls(type, settings, model)) { |
| 2468 | 2521 | await ToolManager.registerFunctionToolsOpenAI(generate_data); |
| 2469 | 2522 | } |
| 2470 | 2523 | |
| @@ -2473,99 +2526,95 @@ async function sendOpenAIRequest(type, messages, signal, { jsonSchema = null } = | ||
| 2473 | 2526 | delete generate_data.stop; |
| 2474 | 2527 | } |
| 2475 | 2528 | |
| 2476 | - // Proxy is only supported for Claude, OpenAI, Mistral, Google MakerSuite, and Vertex AI | |
| 2529 | + if (settings.reverse_proxy && proxySupportedSources.includes(settings.chat_completion_source)) { | |
| 2477 | - if (oai_settings.reverse_proxy && [chat_completion_sources.CLAUDE, chat_completion_sources.OPENAI, chat_completion_sources.MISTRALAI, chat_completion_sources.MAKERSUITE, chat_completion_sources.VERTEXAI, chat_completion_sources.DEEPSEEK, chat_completion_sources.XAI].includes(oai_settings.chat_completion_source)) { | |
| 2478 | 2530 | await validateReverseProxy(); |
| 2479 | 2531 | generate_data['reverse_proxy'] = oai_settingssettings.reverse_proxy; |
| 2480 | 2532 | generate_data['proxy_password'] = oai_settingssettings.proxy_password; |
| 2481 | 2533 | } |
| 2482 | 2534 | |
| 2483 | 2535 | // Add logprobs request (currently OpenAI only, max 5 onper theirOpenAI sidedocs) |
| 2484 | - if (useLogprobs && (isOAI || isAzureOpenAI || isCustom || isDeepSeek || isXAI || isAimlapi || isChutes)) { | |
| 2536 | + const useLogprobs = !!power_user.request_token_probabilities; | |
| 2537 | + if (useLogprobs && logprobsSupportedSources.includes(settings.chat_completion_source)) { | |
| 2485 | 2538 | generate_data['logprobs'] = 5; |
| 2486 | 2539 | } |
| 2487 | 2540 | |
| 2488 | 2541 | // Remove logit bias/logprobs/stop-strings if not supported by the model |
| 2489 | 2542 | const isVision = (m) => ['gpt', 'vision'].every(x => m.includes(x)); |
| 2490 | - if ((isOAI && isVision(oai_settings.openai_model)) || (isAzureOpenAI && isVision(oai_settings.azure_openai_model)) || (isOpenRouter && isVision(oai_settings.openrouter_model))) { | |
| 2543 | + if (gptSources.includes(settings.chat_completion_source) && isVision(model)) { | |
| 2491 | 2544 | delete generate_data.logit_bias; |
| 2492 | 2545 | delete generate_data.stop; |
| 2493 | 2546 | delete generate_data.logprobs; |
| 2494 | 2547 | } |
| 2495 | - if ((isOAI && oai_settings.openai_model.includes('gpt-4.5')) || (isAzureOpenAI && oai_settings.azure_openai_model.includes('gpt-4.5')) || (isOpenRouter && oai_settings.openrouter_model.includes('gpt-4.5'))) { | |
| 2548 | + if (gptSources.includes(settings.chat_completion_source) && model.includes('gpt-4.5')) { | |
| 2496 | 2549 | delete generate_data.logprobs; |
| 2497 | 2550 | } |
| 2498 | 2551 | |
| 2499 | 2552 | if (isClaudesettings.chat_completion_source === chat_completion_sources.CLAUDE) { |
| 2500 | 2553 | generate_data['top_k'] = Number(oai_settingssettings.top_k_openai); |
| 2501 | 2554 | generate_data['use_sysprompt'] = oai_settingssettings.use_sysprompt; |
| 2502 | 2555 | generate_data['stop'] = getCustomStoppingStrings(); // Claude shouldn't have limits on stop strings. |
| 2503 | 2556 | // Don't add a prefill on quiet gens (summarization) and when using continue prefill. |
| 2504 | 2557 | if (type !isQuiet== 'quiet' && !(isContinuetype === 'continue' && oai_settingssettings.continue_prefill)) { |
| 2505 | - generate_data['assistant_prefill'] = isImpersonate ? substituteParams(oai_settings.assistant_impersonation) : substituteParams(oai_settings.assistant_prefill); | |
| 2558 | + generate_data['assistant_prefill'] = type === 'impersonate' | |
| 2559 | + ? substituteParams(settings.assistant_impersonation) | |
| 2560 | + : substituteParams(settings.assistant_prefill); | |
| 2506 | 2561 | } |
| 2507 | 2562 | } |
| 2508 | 2563 | |
| 2509 | 2564 | if (isOpenRoutersettings.chat_completion_source === chat_completion_sources.OPENROUTER) { |
| 2510 | 2565 | generate_data['top_k'] = Number(oai_settingssettings.top_k_openai); |
| 2511 | 2566 | generate_data['min_p'] = Number(oai_settingssettings.min_p_openai); |
| 2512 | 2567 | generate_data['repetition_penalty'] = Number(oai_settingssettings.repetition_penalty_openai); |
| 2513 | 2568 | generate_data['top_a'] = Number(oai_settingssettings.top_a_openai); |
| 2514 | 2569 | generate_data['use_fallback'] = oai_settingssettings.openrouter_use_fallback; |
| 2515 | 2570 | generate_data['provider'] = oai_settingssettings.openrouter_providers; |
| 2516 | 2571 | generate_data['allow_fallbacks'] = oai_settingssettings.openrouter_allow_fallbacks; |
| 2517 | 2572 | generate_data['middleout'] = oai_settingssettings.openrouter_middleout; |
| 2518 | - | |
| 2519 | - if (isTextCompletion) { | |
| 2520 | - generate_data['stop'] = getStoppingStrings(isImpersonate, isContinue); | |
| 2521 | - } | |
| 2522 | 2573 | } |
| 2523 | 2574 | |
| 2524 | - if (isGoogle || isVertexAI) { | |
| 2575 | + if ([chat_completion_sources.MAKERSUITE, chat_completion_sources.VERTEXAI].includes(settings.chat_completion_source)) { | |
| 2525 | 2576 | const stopStringsLimit = 5; |
| 2526 | 2577 | generate_data['top_k'] = Number(oai_settingssettings.top_k_openai); |
| 2527 | 2578 | generate_data['stop'] = getCustomStoppingStrings(stopStringsLimit).slice(0, stopStringsLimit).filter(x => x.length >= 1 && x.length <= 16); |
| 2528 | 2579 | generate_data['use_sysprompt'] = oai_settingssettings.use_sysprompt; |
| 2529 | 2580 | if (isVertexAIsettings.chat_completion_source === chat_completion_sources.VERTEXAI) { |
| 2530 | 2581 | generate_data['vertexai_auth_mode'] = oai_settingssettings.vertexai_auth_mode; |
| 2531 | 2582 | generate_data['vertexai_region'] = oai_settingssettings.vertexai_region; |
| 2532 | 2583 | generate_data['vertexai_express_project_id'] = oai_settingssettings.vertexai_express_project_id; |
| 2533 | 2584 | } |
| 2534 | 2585 | } |
| 2535 | 2586 | |
| 2536 | 2587 | if (isMistralsettings.chat_completion_source === chat_completion_sources.MISTRALAI) { |
| 2537 | 2588 | generate_data['safe_prompt'] = false; // already defaults to false, but just incase they change that in the future. |
| 2538 | 2589 | generate_data['stop'] = getCustomStoppingStrings(); // Mistral shouldn't have limits on stop strings. |
| 2539 | 2590 | } |
| 2540 | 2591 | |
| 2541 | 2592 | if (isCustomsettings.chat_completion_source === chat_completion_sources.CUSTOM) { |
| 2542 | 2593 | generate_data['custom_url'] = oai_settingssettings.custom_url; |
| 2543 | 2594 | generate_data['custom_include_body'] = oai_settingssettings.custom_include_body; |
| 2544 | 2595 | generate_data['custom_exclude_body'] = oai_settingssettings.custom_exclude_body; |
| 2545 | 2596 | generate_data['custom_include_headers'] = oai_settingssettings.custom_include_headers; |
| 2546 | 2597 | } |
| 2547 | 2598 | |
| 2548 | 2599 | if (isCoheresettings.chat_completion_source === chat_completion_sources.COHERE) { |
| 2549 | 2600 | // Clamp to 0.01 -> 0.99 |
| 2550 | 2601 | generate_data['top_p'] = Math.min(Math.max(Number(oai_settingssettings.top_p_openai), 0.01), 0.99); |
| 2551 | 2602 | generate_data['top_k'] = Number(oai_settingssettings.top_k_openai); |
| 2552 | 2603 | // Clamp to 0 -> 1 |
| 2553 | 2604 | generate_data['frequency_penalty'] = Math.min(Math.max(Number(oai_settingssettings.freq_pen_openai), 0), 1); |
| 2554 | 2605 | generate_data['presence_penalty'] = Math.min(Math.max(Number(oai_settingssettings.pres_pen_openai), 0), 1); |
| 2555 | 2606 | generate_data['stop'] = getCustomStoppingStrings(5); |
| 2556 | 2607 | } |
| 2557 | 2608 | |
| 2558 | 2609 | if (isPerplexitysettings.chat_completion_source === chat_completion_sources.PERPLEXITY) { |
| 2559 | 2610 | generate_data['top_k'] = Number(oai_settingssettings.top_k_openai); |
| 2560 | 2611 | generate_data['frequency_penalty'] = Number(oai_settingssettings.freq_pen_openai); |
| 2561 | 2612 | generate_data['presence_penalty'] = Number(oai_settingssettings.pres_pen_openai); |
| 2562 | - | |
| 2563 | - // YEAH BRO JUST USE OPENAI CLIENT BRO | |
| 2564 | 2613 | delete generate_data['stop']; |
| 2565 | 2614 | } |
| 2566 | 2615 | |
| 2567 | 2616 | // https://console.groq.com/docs/openai |
| 2568 | 2617 | if (isGroqsettings.chat_completion_source === chat_completion_sources.GROQ) { |
| 2569 | 2618 | delete generate_data.logprobs; |
| 2570 | 2619 | delete generate_data.logit_bias; |
| 2571 | 2620 | delete generate_data.top_logprobs; |
| @@ -2573,12 +2622,11 @@ async function sendOpenAIRequest(type, messages, signal, { jsonSchema = null } = | ||
| 2573 | 2622 | } |
| 2574 | 2623 | |
| 2575 | 2624 | // https://api-docs.deepseek.com/api/create-chat-completion |
| 2576 | 2625 | if (isDeepSeeksettings.chat_completion_source === chat_completion_sources.DEEPSEEK) { |
| 2577 | 2626 | generate_data.top_p = generate_data.top_p || Number.EPSILON; |
| 2578 | 2627 | } |
| 2579 | 2628 | |
| 2580 | 2629 | if (isXAIsettings.chat_completion_source === chat_completion_sources.XAI) { |
| 2581 | - const model = generate_data.model; | |
| 2582 | 2630 | if (model.includes('grok-3-mini')) { |
| 2583 | 2631 | delete generate_data.presence_penalty; |
| 2584 | 2632 | delete generate_data.frequency_penalty; |
| @@ -2599,61 +2647,44 @@ async function sendOpenAIRequest(type, messages, signal, { jsonSchema = null } = | ||
| 2599 | 2647 | } |
| 2600 | 2648 | } |
| 2601 | 2649 | |
| 2602 | 2650 | if (isPollinationssettings.chat_completion_source === chat_completion_sources.POLLINATIONS) { |
| 2603 | 2651 | delete generate_data.max_tokens; |
| 2604 | 2652 | } |
| 2605 | 2653 | |
| 2606 | 2654 | // https://docs.electronhub.ai/api-reference/chat/completions |
| 2607 | 2655 | if (isElectronHubsettings.chat_completion_source === chat_completion_sources.ELECTRONHUB) { |
| 2608 | 2656 | generate_data['top_k'] = Number(oai_settingssettings.top_k_openai); |
| 2609 | 2657 | } |
| 2610 | 2658 | |
| 2611 | 2659 | if (isChutessettings.chat_completion_source === chat_completion_sources.CHUTES) { |
| 2612 | 2660 | generate_data['min_p'] = Number(oai_settingssettings.min_p_openai); |
| 2613 | 2661 | generate_data['top_k'] = oai_settingssettings.top_k_openai > 0 ? Number(oai_settingssettings.top_k_openai) : undefined; |
| 2614 | 2662 | generate_data['repetition_penalty'] = Number(oai_settingssettings.repetition_penalty_openai); |
| 2615 | - generate_data['seed'] = oai_settings.seed >= 0 ? oai_settings.seed : undefined; | |
| 2616 | 2663 | generate_data['stop'] = getCustomStoppingStrings(); |
| 2617 | 2664 | } |
| 2618 | 2665 | |
| 2619 | 2666 | // https://docs.z.ai/api-reference/llm/chat-completion |
| 2620 | 2667 | if (isZaisettings.chat_completion_source === chat_completion_sources.ZAI) { |
| 2621 | 2668 | generate_data['top_p'] = generate_data.top_p || 0.01; |
| 2622 | 2669 | generate_data['stop'] = getCustomStoppingStrings(1); |
| 2623 | 2670 | generate_data['zai_endpoint'] = oai_settingssettings.zai_endpoint || ZAI_ENDPOINT.COMMON; |
| 2624 | 2671 | delete generate_data.presence_penalty; |
| 2625 | 2672 | delete generate_data.frequency_penalty; |
| 2626 | 2673 | } |
| 2627 | 2674 | |
| 2628 | 2675 | // https://docs.nano-gpt.com/api-reference/endpoint/chat-completion#temperature-&-nucleus |
| 2629 | 2676 | if (isNanoGPTsettings.chat_completion_source === chat_completion_sources.NANOGPT) { |
| 2630 | 2677 | generate_data['top_k'] = Number(oai_settingssettings.top_k_openai); |
| 2631 | 2678 | generate_data['min_p'] = Number(oai_settingssettings.min_p_openai); |
| 2632 | 2679 | generate_data['repetition_penalty'] = Number(oai_settingssettings.repetition_penalty_openai); |
| 2633 | 2680 | generate_data['top_a'] = Number(oai_settingssettings.top_a_openai); |
| 2634 | 2681 | } |
| 2635 | 2682 | |
| 2636 | - const seedSupportedSources = [ | |
| 2683 | + if (seedSupportedSources.includes(settings.chat_completion_source) && settings.seed >= 0) { | |
| 2637 | - chat_completion_sources.OPENAI, | |
| 2684 | + generate_data['seed'] = settings.seed; | |
| 2638 | - chat_completion_sources.AZURE_OPENAI, | |
| 2639 | - chat_completion_sources.OPENROUTER, | |
| 2640 | - chat_completion_sources.MISTRALAI, | |
| 2641 | - chat_completion_sources.CUSTOM, | |
| 2642 | - chat_completion_sources.COHERE, | |
| 2643 | - chat_completion_sources.GROQ, | |
| 2644 | - chat_completion_sources.ELECTRONHUB, | |
| 2645 | - chat_completion_sources.NANOGPT, | |
| 2646 | - chat_completion_sources.XAI, | |
| 2647 | - chat_completion_sources.POLLINATIONS, | |
| 2648 | - chat_completion_sources.AIMLAPI, | |
| 2649 | - chat_completion_sources.VERTEXAI, | |
| 2650 | - chat_completion_sources.MAKERSUITE, | |
| 2651 | - ]; | |
| 2652 | - if (seedSupportedSources.includes(oai_settings.chat_completion_source) && oai_settings.seed >= 0) { | |
| 2653 | - generate_data['seed'] = oai_settings.seed; | |
| 2654 | 2685 | } |
| 2655 | 2686 | |
| 2656 | 2687 | if ((isOAI && /^(o1|o3|o4)/gptSources.testincludes(model)settings.chat_completion_source) || (isAzureOpenAI && /^(o1|o3|o4)/.test(model))) { |
| 2657 | 2688 | generate_data.max_completion_tokens = generate_data.max_tokens; |
| 2658 | 2689 | delete generate_data.max_tokens; |
| 2659 | 2690 | delete generate_data.logprobs; |
| @@ -2676,7 +2707,7 @@ async function sendOpenAIRequest(type, messages, signal, { jsonSchema = null } = | ||
| 2676 | 2707 | } |
| 2677 | 2708 | } |
| 2678 | 2709 | |
| 2679 | 2710 | if ((isOAI && /^gpt-5/gptSources.testincludes(model)settings.chat_completion_source) || (isAzureOpenAI && /^gpt-5/.test(model))) { |
| 2680 | 2711 | generate_data.max_completion_tokens = generate_data.max_tokens; |
| 2681 | 2712 | delete generate_data.max_tokens; |
| 2682 | 2713 | delete generate_data.logprobs; |
| @@ -2703,6 +2734,26 @@ async function sendOpenAIRequest(type, messages, signal, { jsonSchema = null } = | ||
| 2703 | 2734 | generate_data.json_schema = jsonSchema; |
| 2704 | 2735 | } |
| 2705 | 2736 | |
| 2737 | + return { generate_data, stream, canMultiSwipe }; | |
| 2738 | +} | |
| 2739 | + | |
| 2740 | +/** | |
| 2741 | + * Send a chat completion request to backend | |
| 2742 | + * @param {string} type Request type (impersonate, quiet, continue, etc) | |
| 2743 | + * @param {ChatCompletionMessage[]} messages Array of chat completion messages | |
| 2744 | + * @param {AbortSignal?} signal Abort signal for request cancellation | |
| 2745 | + * @param {import('../script.js').AdditionalRequestOptions} options Additional request options | |
| 2746 | + * @returns {Promise<unknown>} | |
| 2747 | + * @throws {Error} | |
| 2748 | + */ | |
| 2749 | +async function sendOpenAIRequest(type, messages, signal, { jsonSchema = null } = {}) { | |
| 2750 | + // Provide default abort signal | |
| 2751 | + if (!signal) { | |
| 2752 | + signal = new AbortController().signal; | |
| 2753 | + } | |
| 2754 | + | |
| 2755 | + const model = getChatCompletionModel(oai_settings); | |
| 2756 | + const { generate_data, stream, canMultiSwipe } = await createGenerationParameters(oai_settings, model, type, messages, { jsonSchema }); | |
| 2706 | 2757 | await eventSource.emit(event_types.CHAT_COMPLETION_SETTINGS_READY, generate_data); |
| 2707 | 2758 | |
| 2708 | 2759 | const generate_url = '/api/backends/chat-completions/generate'; |
| @@ -1220,23 +1220,38 @@ export function removeReasoningFromString(str) { | ||
| 1220 | 1220 | } |
| 1221 | 1221 | |
| 1222 | 1222 | /** |
| 1223 | - * Parses reasoning from a string using the power user reasoning settings. | |
| 1223 | + * Returns the reasoning template object from its name | |
| 1224 | + * @param {string} name of the template | |
| 1225 | + * @returns {ReasoningTemplate} the reasoning template object | |
| 1226 | + * @throws {Error} | |
| 1227 | + */ | |
| 1228 | +export function getReasoningTemplateByName(name) { | |
| 1229 | + const template = reasoning_templates.find(p => p.name === name); | |
| 1230 | + if (!template) throw new Error(`Unknown reasoning template name: "${name}"`); | |
| 1231 | + return template; | |
| 1232 | +} | |
| 1233 | + | |
| 1234 | +/** | |
| 1235 | + * Parses reasoning from a string using the power user reasoning settings or optional template. | |
| 1224 | 1236 | * @typedef {Object} ParsedReasoning |
| 1225 | 1237 | * @property {string} reasoning Reasoning block |
| 1226 | 1238 | * @property {string} content Message content |
| 1227 | 1239 | * @param {string} str Content of the message |
| 1228 | 1240 | * @param {Object} options Optional arguments |
| 1229 | 1241 | * @param {boolean} [options.strict=true] Whether the reasoning block **has** to be at the beginning of the provided string (excluding whitespaces), or can be anywhere in it |
| 1242 | + * @param {ReasoningTemplate} template Optional reasoning template to use instead of power_user.reasoning | |
| 1230 | 1243 | * @returns {ParsedReasoning|null} Parsed reasoning block and message content |
| 1231 | 1244 | */ |
| 1232 | 1245 | export function parseReasoningFromString(str, { strict = true } = {}, template = null) { |
| 1246 | + template = template ?? power_user.reasoning; // if no template given, use the currently selected template | |
| 1247 | + | |
| 1233 | 1248 | // Both prefix and suffix must be defined |
| 1234 | 1249 | if (!power_user.reasoningtemplate.prefix || !power_user.reasoningtemplate.suffix) { |
| 1235 | 1250 | return null; |
| 1236 | 1251 | } |
| 1237 | 1252 | |
| 1238 | 1253 | try { |
| 1239 | 1254 | const regex = new RegExp(`${(strict ? '^\\s*?' : '')}${escapeRegex(power_user.reasoningtemplate.prefix)}(.*?)${escapeRegex(power_user.reasoningtemplate.suffix)}`, 's'); |
| 1240 | 1255 | |
| 1241 | 1256 | let didReplace = false; |
| 1242 | 1257 | let reasoning = ''; |
| @@ -99,7 +99,7 @@ import { getGlobalVariable, getLocalVariable, setGlobalVariable, setLocalVariabl | ||
| 99 | 99 | import { convertCharacterBook, getWorldInfoPrompt, loadWorldInfo, reloadEditor, saveWorldInfo, updateWorldInfoList } from './world-info.js'; |
| 100 | 100 | import { ChatCompletionService, TextCompletionService } from './custom-request.js'; |
| 101 | 101 | import { ConnectionManagerRequestService } from './extensions/shared.js'; |
| 102 | 102 | import { updateReasoningUI, parseReasoningFromString, getReasoningTemplateByName } from './reasoning.js'; |
| 103 | 103 | import { IGNORE_SYMBOL } from './constants.js'; |
| 104 | 104 | |
| 105 | 105 | export function getContext() { |
| @@ -259,6 +259,7 @@ export function getContext() { | ||
| 259 | 259 | ConnectionManagerRequestService, |
| 260 | 260 | updateReasoningUI, |
| 261 | 261 | parseReasoningFromString, |
| 262 | + getReasoningTemplateByName, | |
| 262 | 263 | unshallowCharacter, |
| 263 | 264 | unshallowGroupMembers, |
| 264 | 265 | openThirdPartyExtensionMenu, |
| @@ -139,7 +139,7 @@ export const SERVER_INPUTS = { | ||
| 139 | 139 | }; |
| 140 | 140 | |
| 141 | 141 | const KOBOLDCPP_ORDER = [6, 0, 1, 3, 4, 2, 5]; |
| 142 | 142 | export const settingstextgenerationwebui_settings = { |
| 143 | 143 | temp: 0.7, |
| 144 | 144 | temperature_last: true, |
| 145 | 145 | top_p: 0.5, |
| @@ -231,7 +231,6 @@ const settings = { | ||
| 231 | 231 | }; |
| 232 | 232 | |
| 233 | 233 | export { |
| 234 | - settings as textgenerationwebui_settings, | |
| 235 | 234 | showSamplerControls as showTGSamplerControls, |
| 236 | 235 | }; |
| 237 | 236 | |
| @@ -318,7 +317,7 @@ export const setting_names = [ | ||
| 318 | 317 | const DYNATEMP_BLOCK = document.getElementById('dynatemp_block_ooba'); |
| 319 | 318 | |
| 320 | 319 | export function validateTextGenUrl() { |
| 321 | 320 | const selector = SERVER_INPUTS[settingstextgenerationwebui_settings.type]; |
| 322 | 321 | |
| 323 | 322 | if (!selector) { |
| 324 | 323 | return; |
| @@ -342,7 +341,7 @@ export function validateTextGenUrl() { | ||
| 342 | 341 | * @returns {string} API URL |
| 343 | 342 | */ |
| 344 | 343 | export function getTextGenServer(type = null) { |
| 345 | 344 | const selectedType = type ?? settingstextgenerationwebui_settings.type; |
| 346 | 345 | switch (selectedType) { |
| 347 | 346 | case FEATHERLESS: |
| 348 | 347 | return FEATHERLESS_SERVER; |
| @@ -357,7 +356,7 @@ export function getTextGenServer(type = null) { | ||
| 357 | 356 | case OPENROUTER: |
| 358 | 357 | return OPENROUTER_SERVER; |
| 359 | 358 | default: |
| 360 | 359 | return settingstextgenerationwebui_settings.server_urls[selectedType] ?? ''; |
| 361 | 360 | } |
| 362 | 361 | } |
| 363 | 362 | |
| @@ -368,7 +367,7 @@ async function selectPreset(name) { | ||
| 368 | 367 | return; |
| 369 | 368 | } |
| 370 | 369 | |
| 371 | 370 | settingstextgenerationwebui_settings.preset = name; |
| 372 | 371 | for (const name of setting_names) { |
| 373 | 372 | const value = preset[name]; |
| 374 | 373 | setSettingByName(name, value, true); |
| @@ -382,7 +381,7 @@ async function selectPreset(name) { | ||
| 382 | 381 | export function formatTextGenURL(value) { |
| 383 | 382 | try { |
| 384 | 383 | const noFormatTypes = [MANCER, TOGETHERAI, INFERMATICAI, DREAMGEN, OPENROUTER]; |
| 385 | 384 | if (noFormatTypes.includes(settingstextgenerationwebui_settings.type)) { |
| 386 | 385 | return value; |
| 387 | 386 | } |
| 388 | 387 | |
| @@ -399,7 +398,7 @@ function convertPresets(presets) { | ||
| 399 | 398 | } |
| 400 | 399 | |
| 401 | 400 | function getTokenizerForTokenIds() { |
| 402 | 401 | if (power_user.tokenizer === tokenizers.API_CURRENT && TEXTGEN_TOKENIZERS.includes(settingstextgenerationwebui_settings.type)) { |
| 403 | 402 | return tokenizers.API_CURRENT; |
| 404 | 403 | } |
| 405 | 404 | |
| @@ -407,11 +406,11 @@ function getTokenizerForTokenIds() { | ||
| 407 | 406 | return power_user.tokenizer; |
| 408 | 407 | } |
| 409 | 408 | |
| 410 | 409 | if (settingstextgenerationwebui_settings.type === OPENROUTER) { |
| 411 | 410 | return getCurrentOpenRouterModelTokenizer(); |
| 412 | 411 | } |
| 413 | 412 | |
| 414 | 413 | if (settingstextgenerationwebui_settings.type === DREAMGEN) { |
| 415 | 414 | return getCurrentDreamGenModelTokenizer(); |
| 416 | 415 | } |
| 417 | 416 | |
| @@ -419,10 +418,13 @@ function getTokenizerForTokenIds() { | ||
| 419 | 418 | } |
| 420 | 419 | |
| 421 | 420 | /** |
| 421 | + * Gets the custom token bans from settings and macros. | |
| 422 | + * @param {TextCompletionSettings} settings Text completion settings to use | |
| 422 | 423 | * @typedef {{banned_tokens: string, banned_strings: string[]}} TokenBanResult |
| 423 | 424 | * @returns {TokenBanResult} String with comma-separated banned token IDs |
| 424 | 425 | */ |
| 425 | 426 | function getCustomTokenBans(settings = null) { |
| 427 | + settings = settings ?? textgenerationwebui_settings; | |
| 426 | 428 | if (!settings.send_banned_tokens || (!settings.banned_tokens && !settings.global_banned_tokens && !textgenerationwebui_banned_in_macros.length)) { |
| 427 | 429 | return { |
| 428 | 430 | banned_tokens: '', |
| @@ -491,15 +493,18 @@ function getCustomTokenBans() { | ||
| 491 | 493 | function toggleBannedStringsKillSwitch(isEnabled, title) { |
| 492 | 494 | $('#send_banned_tokens_textgenerationwebui').prop('checked', isEnabled); |
| 493 | 495 | $('#send_banned_tokens_label').find('.menu_button').toggleClass('toggleEnabled', isEnabled).prop('title', title); |
| 494 | 496 | settingstextgenerationwebui_settings.send_banned_tokens = isEnabled; |
| 495 | 497 | saveSettingsDebounced(); |
| 496 | 498 | } |
| 497 | 499 | |
| 498 | 500 | /** |
| 499 | 501 | * Calculates logit bias object from the logit bias list. |
| 502 | + * @param {TextCompletionSettings} settings Text completion settings | |
| 500 | 503 | * @returns {object} Logit bias object |
| 501 | 504 | */ |
| 502 | 505 | function calculateLogitBias(settings = null) { |
| 506 | + settings = settings ?? textgenerationwebui_settings; | |
| 507 | + | |
| 503 | 508 | if (!Array.isArray(settings.logit_bias) || settings.logit_bias.length === 0) { |
| 504 | 509 | return {}; |
| 505 | 510 | } |
| @@ -535,25 +540,25 @@ export async function loadTextGenSettings(data, loadedSettings) { | ||
| 535 | 540 | await loadApiSelectedSamplers(); |
| 536 | 541 | textgenerationwebui_presets = convertPresets(data.textgenerationwebui_presets); |
| 537 | 542 | textgenerationwebui_preset_names = data.textgenerationwebui_preset_names ?? []; |
| 538 | 543 | Object.assign(settingstextgenerationwebui_settings, loadedSettings.textgenerationwebui_settings ?? {}); |
| 539 | 544 | |
| 540 | 545 | if (loadedSettings.api_server_textgenerationwebui) { |
| 541 | 546 | for (const type of Object.keys(SERVER_INPUTS)) { |
| 542 | 547 | settingstextgenerationwebui_settings.server_urls[type] = loadedSettings.api_server_textgenerationwebui; |
| 543 | 548 | } |
| 544 | 549 | delete loadedSettings.api_server_textgenerationwebui; |
| 545 | 550 | } |
| 546 | 551 | |
| 547 | 552 | for (const [type, selector] of Object.entries(SERVER_INPUTS)) { |
| 548 | 553 | const control = $(selector); |
| 549 | 554 | control.val(settingstextgenerationwebui_settings.server_urls[type] ?? '').on('input', function () { |
| 550 | 555 | settingstextgenerationwebui_settings.server_urls[type] = String($(this).val()).trim(); |
| 551 | 556 | saveSettingsDebounced(); |
| 552 | 557 | }); |
| 553 | 558 | } |
| 554 | 559 | |
| 555 | 560 | if (loadedSettings.api_use_mancer_webui) { |
| 556 | 561 | settingstextgenerationwebui_settings.type = MANCER; |
| 557 | 562 | } |
| 558 | 563 | |
| 559 | 564 | for (const name of textgenerationwebui_preset_names) { |
| @@ -563,20 +568,20 @@ export async function loadTextGenSettings(data, loadedSettings) { | ||
| 563 | 568 | $('#settings_preset_textgenerationwebui').append(option); |
| 564 | 569 | } |
| 565 | 570 | |
| 566 | 571 | if (settingstextgenerationwebui_settings.preset) { |
| 567 | 572 | $('#settings_preset_textgenerationwebui').val(settingstextgenerationwebui_settings.preset); |
| 568 | 573 | } |
| 569 | 574 | |
| 570 | 575 | for (const i of setting_names) { |
| 571 | 576 | const value = settingstextgenerationwebui_settings[i]; |
| 572 | 577 | setSettingByName(i, value); |
| 573 | 578 | } |
| 574 | 579 | |
| 575 | 580 | $('#textgen_type').val(settingstextgenerationwebui_settings.type); |
| 576 | 581 | $('#openrouter_providers_text').val(settingstextgenerationwebui_settings.openrouter_providers).trigger('change'); |
| 577 | 582 | showSamplerControls(settingstextgenerationwebui_settings.type); |
| 578 | 583 | BIAS_CACHE.delete(BIAS_KEY); |
| 579 | 584 | displayLogitBias(settingstextgenerationwebui_settings.logit_bias, BIAS_KEY); |
| 580 | 585 | |
| 581 | 586 | registerDebugFunction('change-mancer-url', 'Change Mancer base URL', 'Change Mancer API server base URL', () => { |
| 582 | 587 | const result = prompt(`Enter Mancer base URL\nDefault: ${MANCER_SERVER_DEFAULT}`, MANCER_SERVER); |
| @@ -648,7 +653,7 @@ async function getStatusTextgen() { | ||
| 648 | 653 | return resultCheckStatus(); |
| 649 | 654 | } |
| 650 | 655 | |
| 651 | 656 | if ([textgen_types.GENERIC, textgen_types.OOBA].includes(settingstextgenerationwebui_settings.type) && settingstextgenerationwebui_settings.bypass_status_check) { |
| 652 | 657 | setOnlineStatus(t`Status check bypassed`); |
| 653 | 658 | return resultCheckStatus(); |
| 654 | 659 | } |
| @@ -659,46 +664,46 @@ async function getStatusTextgen() { | ||
| 659 | 664 | headers: getRequestHeaders(), |
| 660 | 665 | body: JSON.stringify({ |
| 661 | 666 | api_server: endpoint, |
| 662 | 667 | api_type: settingstextgenerationwebui_settings.type, |
| 663 | 668 | }), |
| 664 | 669 | signal: abortStatusCheck.signal, |
| 665 | 670 | }); |
| 666 | 671 | |
| 667 | 672 | const data = await response.json(); |
| 668 | 673 | |
| 669 | 674 | if (settingstextgenerationwebui_settings.type === textgen_types.MANCER) { |
| 670 | 675 | loadMancerModels(data?.data); |
| 671 | 676 | setOnlineStatus(settingstextgenerationwebui_settings.mancer_model); |
| 672 | 677 | } else if (settingstextgenerationwebui_settings.type === textgen_types.TOGETHERAI) { |
| 673 | 678 | loadTogetherAIModels(data?.data); |
| 674 | 679 | setOnlineStatus(settingstextgenerationwebui_settings.togetherai_model); |
| 675 | 680 | } else if (settingstextgenerationwebui_settings.type === textgen_types.OLLAMA) { |
| 676 | 681 | loadOllamaModels(data?.data); |
| 677 | 682 | setOnlineStatus(settingstextgenerationwebui_settings.ollama_model || t`Connected`); |
| 678 | 683 | } else if (settingstextgenerationwebui_settings.type === textgen_types.INFERMATICAI) { |
| 679 | 684 | loadInfermaticAIModels(data?.data); |
| 680 | 685 | setOnlineStatus(settingstextgenerationwebui_settings.infermaticai_model); |
| 681 | 686 | } else if (settingstextgenerationwebui_settings.type === textgen_types.DREAMGEN) { |
| 682 | 687 | loadDreamGenModels(data?.data); |
| 683 | 688 | setOnlineStatus(settingstextgenerationwebui_settings.dreamgen_model); |
| 684 | 689 | } else if (settingstextgenerationwebui_settings.type === textgen_types.OPENROUTER) { |
| 685 | 690 | loadOpenRouterModels(data?.data); |
| 686 | 691 | setOnlineStatus(settingstextgenerationwebui_settings.openrouter_model); |
| 687 | 692 | } else if (settingstextgenerationwebui_settings.type === textgen_types.VLLM) { |
| 688 | 693 | loadVllmModels(data?.data); |
| 689 | 694 | setOnlineStatus(settingstextgenerationwebui_settings.vllm_model); |
| 690 | 695 | } else if (settingstextgenerationwebui_settings.type === textgen_types.APHRODITE) { |
| 691 | 696 | loadAphroditeModels(data?.data); |
| 692 | 697 | setOnlineStatus(settingstextgenerationwebui_settings.aphrodite_model); |
| 693 | 698 | } else if (settingstextgenerationwebui_settings.type === textgen_types.FEATHERLESS) { |
| 694 | 699 | loadFeatherlessModels(data?.data); |
| 695 | 700 | setOnlineStatus(settingstextgenerationwebui_settings.featherless_model); |
| 696 | 701 | } else if (settingstextgenerationwebui_settings.type === textgen_types.TABBY) { |
| 697 | 702 | loadTabbyModels(data?.data); |
| 698 | 703 | setOnlineStatus(settingstextgenerationwebui_settings.tabby_model || data?.result); |
| 699 | 704 | } else if (settingstextgenerationwebui_settings.type === textgen_types.GENERIC) { |
| 700 | 705 | loadGenericModels(data?.data); |
| 701 | 706 | setOnlineStatus(settingstextgenerationwebui_settings.generic_model || data?.result || t`Connected`); |
| 702 | 707 | } else { |
| 703 | 708 | setOnlineStatus(data?.result); |
| 704 | 709 | } |
| @@ -718,7 +723,7 @@ async function getStatusTextgen() { | ||
| 718 | 723 | const wantsInstructDerivation = !autoSelected && (power_user.instruct.enabled && power_user.instruct_derived); |
| 719 | 724 | const wantsContextDerivation = !autoSelected && power_user.context_derived; |
| 720 | 725 | const wantsContextSize = power_user.context_size_derived; |
| 721 | 726 | const supportsChatTemplate = [textgen_types.KOBOLDCPP, textgen_types.LLAMACPP].includes(settingstextgenerationwebui_settings.type); |
| 722 | 727 | |
| 723 | 728 | if (supportsChatTemplate && (wantsInstructDerivation || wantsContextDerivation || wantsContextSize)) { |
| 724 | 729 | const response = await fetch('/api/backends/text-completions/props', { |
| @@ -726,7 +731,7 @@ async function getStatusTextgen() { | ||
| 726 | 731 | headers: getRequestHeaders(), |
| 727 | 732 | body: JSON.stringify({ |
| 728 | 733 | api_server: endpoint, |
| 729 | 734 | api_type: settingstextgenerationwebui_settings.type, |
| 730 | 735 | }), |
| 731 | 736 | }); |
| 732 | 737 | |
| @@ -795,15 +800,15 @@ export function initTextGenSettings() { | ||
| 795 | 800 | $('#koboldcpp_order').children().each(function () { |
| 796 | 801 | order.push($(this).data('id')); |
| 797 | 802 | }); |
| 798 | 803 | settingstextgenerationwebui_settings.sampler_order = order; |
| 799 | 804 | console.log('Samplers reordered:', settingstextgenerationwebui_settings.sampler_order); |
| 800 | 805 | saveSettingsDebounced(); |
| 801 | 806 | }, |
| 802 | 807 | }); |
| 803 | 808 | |
| 804 | 809 | $('#koboldcpp_default_order').on('click', function () { |
| 805 | 810 | settingstextgenerationwebui_settings.sampler_order = KOBOLDCPP_ORDER; |
| 806 | 811 | sortKoboldItemsByOrder(settingstextgenerationwebui_settings.sampler_order); |
| 807 | 812 | saveSettingsDebounced(); |
| 808 | 813 | }); |
| 809 | 814 | |
| @@ -814,16 +819,16 @@ export function initTextGenSettings() { | ||
| 814 | 819 | $('#llamacpp_samplers_sortable').children().each(function () { |
| 815 | 820 | order.push($(this).data('name')); |
| 816 | 821 | }); |
| 817 | 822 | settingstextgenerationwebui_settings.samplers = order; |
| 818 | 823 | console.log('Samplers reordered:', settingstextgenerationwebui_settings.samplers); |
| 819 | 824 | saveSettingsDebounced(); |
| 820 | 825 | }, |
| 821 | 826 | }); |
| 822 | 827 | |
| 823 | 828 | $('#llamacpp_samplers_default_order').on('click', function () { |
| 824 | 829 | sortLlamacppItemsByOrder(LLAMACPP_DEFAULT_ORDER); |
| 825 | 830 | settingstextgenerationwebui_settings.samplers = LLAMACPP_DEFAULT_ORDER; |
| 826 | 831 | console.log('Default samplers order loaded:', settingstextgenerationwebui_settings.samplers); |
| 827 | 832 | saveSettingsDebounced(); |
| 828 | 833 | }); |
| 829 | 834 | |
| @@ -834,8 +839,8 @@ export function initTextGenSettings() { | ||
| 834 | 839 | $('#sampler_priority_container').children().each(function () { |
| 835 | 840 | order.push($(this).data('name')); |
| 836 | 841 | }); |
| 837 | 842 | settingstextgenerationwebui_settings.sampler_priority = order; |
| 838 | 843 | console.log('Samplers reordered:', settingstextgenerationwebui_settings.sampler_priority); |
| 839 | 844 | saveSettingsDebounced(); |
| 840 | 845 | }, |
| 841 | 846 | }); |
| @@ -847,8 +852,8 @@ export function initTextGenSettings() { | ||
| 847 | 852 | $('#sampler_priority_container_aphrodite').children().each(function () { |
| 848 | 853 | order.push($(this).data('name')); |
| 849 | 854 | }); |
| 850 | 855 | settingstextgenerationwebui_settings.samplers_priorities = order; |
| 851 | 856 | console.log('Samplers reordered:', settingstextgenerationwebui_settings.samplers_priorities); |
| 852 | 857 | saveSettingsDebounced(); |
| 853 | 858 | }, |
| 854 | 859 | }); |
| @@ -858,12 +863,12 @@ export function initTextGenSettings() { | ||
| 858 | 863 | |
| 859 | 864 | if (json_schema_string) { |
| 860 | 865 | try { |
| 861 | 866 | settingstextgenerationwebui_settings.json_schema = JSON.parse(json_schema_string); |
| 862 | 867 | } catch { |
| 863 | 868 | settingstextgenerationwebui_settings.json_schema = null; |
| 864 | 869 | } |
| 865 | 870 | } else { |
| 866 | 871 | settingstextgenerationwebui_settings.json_schema = null; |
| 867 | 872 | } |
| 868 | 873 | |
| 869 | 874 | saveSettingsDebounced(); |
| @@ -871,38 +876,38 @@ export function initTextGenSettings() { | ||
| 871 | 876 | |
| 872 | 877 | $('#textgenerationwebui_default_order').on('click', function () { |
| 873 | 878 | sortOobaItemsByOrder(OOBA_DEFAULT_ORDER); |
| 874 | 879 | settingstextgenerationwebui_settings.sampler_priority = OOBA_DEFAULT_ORDER; |
| 875 | 880 | console.log('Default samplers order loaded:', settingstextgenerationwebui_settings.sampler_priority); |
| 876 | 881 | saveSettingsDebounced(); |
| 877 | 882 | }); |
| 878 | 883 | |
| 879 | 884 | $('#aphrodite_default_order').on('click', function () { |
| 880 | 885 | sortAphroditeItemsByOrder(APHRODITE_DEFAULT_ORDER); |
| 881 | 886 | settingstextgenerationwebui_settings.samplers_priorities = APHRODITE_DEFAULT_ORDER; |
| 882 | 887 | console.log('Default samplers order loaded:', settingstextgenerationwebui_settings.samplers_priorities); |
| 883 | 888 | saveSettingsDebounced(); |
| 884 | 889 | }); |
| 885 | 890 | |
| 886 | 891 | $('#textgen_type').on('change', function () { |
| 887 | 892 | const type = String($(this).val()); |
| 888 | 893 | settingstextgenerationwebui_settings.type = type; |
| 889 | 894 | |
| 890 | 895 | if ([VLLM, APHRODITE, INFERMATICAI].includes(settingstextgenerationwebui_settings.type)) { |
| 891 | 896 | $('#mirostat_mode_textgenerationwebui').attr('step', 2); //Aphro disallows mode 1 |
| 892 | 897 | $('#do_sample_textgenerationwebui').prop('checked', true); //Aphro should always do sample; 'otherwise set temp to 0 to mimic no sample' |
| 893 | 898 | $('#ban_eos_token_textgenerationwebui').prop('checked', false); //Aphro should not ban EOS, just ignore it; 'add token '2' to ban list do to this' |
| 894 | 899 | //special handling for vLLM/Aphrodite topK -1 disable state |
| 895 | 900 | $('#top_k_textgenerationwebui').attr('min', -1); |
| 896 | 901 | if ($('#top_k_textgenerationwebui').val() === '0' || settingstextgenerationwebui_settings['top_k'] === 0) { |
| 897 | 902 | settingstextgenerationwebui_settings['top_k'] = -1; |
| 898 | 903 | $('#top_k_textgenerationwebui').val('-1').trigger('input'); |
| 899 | 904 | } |
| 900 | 905 | } else { |
| 901 | 906 | $('#mirostat_mode_textgenerationwebui').attr('step', 1); |
| 902 | 907 | //undo special vLLM/Aphrodite setup for topK |
| 903 | 908 | $('#top_k_textgenerationwebui').attr('min', 0); |
| 904 | 909 | if ($('#top_k_textgenerationwebui').val() === '-1' || settingstextgenerationwebui_settings['top_k'] === -1) { |
| 905 | 910 | settingstextgenerationwebui_settings['top_k'] = 0; |
| 906 | 911 | $('#top_k_textgenerationwebui').val('0').trigger('input'); |
| 907 | 912 | } |
| 908 | 913 | } |
| @@ -913,7 +918,7 @@ export function initTextGenSettings() { | ||
| 913 | 918 | |
| 914 | 919 | $('#main_api').trigger('change'); |
| 915 | 920 | |
| 916 | 921 | if (!SERVER_INPUTS[type] || settingstextgenerationwebui_settings.server_urls[type]) { |
| 917 | 922 | $('#api_button_textgenerationwebui').trigger('click'); |
| 918 | 923 | } |
| 919 | 924 | |
| @@ -929,7 +934,7 @@ export function initTextGenSettings() { | ||
| 929 | 934 | $('#samplerResetButton').off('click').on('click', function () { |
| 930 | 935 | const inputs = { |
| 931 | 936 | 'temp_textgenerationwebui': 1, |
| 932 | 937 | 'top_k_textgenerationwebui': [INFERMATICAI, APHRODITE, VLLM].includes(settingstextgenerationwebui_settings.type) ? -1 : 0, |
| 933 | 938 | 'top_p_textgenerationwebui': 1, |
| 934 | 939 | 'min_p_textgenerationwebui': 0, |
| 935 | 940 | 'rep_pen_textgenerationwebui': 1, |
| @@ -1007,19 +1012,19 @@ export function initTextGenSettings() { | ||
| 1007 | 1012 | |
| 1008 | 1013 | if (isCheckbox) { |
| 1009 | 1014 | const value = $(this).prop('checked'); |
| 1010 | 1015 | settingstextgenerationwebui_settings[id] = value; |
| 1011 | 1016 | } |
| 1012 | 1017 | else if (isText) { |
| 1013 | 1018 | const value = $(this).val(); |
| 1014 | 1019 | settingstextgenerationwebui_settings[id] = value; |
| 1015 | 1020 | } |
| 1016 | 1021 | else { |
| 1017 | 1022 | const value = Number($(this).val()); |
| 1018 | 1023 | $(`#${id}_counter_textgenerationwebui`).val(value); |
| 1019 | 1024 | settingstextgenerationwebui_settings[id] = value; |
| 1020 | 1025 | //special handling for vLLM/Aphrodite using -1 as disabled instead of 0 |
| 1021 | 1026 | if ($(this).attr('id') === 'top_k_textgenerationwebui' && [INFERMATICAI, APHRODITE, VLLM].includes(settingstextgenerationwebui_settings.type) && value === 0) { |
| 1022 | 1027 | settingstextgenerationwebui_settings[id] = -1; |
| 1023 | 1028 | $(this).val(-1); |
| 1024 | 1029 | } |
| 1025 | 1030 | } |
| @@ -1027,7 +1032,7 @@ export function initTextGenSettings() { | ||
| 1027 | 1032 | }); |
| 1028 | 1033 | } |
| 1029 | 1034 | |
| 1030 | 1035 | $('#textgen_logit_bias_new_entry').on('click', () => createNewLogitBiasEntry(settingstextgenerationwebui_settings.logit_bias, BIAS_KEY)); |
| 1031 | 1036 | |
| 1032 | 1037 | $('#openrouter_providers_text').on('change', function () { |
| 1033 | 1038 | const selectedProviders = $(this).val(); |
| @@ -1037,7 +1042,7 @@ export function initTextGenSettings() { | ||
| 1037 | 1042 | return; |
| 1038 | 1043 | } |
| 1039 | 1044 | |
| 1040 | 1045 | settingstextgenerationwebui_settings.openrouter_providers = selectedProviders; |
| 1041 | 1046 | |
| 1042 | 1047 | saveSettingsDebounced(); |
| 1043 | 1048 | }); |
| @@ -1086,10 +1091,10 @@ function showSamplerControls(apiType = null) { | ||
| 1086 | 1091 | if (!typeSpecificControlled) $(this).show(); |
| 1087 | 1092 | }); |
| 1088 | 1093 | |
| 1089 | 1094 | showTypeSpecificControls(apiType ?? settingstextgenerationwebui_settings.type); |
| 1090 | 1095 | |
| 1091 | 1096 | const prioritizeManualSamplerSelect = isSamplerManualPriorityEnabled(apiType ?? settingstextgenerationwebui_settings.type); |
| 1092 | 1097 | const samplersActivatedManually = getActiveManualApiSamplers(apiType ?? settingstextgenerationwebui_settings.type); |
| 1093 | 1098 | |
| 1094 | 1099 | if (!samplersActivatedManually?.length || !prioritizeManualSamplerSelect) return; |
| 1095 | 1100 | |
| @@ -1150,13 +1155,13 @@ function insertMissingArrayItems(source, target) { | ||
| 1150 | 1155 | function setSettingByName(setting, value, trigger) { |
| 1151 | 1156 | if ('extensions' === setting) { |
| 1152 | 1157 | value = value || {}; |
| 1153 | 1158 | settingstextgenerationwebui_settings.extensions = value; |
| 1154 | 1159 | return; |
| 1155 | 1160 | } |
| 1156 | 1161 | |
| 1157 | 1162 | if ('json_schema' === setting) { |
| 1158 | 1163 | settingstextgenerationwebui_settings.json_schema = value ?? null; |
| 1159 | 1164 | $('#tabby_json_schema').val(value ? JSON.stringify(settingstextgenerationwebui_settings.json_schema, null, 2) : ''); |
| 1160 | 1165 | return; |
| 1161 | 1166 | } |
| 1162 | 1167 | |
| @@ -1167,7 +1172,7 @@ function setSettingByName(setting, value, trigger) { | ||
| 1167 | 1172 | if ('sampler_order' === setting) { |
| 1168 | 1173 | value = Array.isArray(value) ? value : KOBOLDCPP_ORDER; |
| 1169 | 1174 | sortKoboldItemsByOrder(value); |
| 1170 | 1175 | settingstextgenerationwebui_settings.sampler_order = value; |
| 1171 | 1176 | return; |
| 1172 | 1177 | } |
| 1173 | 1178 | |
| @@ -1175,7 +1180,7 @@ function setSettingByName(setting, value, trigger) { | ||
| 1175 | 1180 | value = Array.isArray(value) ? value : OOBA_DEFAULT_ORDER; |
| 1176 | 1181 | insertMissingArrayItems(OOBA_DEFAULT_ORDER, value); |
| 1177 | 1182 | sortOobaItemsByOrder(value); |
| 1178 | 1183 | settingstextgenerationwebui_settings.sampler_priority = value; |
| 1179 | 1184 | return; |
| 1180 | 1185 | } |
| 1181 | 1186 | |
| @@ -1183,7 +1188,7 @@ function setSettingByName(setting, value, trigger) { | ||
| 1183 | 1188 | value = Array.isArray(value) ? value : APHRODITE_DEFAULT_ORDER; |
| 1184 | 1189 | insertMissingArrayItems(APHRODITE_DEFAULT_ORDER, value); |
| 1185 | 1190 | sortAphroditeItemsByOrder(value); |
| 1186 | 1191 | settingstextgenerationwebui_settings.samplers_priorities = value; |
| 1187 | 1192 | return; |
| 1188 | 1193 | } |
| 1189 | 1194 | |
| @@ -1191,12 +1196,12 @@ function setSettingByName(setting, value, trigger) { | ||
| 1191 | 1196 | value = Array.isArray(value) ? value : LLAMACPP_DEFAULT_ORDER; |
| 1192 | 1197 | insertMissingArrayItems(LLAMACPP_DEFAULT_ORDER, value); |
| 1193 | 1198 | sortLlamacppItemsByOrder(value); |
| 1194 | 1199 | settingstextgenerationwebui_settings.samplers = value; |
| 1195 | 1200 | return; |
| 1196 | 1201 | } |
| 1197 | 1202 | |
| 1198 | 1203 | if ('logit_bias' === setting) { |
| 1199 | 1204 | settingstextgenerationwebui_settings.logit_bias = Array.isArray(value) ? value : []; |
| 1200 | 1205 | return; |
| 1201 | 1206 | } |
| 1202 | 1207 | |
| @@ -1234,8 +1239,8 @@ function setSettingByName(setting, value, trigger) { | ||
| 1234 | 1239 | |
| 1235 | 1240 | /** |
| 1236 | 1241 | * Sends a streaming request for textgenerationwebui. |
| 1237 | 1242 | * @param {object} generate_data |
| 1238 | 1243 | * @param {AbortSignal} signal |
| 1239 | 1244 | * @returns {Promise<(function(): AsyncGenerator<{swipes: [], text: string, toolCalls: [], logprobs: {token: string, topLogprobs: Candidate[]}|null}, void, *>)|*>} |
| 1240 | 1245 | * @throws {Error} - If the response status is not OK, or from within the generator |
| 1241 | 1246 | */ |
| @@ -1304,7 +1309,7 @@ export function parseTextgenLogprobs(token, logprobs) { | ||
| 1304 | 1309 | return null; |
| 1305 | 1310 | } |
| 1306 | 1311 | |
| 1307 | 1312 | switch (settingstextgenerationwebui_settings.type) { |
| 1308 | 1313 | case KOBOLDCPP: |
| 1309 | 1314 | case TABBY: |
| 1310 | 1315 | case VLLM: |
| @@ -1406,7 +1411,13 @@ function toIntArray(string) { | ||
| 1406 | 1411 | return string.split(',').map(x => parseInt(x)).filter(x => !isNaN(x)); |
| 1407 | 1412 | } |
| 1408 | 1413 | |
| 1409 | -export function getTextGenModel() { | |
| 1414 | +/** | |
| 1415 | + * Gets the text generation model specified by the given text completion settings | |
| 1416 | + * @param {TextCompletionSettings} settings Text completion settings to use | |
| 1417 | + * @returns {string} model name | |
| 1418 | + */ | |
| 1419 | +export function getTextGenModel(settings = null) { | |
| 1420 | + settings = settings ?? textgenerationwebui_settings; | |
| 1410 | 1421 | switch (settings.type) { |
| 1411 | 1422 | case OOBA: |
| 1412 | 1423 | if (settings.custom_model) { |
| @@ -1455,10 +1466,16 @@ export function getTextGenModel() { | ||
| 1455 | 1466 | } |
| 1456 | 1467 | |
| 1457 | 1468 | export function isJsonSchemaSupported() { |
| 1458 | 1469 | return [TABBY, LLAMACPP].includes(settingstextgenerationwebui_settings.type) && main_api === 'textgenerationwebui'; |
| 1459 | 1470 | } |
| 1460 | 1471 | |
| 1461 | -function isDynamicTemperatureSupported() { | |
| 1472 | +/** | |
| 1473 | + * Returns whether dynamic temperature is supported by the given text completion settings | |
| 1474 | + * @param {TextCompletionSettings} settings Text completion settings to use | |
| 1475 | + * @returns {boolean} Whether dynamic temperature supported | |
| 1476 | + */ | |
| 1477 | +function isDynamicTemperatureSupported(settings = null) { | |
| 1478 | + settings = settings ?? textgenerationwebui_settings; | |
| 1462 | 1479 | return settings.dynatemp && DYNATEMP_BLOCK?.dataset?.tgType?.includes(settings.type); |
| 1463 | 1480 | } |
| 1464 | 1481 | |
| @@ -1468,7 +1485,7 @@ function isDynamicTemperatureSupported() { | ||
| 1468 | 1485 | * @returns {number} Number of logprobs to request |
| 1469 | 1486 | */ |
| 1470 | 1487 | export function getLogprobsNumber(type = null) { |
| 1471 | 1488 | const selectedType = type ?? settingstextgenerationwebui_settings.type; |
| 1472 | 1489 | if (selectedType === VLLM || selectedType === INFERMATICAI) { |
| 1473 | 1490 | return 5; |
| 1474 | 1491 | } |
| @@ -1504,10 +1521,25 @@ export function replaceMacrosInList(str) { | ||
| 1504 | 1521 | } |
| 1505 | 1522 | } |
| 1506 | 1523 | |
| 1507 | -export async function getTextGenGenerationData(finalPrompt, maxTokens, isImpersonate, isContinue, cfgValues, type) { | |
| 1524 | +/** | |
| 1525 | + * Build the generation parameter object for an text completion request | |
| 1526 | + * @param {TextCompletionSettings} settings Text completion settings to use | |
| 1527 | + * @param {string} model Model to use | |
| 1528 | + * @param {string} finalPrompt The final prompt to send | |
| 1529 | + * @param {number} maxTokens Max allowed generation tokens | |
| 1530 | + * @param {boolean} isImpersonate Whether this is for an impersonation | |
| 1531 | + * @param {boolean} isContinue Whether this is for a continue | |
| 1532 | + * @param {object} cfgValues Additional parameters (guidanceScale, negativePrompt) | |
| 1533 | + * @param {string} type Request type (impersonate, quiet, continue, etc) | |
| 1534 | + * @returns {object} Final generation parameters object appropriate for the text completion source | |
| 1535 | + */ | |
| 1536 | +export function createTextGenGenerationData(settings, model, finalPrompt = null, maxTokens = null, isImpersonate = false, isContinue = false, cfgValues = null, type = 'quiet') { | |
| 1537 | + settings = settings ?? textgenerationwebui_settings; | |
| 1538 | + model = model ?? getTextGenModel(settings); | |
| 1539 | + | |
| 1508 | 1540 | const canMultiSwipe = !isContinue && !isImpersonate && type !== 'quiet'; |
| 1509 | 1541 | const dynatemp = isDynamicTemperatureSupported(settings); |
| 1510 | 1542 | const { banned_tokens, banned_strings } = getCustomTokenBans(settings); |
| 1511 | 1543 | const jsonSchema = isObject(settings.json_schema) |
| 1512 | 1544 | ? settings.json_schema_allow_empty |
| 1513 | 1545 | ? settings.json_schema |
| @@ -1516,10 +1548,10 @@ export async function getTextGenGenerationData(finalPrompt, maxTokens, isImperso | ||
| 1516 | 1548 | |
| 1517 | 1549 | let params = { |
| 1518 | 1550 | 'prompt': finalPrompt, |
| 1519 | 1551 | 'model': getTextGenModel()model, |
| 1520 | 1552 | 'max_new_tokens': maxTokens, |
| 1521 | 1553 | 'max_tokens': maxTokens, |
| 1522 | 1554 | 'logprobs': power_user.request_token_probabilities ? getLogprobsNumber(settings.type) : undefined, |
| 1523 | 1555 | 'temperature': dynatemp ? (settings.min_temp + settings.max_temp) / 2 : settings.temp, |
| 1524 | 1556 | 'top_p': settings.top_p, |
| 1525 | 1557 | 'typical_p': settings.typical_p, |
| @@ -1571,7 +1603,7 @@ export async function getTextGenGenerationData(finalPrompt, maxTokens, isImperso | ||
| 1571 | 1603 | banned_tokens, |
| 1572 | 1604 | 'banned_strings': banned_strings, |
| 1573 | 1605 | 'api_type': settings.type, |
| 1574 | 1606 | 'api_server': getTextGenServer(settings.type), |
| 1575 | 1607 | 'sampler_order': settings.type === textgen_types.KOBOLDCPP ? settings.sampler_order : undefined, |
| 1576 | 1608 | 'xtc_threshold': settings.xtc_threshold, |
| 1577 | 1609 | 'xtc_probability': settings.xtc_probability, |
| @@ -1713,7 +1745,7 @@ export async function getTextGenGenerationData(finalPrompt, maxTokens, isImperso | ||
| 1713 | 1745 | } |
| 1714 | 1746 | |
| 1715 | 1747 | if (Array.isArray(settings.logit_bias) && settings.logit_bias.length) { |
| 1716 | 1748 | const logitBias = BIAS_CACHE.get(BIAS_KEY) || calculateLogitBias(settings); |
| 1717 | 1749 | BIAS_CACHE.set(BIAS_KEY, logitBias); |
| 1718 | 1750 | params.logit_bias = logitBias; |
| 1719 | 1751 | } |
| @@ -1750,8 +1782,12 @@ export async function getTextGenGenerationData(finalPrompt, maxTokens, isImperso | ||
| 1750 | 1782 | delete params.guided_json; |
| 1751 | 1783 | } |
| 1752 | 1784 | } |
| 1785 | + return params; | |
| 1786 | +} | |
| 1753 | 1787 | |
| 1788 | +export async function getTextGenGenerationData(finalPrompt, maxTokens, isImpersonate, isContinue, cfgValues, type) { | |
| 1789 | + const model = getTextGenModel(textgenerationwebui_settings); | |
| 1790 | + const params = createTextGenGenerationData(textgenerationwebui_settings, model, finalPrompt, maxTokens, isImpersonate, isContinue, cfgValues, type); | |
| 1754 | 1791 | await eventSource.emit(event_types.TEXT_COMPLETION_SETTINGS_READY, params); |
| 1755 | - | |
| 1756 | 1792 | return params; |
| 1757 | 1793 | } |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | import { DOMPurify } from '../lib.js'; |
| 2 | 2 | |
| 3 | 3 | import { addOneMessage, chat, event_types, eventSource, main_api, saveChatConditional, system_avatar, systemUserName } from '../script.js'; |
| 4 | 4 | import { chat_completion_sources, custom_prompt_post_processing_types, getChatCompletionModel, model_list, oai_settings } from './openai.js'; |
| 5 | 5 | import { Popup } from './popup.js'; |
| 6 | 6 | import { SlashCommand } from './slash-commands/SlashCommand.js'; |
| 7 | 7 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; |
| @@ -589,66 +589,42 @@ export class ToolManager { | ||
| 589 | 589 | |
| 590 | 590 | /** |
| 591 | 591 | * Checks if tool calling is supported for the current settings and generation type. |
| 592 | + * @param {ChatCompletionSettings} settings Optional chat completion settings | |
| 593 | + * @param {string} model Optional model name | |
| 592 | 594 | * @returns {boolean} Whether tool calling is supported for the given type |
| 593 | 595 | */ |
| 594 | 596 | static isToolCallingSupported(settings = null, model = null) { |
| 595 | - if (main_api !== 'openai' || !oai_settings.function_calling) { | |
| 597 | + settings = settings ?? oai_settings; | |
| 598 | + model = model ?? getChatCompletionModel(settings); | |
| 599 | + | |
| 600 | + if (main_api !== 'openai' || !settings.function_calling) { | |
| 596 | 601 | return false; |
| 597 | 602 | } |
| 598 | 603 | |
| 599 | 604 | // Post-processing will forcefully remove past tool calls from the prompt, making them useless |
| 600 | 605 | const { NONE, MERGE_TOOLS, SEMI_TOOLS, STRICT_TOOLS } = custom_prompt_post_processing_types; |
| 601 | 606 | const allowedPromptPostProcessing = [NONE, MERGE_TOOLS, SEMI_TOOLS, STRICT_TOOLS]; |
| 602 | 607 | if (!allowedPromptPostProcessing.includes(oai_settingssettings.custom_prompt_post_processing)) { |
| 603 | 608 | return false; |
| 604 | 609 | } |
| 605 | 610 | |
| 606 | - if (oai_settings.chat_completion_source === chat_completion_sources.POLLINATIONS && Array.isArray(model_list)) { | |
| 611 | + const currentModel = Array.isArray(model_list) ? model_list.find(m => m.id === model) : null; | |
| 607 | - const currentModel = model_list.find(model => model.id === oai_settings.pollinations_model); | |
| 612 | + if (currentModel) { | |
| 608 | 613 | ifswitch (currentModelsettings.chat_completion_source) { |
| 609 | - return currentModel.tools; | |
| 614 | + case chat_completion_sources.POLLINATIONS: | |
| 610 | - } | |
| 615 | + return currentModel.tools; | |
| 611 | - } | |
| 616 | + case chat_completion_sources.FIREWORKS: | |
| 612 | - | |
| 617 | + return currentModel.supports_tools; | |
| 613 | - if (oai_settings.chat_completion_source === chat_completion_sources.FIREWORKS && Array.isArray(model_list)) { | |
| 618 | + case chat_completion_sources.OPENROUTER: | |
| 614 | - const currentModel = model_list.find(model => model.id === oai_settings.fireworks_model); | |
| 619 | + return currentModel.supported_parameters?.includes('tools'); | |
| 615 | - if (currentModel) { | |
| 620 | + case chat_completion_sources.MISTRALAI: | |
| 616 | 621 | return currentModel.supports_toolscapabilities?.function_calling; |
| 617 | - } | |
| 622 | + case chat_completion_sources.AIMLAPI: | |
| 618 | - } | |
| 623 | + return currentModel.features?.includes('openai/chat-completion.function'); | |
| 619 | - | |
| 624 | + case chat_completion_sources.CHUTES: | |
| 620 | - if (oai_settings.chat_completion_source === chat_completion_sources.OPENROUTER && Array.isArray(model_list)) { | |
| 625 | + return currentModel.supported_features?.includes('tools'); | |
| 621 | - const currentModel = model_list.find(model => model.id === oai_settings.openrouter_model); | |
| 626 | + case chat_completion_sources.ELECTRONHUB: | |
| 622 | - if (Array.isArray(currentModel?.supported_parameters)) { | |
| 627 | + return currentModel.metadata?.function_call; | |
| 623 | - return currentModel.supported_parameters.includes('tools'); | |
| 624 | - } | |
| 625 | - } | |
| 626 | - | |
| 627 | - if (oai_settings.chat_completion_source === chat_completion_sources.MISTRALAI && Array.isArray(model_list)) { | |
| 628 | - const currentModel = model_list.find(model => model.id === oai_settings.mistralai_model); | |
| 629 | - if (currentModel && currentModel.capabilities) { | |
| 630 | - return currentModel.capabilities.function_calling; | |
| 631 | - } | |
| 632 | - } | |
| 633 | - | |
| 634 | - if (oai_settings.chat_completion_source === chat_completion_sources.AIMLAPI && Array.isArray(model_list)) { | |
| 635 | - const currentModel = model_list.find(model => model.id === oai_settings.aimlapi_model); | |
| 636 | - if (Array.isArray(currentModel?.features)) { | |
| 637 | - return currentModel.features.includes('openai/chat-completion.function'); | |
| 638 | - } | |
| 639 | - } | |
| 640 | - | |
| 641 | - if (oai_settings.chat_completion_source === chat_completion_sources.CHUTES && Array.isArray(model_list)) { | |
| 642 | - const currentModel = model_list.find(model => model.id === oai_settings.chutes_model); | |
| 643 | - if (currentModel) { | |
| 644 | - return currentModel.supported_features?.includes('tools'); | |
| 645 | - } | |
| 646 | - } | |
| 647 | - | |
| 648 | - if (oai_settings.chat_completion_source === chat_completion_sources.ELECTRONHUB && Array.isArray(model_list)) { | |
| 649 | - const currentModel = model_list.find(model => model.id === oai_settings.electronhub_model); | |
| 650 | - if (currentModel) { | |
| 651 | - return currentModel.metadata?.function_call; | |
| 652 | 628 | } |
| 653 | 629 | } |
| 654 | 630 | |
| @@ -676,17 +652,21 @@ export class ToolManager { | ||
| 676 | 652 | chat_completion_sources.ZAI, |
| 677 | 653 | chat_completion_sources.SILICONFLOW, |
| 678 | 654 | ]; |
| 679 | 655 | return supportedSources.includes(oai_settingssettings.chat_completion_source); |
| 680 | 656 | } |
| 681 | 657 | |
| 682 | 658 | /** |
| 683 | 659 | * Checks if tool calls can be performed for the current settings and generation type. |
| 684 | 660 | * @param {string} type Generation type |
| 661 | + * @param {ChatCompletionSettings} settings Optional chat completion settings | |
| 662 | + * @param {string} model Optional model name | |
| 685 | 663 | * @returns {boolean} Whether tool calls can be performed for the given type |
| 686 | 664 | */ |
| 687 | 665 | static canPerformToolCalls(type, settings = null, model = null) { |
| 666 | + settings = settings ?? oai_settings; | |
| 667 | + model = model ?? getChatCompletionModel(settings); | |
| 688 | 668 | const noToolCallTypes = ['impersonate', 'quiet', 'continue']; |
| 689 | 669 | const isSupported = ToolManager.isToolCallingSupported(settings, model); |
| 690 | 670 | return isSupported && !noToolCallTypes.includes(type); |
| 691 | 671 | } |
| 692 | 672 | |