Add support of stop strings to OpenAI / Claude

This commit is contained in:
Cohee
2023-08-26 00:12:11 +03:00
parent c0a6e9be8d
commit 42e6da4a36
3 changed files with 30 additions and 4 deletions

View File

@@ -34,6 +34,7 @@ import {
} from "./PromptManager.js";
import {
getCustomStoppingStrings,
persona_description_positions,
power_user,
} from "./power-user.js";
@@ -120,6 +121,7 @@ const j2_max_topk = 10.0;
const j2_max_freq = 5.0;
const j2_max_pres = 5.0;
const openrouter_website_model = 'OR_Website';
const openai_max_stop_strings = 4;
let biasCache = undefined;
let model_list = [];
@@ -1138,6 +1140,7 @@ async function sendOpenAIRequest(type, openai_msgs_tosend, signal) {
"max_tokens": oai_settings.openai_max_tokens,
"stream": stream,
"logit_bias": logit_bias,
"stop": getCustomStoppingStrings(openai_max_stop_strings),
};
// Proxy is only supported for Claude and OpenAI
@@ -1151,6 +1154,7 @@ async function sendOpenAIRequest(type, openai_msgs_tosend, signal) {
generate_data['use_claude'] = true;
generate_data['top_k'] = Number(oai_settings.top_k_openai);
generate_data['exclude_assistant'] = oai_settings.exclude_assistant;
generate_data['stop'] = getCustomStoppingStrings(); // Claude shouldn't have limits on stop strings.
// Don't add a prefill on quiet gens (summarization)
if (!isQuiet && !oai_settings.exclude_assistant) {
generate_data['assistant_prefill'] = substituteParams(oai_settings.assistant_prefill);

View File

@@ -1537,8 +1537,19 @@ function setAvgBG() {
}
export function getCustomStoppingStrings() {
/**
* Gets the custom stopping strings from the power user settings.
* @param {number | undefined} limit Number of strings to return. If undefined, returns all strings.
* @returns {string[]} An array of custom stopping strings
*/
export function getCustomStoppingStrings(limit = undefined) {
try {
// If there's no custom stopping strings, return an empty array
if (!power_user.custom_stopping_strings) {
return [];
}
// Parse the JSON string
const strings = JSON.parse(power_user.custom_stopping_strings);
@@ -1547,8 +1558,8 @@ export function getCustomStoppingStrings() {
return [];
}
// Make sure all the elements are strings
return strings.filter((s) => typeof s === 'string');
// Make sure all the elements are strings. Apply the limit.
return strings.filter((s) => typeof s === 'string').slice(0, limit);
} catch (error) {
// If there's an error, return an empty array
console.warn('Error parsing custom stopping strings:', error);