Horde should actually work now

This commit is contained in:
SillyLossy
2023-04-02 14:22:18 +03:00
parent a71c39ec7f
commit 4e3c9db5ae
4 changed files with 91 additions and 16 deletions

View File

@@ -88,7 +88,8 @@
<form action="javascript:void(null);" method="post" enctype="multipart/form-data">
<label for="use_horde" class="checkbox_label">
<input id="use_horde" type="checkbox" />
Use Horde. <a target="_blank" href="https://stablehorde.net/">Learn more</a>
Use Horde.
<a target="_blank" href="https://horde.koboldai.net/">Learn more</a>
</label>
<div id="kobold_api_block">
<h4>API url</h4>
@@ -104,10 +105,17 @@
Adjust generation to worker capabilities
</label>
<h4>API key</h4>
<h5>Get it here: <a target="_blank" href="https://stablehorde.net/register">Register</a></h5>
<h5>Get it here: <a target="_blank" href="https://horde.koboldai.net/register">Register</a></h5>
<input id="horde_api_key" name="horde_api_key" class="text_pole" maxlength="500" value="0000000000" autocomplete="off">
<h4>Model</h4>
<select id="horde_model"></select>
<h4 class="horde_model_title">
Model
<div id="horde_refresh" title="Refresh models" class="right_menu_button">
<img class="svg_icon" src="img/repeat-solid.svg" />
</div>
</h4>
<select id="horde_model">
<option>-- Not connected to Horde --</option>
</select>
</div>
<div id="online_status2">
<div id="online_status_indicator2"></div>

View File

