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>
This commit is contained in:
@@ -397,7 +397,7 @@ export class ConnectionManagerRequestService {
|
||||
throw new Error('Connection Manager is not available');
|
||||
}
|
||||
|
||||
const profile = context.extensionSettings.connectionManager.profiles.find((p) => p.id === profileId);
|
||||
const profile = this.getProfile(profileId);
|
||||
const selectedApiMap = this.validateProfile(profile);
|
||||
|
||||
try {
|
||||
@@ -453,6 +453,38 @@ export class ConnectionManagerRequestService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If using text completion, return a formatted prompt string given an array of messages, a given profile ID, and optional instruct settings.
|
||||
* If using chat completion, simply return the given prompt as-is.
|
||||
* @param {ChatCompletionMessage[]} prompt An array of prompt messages.
|
||||
* @param {string} profileId ID of a given connection profile (from which to infer a completion preset).
|
||||
* @param {InstructSettings} instructSettings optional instruct settings
|
||||
*/
|
||||
static constructPrompt(prompt, profileId, instructSettings = null) {
|
||||
const context = SillyTavern.getContext();
|
||||
const profile = this.getProfile(profileId);
|
||||
const selectedApiMap = this.validateProfile(profile);
|
||||
const instructName = profile.instruct;
|
||||
|
||||
switch (selectedApiMap.selected) {
|
||||
case 'openai': {
|
||||
if (!selectedApiMap.source) {
|
||||
throw new Error(`API type ${selectedApiMap.selected} does not support chat completions`);
|
||||
}
|
||||
return prompt;
|
||||
}
|
||||
case 'textgenerationwebui': {
|
||||
if (!selectedApiMap.type) {
|
||||
throw new Error(`API type ${selectedApiMap.selected} does not support text completions`);
|
||||
}
|
||||
return context.TextCompletionService.constructPrompt(prompt, instructName, instructSettings);
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Unknown API type ${selectedApiMap.selected}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Respects allowed types.
|
||||
* @returns {import('./connection-manager/index.js').ConnectionProfile[]}
|
||||
@@ -467,6 +499,18 @@ export class ConnectionManagerRequestService {
|
||||
return profiles.filter((p) => this.isProfileSupported(p));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return profile data given the profile ID
|
||||
* @param {string} profileId
|
||||
* @returns {import('./connection-manager/index.js').ConnectionProfile?} [profile]
|
||||
* @throws {Error}
|
||||
*/
|
||||
static getProfile(profileId) {
|
||||
const profile = SillyTavern.getContext().extensionSettings.connectionManager.profiles.find((p) => p.id === profileId);
|
||||
if (!profile) throw new Error(`Profile not found (ID: ${profileId})`);
|
||||
return profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('./connection-manager/index.js').ConnectionProfile?} [profile]
|
||||
* @returns {boolean}
|
||||
|
||||
Reference in New Issue
Block a user