Skip to content

Commit 684aed4

Browse files
committed
switch to the admin api
1 parent 7a32fd2 commit 684aed4

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

koboldcpp.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,40 +4041,43 @@ def do_POST(self):
40414041
global current_model
40424042

40434043
model = genparams["model"]
4044-
if server_process is not None and current_model != model:
4045-
import psutil
4046-
parent = psutil.Process(server_process.pid)
4047-
processes = parent.children(recursive=True) + [parent]
4048-
for process in processes:
4049-
process.terminate()
4050-
for process in processes:
4051-
process.wait()
4052-
4053-
server_process = None
4044+
model_path = next((str(path) for path in get_models() if path.stem == model), None)
4045+
if model_path is None:
4046+
self.send_response(404)
4047+
self.end_headers(content_type='application/json')
4048+
self.wfile.write(json.dumps({"detail": {
4049+
"error": "Model Not Found",
4050+
"msg": f"Model file {model} not found.",
4051+
"type": "bad_input",
4052+
}}).encode())
4053+
return
40544054

40554055
if server_process is None:
4056-
current_model = model
4057-
4058-
model_path = next((str(path) for path in get_models() if path.stem == model), None)
4059-
if model_path is None:
4060-
self.send_response(404)
4061-
self.end_headers(content_type='application/json')
4062-
self.wfile.write(json.dumps({"detail": {
4063-
"error": "Model Not Found",
4064-
"msg": f"Model file {model} not found.",
4065-
"type": "bad_input",
4066-
}}).encode())
4067-
return
4068-
40694056
server_process = subprocess.Popen([sys.executable] + sys.argv + ["--port", str(args.port + 1), "--model", model_path], env={
40704057
"KOBOLDCPP_SERVER": "True"
40714058
})
4072-
4073-
# Poke the server until it's alive
4059+
elif current_model != model:
4060+
with urllib.request.urlopen(urllib.request.Request(f"http://localhost:{args.port + 1}/api/admin/reload_config", method="POST", data=json.dumps({"filename": model_path}).encode(), headers={
4061+
"Authorization": f"Bearer {args.adminpassword}"
4062+
})) as response:
4063+
if response.status != 200:
4064+
self.send_response(500)
4065+
self.end_headers(content_type='application/json')
4066+
self.wfile.write(json.dumps({"detail": {
4067+
"error": "Failed to switch model",
4068+
"msg": f"Failed to switch model to {model}.",
4069+
"type": "server_error",
4070+
}}).encode())
4071+
return
4072+
4073+
current_model = model
4074+
4075+
# Poke the server until it has the new model
40744076
while True:
40754077
try:
4076-
with urllib.request.urlopen(urllib.request.Request(f"http://localhost:{args.port + 1}", method="HEAD"), timeout=1000) as response:
4077-
if response.status == 200:
4078+
with urllib.request.urlopen(urllib.request.Request(f"http://localhost:{args.port + 1}/api/v1/model", method="GET"), timeout=1000) as response:
4079+
data = json.loads(response.read().decode())
4080+
if response.status == 200 and data.get("result") == f"koboldcpp/{model}":
40784081
break
40794082

40804083
time.sleep(1)

0 commit comments

Comments
 (0)