@@ -115,6 +115,7 @@ export {
showSwipeButtons,
hideSwipeButtons,
changeMainAPI,
setGenerationProgress,
chat,
this_chid,
settings,
@@ -1085,6 +1086,7 @@ function appendToStoryString(value, prefix) {
async function Generate(type, automatic_trigger, force_name2) {
console.log('Generate entered');
setGenerationProgress(0);
tokens_already_generated = 0;
message_already_generated = name2 + ': ';
@@ -1708,9 +1710,13 @@ async function Generate(type, automatic_trigger, force_name2) {
if (!data.error) {
//const getData = await response.json();
var getMessage = "";
if (main_api == 'kobold') {
if (main_api == 'kobold' && !horde_settings.use_horde) {
getMessage = data.results[0].text;
} else if (main_api == 'textgenerationwebui') {
}
else if (main_api == 'kobold' && horde_settings.use_horde) {
getMessage = data;
}
else if (main_api == 'textgenerationwebui') {
getMessage = data.data[0];
if (getMessage == null || data.error) {
activateSendButtons();
@@ -1718,7 +1724,8 @@ async function Generate(type, automatic_trigger, force_name2) {
return;
}
getMessage = getMessage.substring(finalPromt.length);
} else if (main_api == 'novel') {
}
else if (main_api == 'novel') {
getMessage = data.output;
}
if (main_api == 'openai') {
@@ -1795,6 +1802,7 @@ async function Generate(type, automatic_trigger, force_name2) {
activateSendButtons();
showSwipeButtons();
setGenerationProgress(0);
$('.mes_edit:last').show();
};
@@ -1802,6 +1810,7 @@ async function Generate(type, automatic_trigger, force_name2) {
$("#send_textarea").removeAttr('disabled');
is_send_press = false;
activateSendButtons();
setGenerationProgress(0);
console.log(exception);
console.log(jqXHR);
};
@@ -2966,6 +2975,18 @@ function setGenerationFunction(func) {
extension_generation_function = func;
}
function setGenerationProgress(progress) {
if (!progress) {
$('#send_textarea').css({'background': '', 'transition': ''});
}
else {
$('#send_textarea').css({
'background': `linear-gradient(90deg, #008000d6 ${progress}%, transparent ${progress}%)`,
'transition': '0.25s ease-in-out'
});
}
}
window["TavernAI"].getContext = function () {
return {
chat: chat,

View File

@@ -1,4 +1,4 @@
import { saveSettingsDebounced, changeMainAPI, callPopup } from "../script.js";
import { saveSettingsDebounced, changeMainAPI, callPopup, setGenerationProgress } from "../script.js";
import { delay } from "./utils.js";
export {
@@ -19,7 +19,7 @@ let horde_settings = {
};
const MAX_RETRIES = 100;
const CHECK_INTERVAL = 1000;
const CHECK_INTERVAL = 3000;
async function getWorkers() {
const response = await fetch('https://horde.koboldai.net/api/v2/workers?type=text');
@@ -27,12 +27,23 @@ async function getWorkers() {
return data;
}
function validateHordeModel() {
let selectedModel = models.find(m => m.name == horde_settings.model);
if (!selectedModel) {
callPopup('No Horde model selected or the selected model is no longer available. Please choose another model');
throw new Error('No Horde model available');
}
return selectedModel;
}
async function adjustHordeGenerationParams(max_context_length, max_length) {
const workers = await getWorkers();
let maxContextLength = max_context_length;
let maxLength = max_length;
let availableWorkers = [];
let selectedModel = models.find(m => m.name == horde_settings.model);
let selectedModel = validateHordeModel();
if (!selectedModel) {
return { maxContextLength, maxLength };
@@ -54,13 +65,21 @@ async function adjustHordeGenerationParams(max_context_length, max_length) {
}
async function generateHorde(prompt, params) {
validateHordeModel();
delete params.prompt;
// No idea what these do
params["n"] = 1;
params["frmtadsnsp"] = false;
params["frmtrmblln"] = false;
params["frmtrmspch"] = false;
params["frmttriminc"] = false;
const payload = {
"prompt": prompt,
"params": params,
"trusted_workers": false,
"slow_workers": false,
//"trusted_workers": false,
//"slow_workers": false,
"models": [horde_settings.model],
};
@@ -81,11 +100,10 @@ async function generateHorde(prompt, params) {
const responseJson = await response.json();
const task_id = responseJson.id;
let queue_position_first = null;
console.log(`Horde task id = ${task_id}`);
for (let retryNumber = 0; retryNumber < MAX_RETRIES; retryNumber++) {
await delay(CHECK_INTERVAL);
const statusCheckResponse = await fetch(`https://horde.koboldai.net/api/v2/generate/text/status/${task_id}`, {
headers: {
"Content-Type": "application/json",
@@ -96,12 +114,27 @@ async function generateHorde(prompt, params) {
const statusCheckJson = await statusCheckResponse.json();
console.log(statusCheckJson);
if (statusCheckJson.done) {
const generatedText = statusCheckJson.generations[0];
if (statusCheckJson.done && Array.isArray(statusCheckJson.generations) && statusCheckJson.generations.length) {
setGenerationProgress(100);
const generatedText = statusCheckJson.generations[0].text;
console.log(generatedText);
return generatedText;
}
else if (!queue_position_first) {
queue_position_first = statusCheckJson.queue_position;
setGenerationProgress(0);
}
else if (statusCheckJson.queue_position >= 0) {
let queue_position = statusCheckJson.queue_position;
const progress = Math.round(100 - (queue_position / queue_position_first * 100));
setGenerationProgress(progress);
}
await delay(CHECK_INTERVAL);
}
callPopup('Horde request timed out. Try again', 'text');
throw new Error('Horde timeout');
}
async function checkHordeStatus() {
@@ -169,4 +202,6 @@ $(document).ready(function () {
horde_settings.auto_adjust = !!$(this).prop("checked");
saveSettingsDebounced();
});
$("#horde_refresh").on("click", getHordeModels);
})

View File

@@ -1272,6 +1272,17 @@ input[type=search]:focus::-webkit-search-cancel-button {
display: inline-block;
}
.horde_model_title {
display: flex;
flex-direction: row;
align-items: center;
column-gap: 20px;
}
.horde_model_title .right_menu_button img.svg_icon {
height: 20px;
}
#softprompt {
margin-bottom: 10px;
}