Skip to content

Commit

Permalink
added automatic recovery if bad config is loaded, will restore to kno…
Browse files Browse the repository at this point in the history
…wn good config
  • Loading branch information
LostRuins committed Feb 15, 2025
1 parent 302fedc commit f48bd3f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
14 changes: 12 additions & 2 deletions klite.embd
Original file line number Diff line number Diff line change
Expand Up @@ -9167,6 +9167,7 @@ Current version indicated by LITEVER below.
document.getElementById("featherlessdesc").classList.add("hidden");
document.getElementById("grokdesc").classList.add("hidden");
document.getElementById("oaidesc").classList.add("hidden");
document.getElementById("openrouterproviderbox").classList.add("hidden");
if(epchoice==2)
{
document.getElementById("oaidesc").classList.remove("hidden");
Expand Down Expand Up @@ -9209,6 +9210,7 @@ Current version indicated by LITEVER below.
{
document.getElementById("openrouterdesc").classList.remove("hidden");
document.getElementById("custom_openrouter_model").classList.remove("hidden");
document.getElementById("openrouterproviderbox").classList.remove("hidden");
document.getElementById("custom_oai_endpoint").value = default_openrouter_base;
document.getElementById("custom_oai_endpoint").classList.add("hidden");
document.getElementById("custom_oai_key").value = localsettings.saved_openrouter_key;
Expand Down Expand Up @@ -14183,6 +14185,12 @@ Current version indicated by LITEVER below.
if(targetep.toLowerCase().includes("openrouter.ai"))
{
oaiheaders["HTTP-Referer"] = "https://lite.koboldai.net";
if (document.getElementById("openrouterproviders").value.trim() != "") {
oai_payload.provider = {
"order": [document.getElementById("openrouterproviders").value.trim()],
"allow_fallbacks": false, //always explicitly selected
};
}
}

if(is_browser_supports_sse() && document.getElementById("oaistreaming").checked)
Expand Down Expand Up @@ -20357,8 +20365,10 @@ Current version indicated by LITEVER below.
<textarea class="form-control" rows="3" style="resize: vertical; line-height:1.1; padding:4px; display:inline; width: 100%;" type="text" id="jailbreakprompttext2" placeholder="(Enter Assistant Postfix)"
value="" onload="togglejailbreak2();"></textarea>
</div>


</span>
<span id="openrouterproviderbox" class="hidden"><br>Preferred Provider: <input style="height: 25px; font-size:12px;padding:4px;display:inline;width:calc(100% - 140px)" class="form-control" type="text" id="openrouterproviders" placeholder="(Automatic)" value="">
<div style="display:inline;width:210px;">
</div>
</span>

</div>
Expand Down
54 changes: 40 additions & 14 deletions koboldcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# editing tools, save formats, memory, world info, author's note, characters,
# scenarios and everything Kobold and KoboldAI Lite have to offer.

import copy
import ctypes
import multiprocessing
import os
Expand Down Expand Up @@ -47,7 +48,7 @@
dry_seq_break_max = 128

# global vars
KcppVersion = "1.83.1"
KcppVersion = "1.84"
showdebug = True
kcpp_instance = None #global running instance
global_memory = {"tunnel_url": "", "restart_target":"", "input_to_exit":False, "load_complete":False}
Expand Down Expand Up @@ -4796,16 +4797,19 @@ def unload_libs():

def reload_new_config(filename): #for changing config after launch
with open(filename, 'r', encoding='utf-8', errors='ignore') as f:
config = json.load(f)
args.istemplate = False
for key, value in config.items(): #do not overwrite certain values
if key not in ["remotetunnel","showgui","port","host","port_param","admin","adminpassword","admindir","ssl","nocertify","benchmark","prompt","config"]:
setattr(args, key, value)
setattr(args,"showgui",False)
setattr(args,"benchmark",False)
setattr(args,"prompt","")
setattr(args,"config",None)
setattr(args,"launch",None)
try:
config = json.load(f)
args.istemplate = False
for key, value in config.items(): #do not overwrite certain values
if key not in ["remotetunnel","showgui","port","host","port_param","admin","adminpassword","admindir","ssl","nocertify","benchmark","prompt","config"]:
setattr(args, key, value)
setattr(args,"showgui",False)
setattr(args,"benchmark",False)
setattr(args,"prompt","")
setattr(args,"config",None)
setattr(args,"launch",None)
except Exception as e:
print(f"Reload New Config Failed: {e}")

def load_config_cli(filename):
print("Loading .kcpps configuration file...")
Expand Down Expand Up @@ -5023,15 +5027,36 @@ def main(launch_args):
setuptunnel(global_memory, True if args.sdmodel else False)

# invoke the main koboldcpp process
original_args = copy.deepcopy(args)

kcpp_instance = multiprocessing.Process(target=kcpp_main_process,kwargs={"launch_args": args, "g_memory": global_memory, "gui_launcher": using_gui_launcher})
kcpp_instance.daemon = True
kcpp_instance.start()

fault_recovery_mode = False #if a config reload fails, recover back to old settings

while True: # keep the manager alive
try:
restart_target = ""
if not kcpp_instance or not kcpp_instance.is_alive():
break
if fault_recovery_mode:
#attempt to recover
print("Attempting to recover to safe mode, launching known-good config...")
fault_recovery_mode = False
args = copy.deepcopy(original_args) #restore known good original launcher args
if kcpp_instance:
kcpp_instance.terminate()
kcpp_instance.join(timeout=10) # Ensure process is stopped
kcpp_instance = None
kcpp_instance = multiprocessing.Process(target=kcpp_main_process,kwargs={"launch_args": args, "g_memory": global_memory, "gui_launcher": False})
kcpp_instance.daemon = True
kcpp_instance.start()
global_memory["restart_target"] = ""
time.sleep(3)
else:
break # kill the program
if fault_recovery_mode and global_memory["load_complete"]:
fault_recovery_mode = False
restart_target = global_memory["restart_target"]
if restart_target!="":
print(f"Reloading new config: {restart_target}")
Expand All @@ -5047,12 +5072,13 @@ def main(launch_args):
kcpp_instance.join(timeout=10) # Ensure process is stopped
kcpp_instance = None
print("Restarting KoboldCpp...")
fault_recovery_mode = True
reload_new_config(targetfilepath)
kcpp_instance = multiprocessing.Process(target=kcpp_main_process,kwargs={"launch_args": args, "g_memory": global_memory, "gui_launcher": using_gui_launcher})
kcpp_instance = multiprocessing.Process(target=kcpp_main_process,kwargs={"launch_args": args, "g_memory": global_memory, "gui_launcher": False})
kcpp_instance.daemon = True
kcpp_instance.start()
global_memory["restart_target"] = ""
time.sleep(1)
time.sleep(3)
else:
time.sleep(0.2)
except (KeyboardInterrupt,SystemExit):
Expand Down

0 comments on commit f48bd3f

Please # to comment.