#2051 #2031 Selectable prompt converter for custom OAI type

This commit is contained in:
Cohee 2024-04-10 21:12:57 +03:00
parent a9a3d91e4f
commit 53baa7aae7
3 changed files with 54 additions and 0 deletions

View File

@ -2733,6 +2733,11 @@
<div class="flex-container">
<select id="model_custom_select" class="text_pole"></select>
</div>
<h4 data-i18n="Prompt Post-Processing">Prompt Post-Processing</h4>
<select id="custom_prompt_post_processing" class="text_pole" title="Applies additional processing to the prompt before sending it to the API.">
<option value="">None</option>
<option value="claude">Claude</option>
</select>
</form>
<div class="flex-container flex">
<div id="api_button_openai" class="api_button menu_button menu_button_icon" type="submit" data-i18n="Connect">Connect</div>

View File

@ -186,6 +186,11 @@ const continue_postfix_types = {
DOUBLE_NEWLINE: '\n\n',
};
const custom_prompt_post_processing_types = {
NONE: '',
CLAUDE: 'claude',
};
const prefixMap = selected_group ? {
assistant: '',
user: '',
@ -262,6 +267,7 @@ const default_settings = {
continue_prefill: false,
names_behavior: character_names_behavior.NONE,
continue_postfix: continue_postfix_types.SPACE,
custom_prompt_post_processing: custom_prompt_post_processing_types.NONE,
seed: -1,
n: 1,
};
@ -331,6 +337,7 @@ const oai_settings = {
continue_prefill: false,
names_behavior: character_names_behavior.NONE,
continue_postfix: continue_postfix_types.SPACE,
custom_prompt_post_processing: custom_prompt_post_processing_types.NONE,
seed: -1,
n: 1,
};
@ -1743,6 +1750,7 @@ async function sendOpenAIRequest(type, messages, signal) {
generate_data['custom_include_body'] = oai_settings.custom_include_body;
generate_data['custom_exclude_body'] = oai_settings.custom_exclude_body;
generate_data['custom_include_headers'] = oai_settings.custom_include_headers;
generate_data['custom_prompt_post_processing'] = oai_settings.custom_prompt_post_processing;
}
if (isCohere) {
@ -2625,6 +2633,7 @@ function loadOpenAISettings(data, settings) {
oai_settings.custom_include_body = settings.custom_include_body ?? default_settings.custom_include_body;
oai_settings.custom_exclude_body = settings.custom_exclude_body ?? default_settings.custom_exclude_body;
oai_settings.custom_include_headers = settings.custom_include_headers ?? default_settings.custom_include_headers;
oai_settings.custom_prompt_post_processing = settings.custom_prompt_post_processing ?? default_settings.custom_prompt_post_processing;
oai_settings.google_model = settings.google_model ?? default_settings.google_model;
oai_settings.chat_completion_source = settings.chat_completion_source ?? default_settings.chat_completion_source;
oai_settings.api_url_scale = settings.api_url_scale ?? default_settings.api_url_scale;
@ -2770,6 +2779,8 @@ function loadOpenAISettings(data, settings) {
$('#chat_completion_source').val(oai_settings.chat_completion_source).trigger('change');
$('#oai_max_context_unlocked').prop('checked', oai_settings.max_context_unlocked);
$('#custom_prompt_post_processing').val(oai_settings.custom_prompt_post_processing);
$(`#custom_prompt_post_processing option[value="${oai_settings.custom_prompt_post_processing}"]`).attr('selected', true);
}
function setNamesBehaviorControls() {
@ -2924,6 +2935,7 @@ async function saveOpenAIPreset(name, settings, triggerUi = true) {
custom_include_body: settings.custom_include_body,
custom_exclude_body: settings.custom_exclude_body,
custom_include_headers: settings.custom_include_headers,
custom_prompt_post_processing: settings.custom_prompt_post_processing,
google_model: settings.google_model,
temperature: settings.temp_openai,
frequency_penalty: settings.freq_pen_openai,
@ -3313,6 +3325,7 @@ function onSettingsPresetChange() {
custom_include_body: ['#custom_include_body', 'custom_include_body', false],
custom_exclude_body: ['#custom_exclude_body', 'custom_exclude_body', false],
custom_include_headers: ['#custom_include_headers', 'custom_include_headers', false],
custom_prompt_post_processing: ['#custom_prompt_post_processing', 'custom_prompt_post_processing', false],
google_model: ['#model_google_select', 'google_model', false],
openai_max_context: ['#openai_max_context', 'openai_max_context', false],
openai_max_tokens: ['#openai_max_tokens', 'openai_max_tokens', false],
@ -4494,6 +4507,11 @@ $(document).ready(async function () {
saveSettingsDebounced();
});
$('#custom_prompt_post_processing').on('change', function () {
oai_settings.custom_prompt_post_processing = String($(this).val());
saveSettingsDebounced();
});
$('#names_behavior').on('input', function () {
oai_settings.names_behavior = Number($(this).val());
setNamesBehaviorControls();

View File

@ -15,6 +15,23 @@ const API_CLAUDE = 'https://api.anthropic.com/v1';
const API_MISTRAL = 'https://api.mistral.ai/v1';
const API_COHERE = 'https://api.cohere.ai/v1';
/**
* Applies a post-processing step to the generated messages.
* @param {object[]} messages Messages to post-process
* @param {string} type Prompt conversion type
* @param {string} charName Character name
* @param {string} userName User name
* @returns
*/
function postProcessPrompt(messages, type, charName, userName) {
switch (type) {
case 'claude':
return convertClaudeMessages(messages, '', false, '', charName, userName).messages;
default:
return messages;
}
}
/**
* Ollama strikes back. Special boy #2's steaming routine.
* Wrap this abomination into proper SSE stream, again.
@ -522,6 +539,11 @@ async function sendMistralAIRequest(request, response) {
}
}
/**
* Sends a request to Cohere API.
* @param {express.Request} request Express request
* @param {express.Response} response Express response
*/
async function sendCohereRequest(request, response) {
const apiKey = readSecret(SECRET_KEYS.COHERE);
const controller = new AbortController();
@ -848,6 +870,15 @@ router.post('/generate', jsonParser, function (request, response) {
mergeObjectWithYaml(bodyParams, request.body.custom_include_body);
mergeObjectWithYaml(headers, request.body.custom_include_headers);
if (request.body.custom_prompt_post_processing) {
console.log('Applying custom prompt post-processing of type', request.body.custom_prompt_post_processing);
request.body.messages = postProcessPrompt(
request.body.messages,
request.body.custom_prompt_post_processing,
request.body.char_name,
request.body.user_name);
}
} else {
console.log('This chat completion source is not supported yet.');
return response.status(400).send({ error: true });