mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Fix window.ai streaming
This commit is contained in:
@ -3813,16 +3813,6 @@ async function getSettings(type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Load which API we are using
|
|
||||||
if (settings.main_api != undefined) {
|
|
||||||
main_api = settings.main_api;
|
|
||||||
$("#main_api option[value=" + main_api + "]").attr(
|
|
||||||
"selected",
|
|
||||||
"true"
|
|
||||||
);
|
|
||||||
changeMainAPI();
|
|
||||||
}
|
|
||||||
|
|
||||||
//Load KoboldAI settings
|
//Load KoboldAI settings
|
||||||
koboldai_setting_names = data.koboldai_setting_names;
|
koboldai_setting_names = data.koboldai_setting_names;
|
||||||
koboldai_settings = data.koboldai_settings;
|
koboldai_settings = data.koboldai_settings;
|
||||||
@ -3911,7 +3901,7 @@ async function getSettings(type) {
|
|||||||
// Load power user settings
|
// Load power user settings
|
||||||
loadPowerUserSettings(settings, data);
|
loadPowerUserSettings(settings, data);
|
||||||
|
|
||||||
// Load- character tags
|
// Load character tags
|
||||||
loadTagsSettings(settings);
|
loadTagsSettings(settings);
|
||||||
|
|
||||||
// Load context templates
|
// Load context templates
|
||||||
@ -3924,8 +3914,14 @@ async function getSettings(type) {
|
|||||||
$("#amount_gen").val(amount_gen);
|
$("#amount_gen").val(amount_gen);
|
||||||
$("#amount_gen_counter").text(`${amount_gen}`);
|
$("#amount_gen_counter").text(`${amount_gen}`);
|
||||||
|
|
||||||
//Enable GUI deference settings if GUI is selected for Kobold
|
//Load which API we are using
|
||||||
if (main_api === "kobold") {
|
if (settings.main_api != undefined) {
|
||||||
|
main_api = settings.main_api;
|
||||||
|
$("#main_api option[value=" + main_api + "]").attr(
|
||||||
|
"selected",
|
||||||
|
"true"
|
||||||
|
);
|
||||||
|
changeMainAPI();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Load User's Name and Avatar
|
//Load User's Name and Avatar
|
||||||
|
@ -557,8 +557,28 @@ async function sendOpenAIRequest(type, openai_msgs_tosend, signal) {
|
|||||||
return showWindowExtensionError();
|
return showWindowExtensionError();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function* windowStreamingFunction(res) {
|
let content = '';
|
||||||
yield (res?.message?.content || '');
|
let lastContent = '';
|
||||||
|
let finished = false;
|
||||||
|
|
||||||
|
async function* windowStreamingFunction() {
|
||||||
|
while (true) {
|
||||||
|
if (signal.aborted) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await delay(1);
|
||||||
|
|
||||||
|
if (lastContent !== content) {
|
||||||
|
yield content;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastContent = content;
|
||||||
|
|
||||||
|
if (finished) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const generatePromise = window.ai.generateText(
|
const generatePromise = window.ai.generateText(
|
||||||
@ -568,22 +588,34 @@ async function sendOpenAIRequest(type, openai_msgs_tosend, signal) {
|
|||||||
{
|
{
|
||||||
temperature: parseFloat(oai_settings.temp_openai),
|
temperature: parseFloat(oai_settings.temp_openai),
|
||||||
maxTokens: oai_settings.openai_max_tokens,
|
maxTokens: oai_settings.openai_max_tokens,
|
||||||
onStreamResult: windowStreamingFunction,
|
onStreamResult: (res, err) => {
|
||||||
|
if (err) {
|
||||||
|
handleWindowError(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
const thisContent = res?.message?.content;
|
||||||
|
|
||||||
|
if (res?.isPartial) {
|
||||||
|
content += thisContent;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
content = thisContent;
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (stream) {
|
|
||||||
return windowStreamingFunction;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [{ message }] = await generatePromise;
|
if (stream) {
|
||||||
windowStreamingFunction(message);
|
generatePromise.then(() => { finished = true; }).catch(handleWindowError);
|
||||||
return message?.content;
|
return windowStreamingFunction;
|
||||||
|
} else {
|
||||||
|
const result = await generatePromise;
|
||||||
|
content = result[0]?.message?.content;
|
||||||
|
return content;
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const text = parseWindowError(err);
|
handleWindowError(err);
|
||||||
toastr.error(text, 'Window.ai returned an error');
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -651,6 +683,12 @@ async function sendOpenAIRequest(type, openai_msgs_tosend, signal) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleWindowError(err) {
|
||||||
|
const text = parseWindowError(err);
|
||||||
|
toastr.error(text, 'Window.ai returned an error');
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
function parseWindowError(err) {
|
function parseWindowError(err) {
|
||||||
let text = 'Unknown error';
|
let text = 'Unknown error';
|
||||||
|
|
||||||
@ -671,7 +709,7 @@ function parseWindowError(err) {
|
|||||||
text = 'Malformed request';
|
text = 'Malformed request';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -812,6 +850,7 @@ function loadOpenAISettings(data, settings) {
|
|||||||
oai_settings.bias_preset_selected = settings.bias_preset_selected ?? default_settings.bias_preset_selected;
|
oai_settings.bias_preset_selected = settings.bias_preset_selected ?? default_settings.bias_preset_selected;
|
||||||
oai_settings.bias_presets = settings.bias_presets ?? default_settings.bias_presets;
|
oai_settings.bias_presets = settings.bias_presets ?? default_settings.bias_presets;
|
||||||
oai_settings.legacy_streaming = settings.legacy_streaming ?? default_settings.legacy_streaming;
|
oai_settings.legacy_streaming = settings.legacy_streaming ?? default_settings.legacy_streaming;
|
||||||
|
oai_settings.use_window_ai = settings.use_window_ai ?? default_settings.use_window_ai;
|
||||||
|
|
||||||
if (settings.nsfw_toggle !== undefined) oai_settings.nsfw_toggle = !!settings.nsfw_toggle;
|
if (settings.nsfw_toggle !== undefined) oai_settings.nsfw_toggle = !!settings.nsfw_toggle;
|
||||||
if (settings.keep_example_dialogue !== undefined) oai_settings.keep_example_dialogue = !!settings.keep_example_dialogue;
|
if (settings.keep_example_dialogue !== undefined) oai_settings.keep_example_dialogue = !!settings.keep_example_dialogue;
|
||||||
@ -891,7 +930,7 @@ async function getStatusOpen() {
|
|||||||
showWindowExtensionError();
|
showWindowExtensionError();
|
||||||
status = 'no_connection';
|
status = 'no_connection';
|
||||||
}
|
}
|
||||||
|
|
||||||
setOnlineStatus(status);
|
setOnlineStatus(status);
|
||||||
return resultCheckStatusOpen();
|
return resultCheckStatusOpen();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user