mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
OpenAI fixes
This commit is contained in:
@ -104,6 +104,7 @@ export {
|
|||||||
nai_settings,
|
nai_settings,
|
||||||
token,
|
token,
|
||||||
name1,
|
name1,
|
||||||
|
name2,
|
||||||
is_send_press,
|
is_send_press,
|
||||||
api_server_textgenerationwebui,
|
api_server_textgenerationwebui,
|
||||||
count_view_mes,
|
count_view_mes,
|
||||||
@ -1457,7 +1458,7 @@ async function Generate(type, automatic_trigger, force_name2) {//encode("dsfs").
|
|||||||
|
|
||||||
|
|
||||||
if (main_api == 'openai') {
|
if (main_api == 'openai') {
|
||||||
let prompt = prepareOpenAIMessages(name2, storyString, worldInfoBefore, worldInfoAfter, extension_prompt);
|
let prompt = prepareOpenAIMessages(name2, storyString, worldInfoBefore, worldInfoAfter, extension_prompt, promptBias);
|
||||||
sendOpenAIRequest(prompt).then(onSuccess).catch(onError);
|
sendOpenAIRequest(prompt).then(onSuccess).catch(onError);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -3277,6 +3278,10 @@ $(document).ready(function () {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: function (jqXHR, exception) {
|
error: function (jqXHR, exception) {
|
||||||
|
if (jqXHR.status == 403) {
|
||||||
|
callPopup(`Character can't be imported due to invalid name. Please choose other name`, 'text');
|
||||||
|
}
|
||||||
|
|
||||||
//alert('ERROR: '+xhr.status+ ' Status Text: '+xhr.statusText+' '+xhr.responseText);
|
//alert('ERROR: '+xhr.status+ ' Status Text: '+xhr.statusText+' '+xhr.responseText);
|
||||||
$("#create_button").removeAttr("disabled");
|
$("#create_button").removeAttr("disabled");
|
||||||
},
|
},
|
||||||
@ -3941,6 +3946,9 @@ $(document).ready(function () {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: function (jqXHR, exception) {
|
error: function (jqXHR, exception) {
|
||||||
|
if (jqXHR.status == 403) {
|
||||||
|
callPopup(`Character can't be imported due to invalid name. Please choose other name`, 'text');
|
||||||
|
}
|
||||||
$("#create_button").removeAttr("disabled");
|
$("#create_button").removeAttr("disabled");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -15,6 +15,7 @@ import {
|
|||||||
setOnlineStatus,
|
setOnlineStatus,
|
||||||
token,
|
token,
|
||||||
name1,
|
name1,
|
||||||
|
name2,
|
||||||
} from "../script.js";
|
} from "../script.js";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -184,7 +185,7 @@ function formatWorldInfo(value) {
|
|||||||
return `[Details of the fictional world the RP set in:\n${value}\n]`;
|
return `[Details of the fictional world the RP set in:\n${value}\n]`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepareOpenAIMessages(name2, storyString, worldInfoBefore, worldInfoAfter, extensionPrompt) {
|
function prepareOpenAIMessages(name2, storyString, worldInfoBefore, worldInfoAfter, extensionPrompt, bias) {
|
||||||
let this_max_context = oai_settings.openai_max_context;
|
let this_max_context = oai_settings.openai_max_context;
|
||||||
let nsfw_toggle_prompt = "";
|
let nsfw_toggle_prompt = "";
|
||||||
let enhance_definitions_prompt = "";
|
let enhance_definitions_prompt = "";
|
||||||
@ -220,6 +221,11 @@ function prepareOpenAIMessages(name2, storyString, worldInfoBefore, worldInfoAft
|
|||||||
let start_chat_count = countTokens([new_chat_msg]);
|
let start_chat_count = countTokens([new_chat_msg]);
|
||||||
let total_count = countTokens([prompt_msg], true) + start_chat_count;
|
let total_count = countTokens([prompt_msg], true) + start_chat_count;
|
||||||
|
|
||||||
|
if (bias) {
|
||||||
|
let bias_msg = { "role": "system", "content": bias };
|
||||||
|
openai_msgs.push(bias_msg);
|
||||||
|
}
|
||||||
|
|
||||||
// The user wants to always have all example messages in the context
|
// The user wants to always have all example messages in the context
|
||||||
if (pin_examples) {
|
if (pin_examples) {
|
||||||
// first we send *all* example messages
|
// first we send *all* example messages
|
||||||
@ -314,6 +320,7 @@ async function sendOpenAIRequest(openai_msgs_tosend) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const generate_url = '/generate_openai';
|
const generate_url = '/generate_openai';
|
||||||
|
// TODO: fix streaming
|
||||||
const streaming = oai_settings.stream_openai;
|
const streaming = oai_settings.stream_openai;
|
||||||
const last_view_mes = count_view_mes;
|
const last_view_mes = count_view_mes;
|
||||||
|
|
||||||
@ -392,7 +399,7 @@ function countTokens(messages, full = false) {
|
|||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
async: false,
|
async: false,
|
||||||
type: 'POST', //
|
type: 'POST', //
|
||||||
url: '/tokenize_openai', //
|
url: `/tokenize_openai?model=${oai_settings.openai_model}`,
|
||||||
data: JSON.stringify(messages),
|
data: JSON.stringify(messages),
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
contentType: "application/json",
|
contentType: "application/json",
|
||||||
|
33
server.js
33
server.js
@ -557,7 +557,7 @@ app.post("/createcharacter", urlencodedParser, function (request, response) {
|
|||||||
|
|
||||||
if (request.body.ch_name !== sanitize(request.body.ch_name)) {
|
if (request.body.ch_name !== sanitize(request.body.ch_name)) {
|
||||||
console.error('Malicious character name prevented');
|
console.error('Malicious character name prevented');
|
||||||
return response.send(400);
|
return response.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('/createcharacter -- looking for -- ' + (charactersPath + request.body.ch_name + '.png'));
|
console.log('/createcharacter -- looking for -- ' + (charactersPath + request.body.ch_name + '.png'));
|
||||||
@ -636,7 +636,7 @@ app.post("/deletecharacter", urlencodedParser, function (request, response) {
|
|||||||
|
|
||||||
if (request.body.avatar_url !== sanitize(request.body.avatar_url)) {
|
if (request.body.avatar_url !== sanitize(request.body.avatar_url)) {
|
||||||
console.error('Malicious filename prevented');
|
console.error('Malicious filename prevented');
|
||||||
return response.sendStatus(400);
|
return response.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
const avatarPath = charactersPath + request.body.avatar_url;
|
const avatarPath = charactersPath + request.body.avatar_url;
|
||||||
@ -649,7 +649,7 @@ app.post("/deletecharacter", urlencodedParser, function (request, response) {
|
|||||||
|
|
||||||
if (dir_name !== sanitize(dir_name)) {
|
if (dir_name !== sanitize(dir_name)) {
|
||||||
console.error('Malicious dirname prevented');
|
console.error('Malicious dirname prevented');
|
||||||
return response.sendStatus(400);
|
return response.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
rimraf(path.join(chatsPath, sanitize(dir_name)), (err) => {
|
rimraf(path.join(chatsPath, sanitize(dir_name)), (err) => {
|
||||||
@ -797,7 +797,7 @@ app.post("/delbackground", jsonParser, function (request, response) {
|
|||||||
|
|
||||||
if (request.body.bg !== sanitize(request.body.bg)) {
|
if (request.body.bg !== sanitize(request.body.bg)) {
|
||||||
console.error('Malicious bg name prevented');
|
console.error('Malicious bg name prevented');
|
||||||
return response.sendStatus(400);
|
return response.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileName = path.join('public/backgrounds/', sanitize(request.body.bg));
|
const fileName = path.join('public/backgrounds/', sanitize(request.body.bg));
|
||||||
@ -1239,7 +1239,7 @@ app.post("/importcharacter", urlencodedParser, async function (request, response
|
|||||||
if (jsonData.name !== undefined) {
|
if (jsonData.name !== undefined) {
|
||||||
if (jsonData.name !== sanitize(jsonData.name)) {
|
if (jsonData.name !== sanitize(jsonData.name)) {
|
||||||
console.error('Malicious character name prevented');
|
console.error('Malicious character name prevented');
|
||||||
return response.send(400);
|
return response.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
png_name = getPngName(jsonData.name);
|
png_name = getPngName(jsonData.name);
|
||||||
@ -1249,7 +1249,7 @@ app.post("/importcharacter", urlencodedParser, async function (request, response
|
|||||||
} else if (jsonData.char_name !== undefined) {//json Pygmalion notepad
|
} else if (jsonData.char_name !== undefined) {//json Pygmalion notepad
|
||||||
if (jsonData.char_name !== sanitize(jsonData.char_name)) {
|
if (jsonData.char_name !== sanitize(jsonData.char_name)) {
|
||||||
console.error('Malicious character name prevented');
|
console.error('Malicious character name prevented');
|
||||||
return response.send(400);
|
return response.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
png_name = getPngName(jsonData.char_name);
|
png_name = getPngName(jsonData.char_name);
|
||||||
@ -1269,7 +1269,7 @@ app.post("/importcharacter", urlencodedParser, async function (request, response
|
|||||||
|
|
||||||
if (jsonData.name !== sanitize(jsonData.name)) {
|
if (jsonData.name !== sanitize(jsonData.name)) {
|
||||||
console.error('Malicious character name prevented');
|
console.error('Malicious character name prevented');
|
||||||
return response.send(400);
|
return response.sendStatus(403);
|
||||||
}
|
}
|
||||||
|
|
||||||
png_name = getPngName(jsonData.name);
|
png_name = getPngName(jsonData.name);
|
||||||
@ -1687,16 +1687,31 @@ app.post("/generate_openai", jsonParser, function(request, response_generate_ope
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const turbo_encoder = tiktoken.get_encoding("cl100k_base");
|
const tokenizers = {
|
||||||
|
'gpt-3.5-turbo-0301': tiktoken.encoding_for_model('gpt-3.5-turbo-0301'),
|
||||||
|
};
|
||||||
|
|
||||||
|
function getTokenizer(model) {
|
||||||
|
let tokenizer = tokenizers[model];
|
||||||
|
|
||||||
|
if (!tokenizer) {
|
||||||
|
tokenizer = tiktoken.encoding_for_model(model);
|
||||||
|
tokenizers[tokenizer];
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokenizer;
|
||||||
|
}
|
||||||
|
|
||||||
app.post("/tokenize_openai", jsonParser, function(request, response_tokenize_openai = response){
|
app.post("/tokenize_openai", jsonParser, function(request, response_tokenize_openai = response){
|
||||||
if(!request.body) return response_tokenize_openai.sendStatus(400);
|
if(!request.body) return response_tokenize_openai.sendStatus(400);
|
||||||
|
|
||||||
|
const tokenizer = getTokenizer(request.query.model);
|
||||||
|
|
||||||
let num_tokens = 0;
|
let num_tokens = 0;
|
||||||
for (var msg of request.body) {
|
for (var msg of request.body) {
|
||||||
num_tokens += 4;
|
num_tokens += 4;
|
||||||
for (const [key, value] of Object.entries(msg)) {
|
for (const [key, value] of Object.entries(msg)) {
|
||||||
num_tokens += turbo_encoder.encode(value).length;
|
num_tokens += tokenizer.encode(value).length;
|
||||||
if (key == "name") {
|
if (key == "name") {
|
||||||
num_tokens += -1;
|
num_tokens += -1;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user