Initial model gateway baseline
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
*.bak
|
||||||
|
*.bak.*
|
||||||
|
*.log
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
.DS_Store
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
# Model Gateway
|
||||||
|
|
||||||
|
Gateway for routing OpenAI-compatible API requests to local llama-server instances.
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
Client / Hermes
|
||||||
|
|
|
||||||
|
v
|
||||||
|
model-gateway :8090
|
||||||
|
|
|
||||||
|
v
|
||||||
|
modelctl
|
||||||
|
|
|
||||||
|
v
|
||||||
|
llama-server
|
||||||
|
|
|
||||||
|
v
|
||||||
|
GPU
|
||||||
|
|
||||||
|
## Components
|
||||||
|
|
||||||
|
- gateway.py — OpenAI-compatible HTTP gateway
|
||||||
|
- modelctl — model lifecycle manager
|
||||||
|
- models.json — model configuration
|
||||||
|
- llama-server — llama.cpp inference backend
|
||||||
|
|
||||||
|
## Gateway
|
||||||
|
|
||||||
|
Default listen address: 0.0.0.0:8090
|
||||||
|
|
||||||
|
### Endpoints
|
||||||
|
|
||||||
|
GET /health
|
||||||
|
GET /v1/models
|
||||||
|
GET /props
|
||||||
|
GET /props?model=<model>
|
||||||
|
|
||||||
|
POST /v1/chat/completions
|
||||||
|
POST /v1/completions
|
||||||
|
|
||||||
|
## Model management
|
||||||
|
|
||||||
|
modelctl list
|
||||||
|
modelctl list --json
|
||||||
|
modelctl status
|
||||||
|
modelctl load <profile>
|
||||||
|
modelctl stop
|
||||||
|
|
||||||
|
## Diagnostics
|
||||||
|
|
||||||
|
modelctl status
|
||||||
|
nvidia-smi
|
||||||
|
ps aux | grep '[l]lama-server'
|
||||||
|
curl http://127.0.0.1:8090/health
|
||||||
|
curl http://127.0.0.1:8090/v1/models
|
||||||
|
curl http://127.0.0.1:8090/props
|
||||||
|
|
||||||
|
## Deployment
|
||||||
|
|
||||||
|
Machine-specific settings such as llama.cpp path, GGUF paths,
|
||||||
|
GPU parameters and ports should be kept in configuration.
|
||||||
|
|
||||||
|
## Current status
|
||||||
|
|
||||||
|
Baseline repository for the local model gateway.
|
||||||
|
|
||||||
|
Further work:
|
||||||
|
|
||||||
|
- reliable model switching
|
||||||
|
- clean model shutdown
|
||||||
|
- model logging
|
||||||
|
- installation/deployment script
|
||||||
|
- systemd integration
|
||||||
|
- complete model configuration documentation
|
||||||
|
- troubleshooting guide
|
||||||
Executable
+515
@@ -0,0 +1,515 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import threading
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
|
||||||
|
|
||||||
|
HOST = "0.0.0.0"
|
||||||
|
PORT = 8090
|
||||||
|
|
||||||
|
MODELCTL = "/usr/local/bin/modelctl"
|
||||||
|
|
||||||
|
switch_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
|
def get_models():
|
||||||
|
result = subprocess.run(
|
||||||
|
[MODELCTL, "list", "--json"],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
data = json.loads(result.stdout)
|
||||||
|
return data.get("models", {})
|
||||||
|
|
||||||
|
|
||||||
|
def model_url(port):
|
||||||
|
return f"http://127.0.0.1:{port}"
|
||||||
|
|
||||||
|
|
||||||
|
def get_loaded_model(port):
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(
|
||||||
|
f"{model_url(port)}/v1/models",
|
||||||
|
timeout=3,
|
||||||
|
) as response:
|
||||||
|
data = json.loads(response.read())
|
||||||
|
|
||||||
|
models = data.get("data", [])
|
||||||
|
|
||||||
|
if models:
|
||||||
|
return models[0].get("id")
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_model():
|
||||||
|
models = get_models()
|
||||||
|
|
||||||
|
for info in models.values():
|
||||||
|
loaded = get_loaded_model(info["port"])
|
||||||
|
|
||||||
|
if loaded:
|
||||||
|
return loaded
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def switch_model(model_name):
|
||||||
|
models = get_models()
|
||||||
|
|
||||||
|
if model_name not in models:
|
||||||
|
raise RuntimeError(f"Unknown model: {model_name}")
|
||||||
|
|
||||||
|
target = models[model_name]
|
||||||
|
|
||||||
|
with switch_lock:
|
||||||
|
current = get_current_model()
|
||||||
|
|
||||||
|
if current == model_name:
|
||||||
|
return target["port"]
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"[gateway] switching {current} -> {model_name}",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
result = subprocess.run(
|
||||||
|
[
|
||||||
|
MODELCTL,
|
||||||
|
"load",
|
||||||
|
target["profile"],
|
||||||
|
],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"[gateway] modelctl returncode={result.returncode}",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.stdout:
|
||||||
|
print(
|
||||||
|
"[gateway] modelctl STDOUT:",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
print(result.stdout, flush=True)
|
||||||
|
|
||||||
|
if result.stderr:
|
||||||
|
print(
|
||||||
|
"[gateway] modelctl STDERR:",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
print(result.stderr, flush=True)
|
||||||
|
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"modelctl failed with code {result.returncode}"
|
||||||
|
)
|
||||||
|
|
||||||
|
deadline = time.time() + 60
|
||||||
|
loaded = None
|
||||||
|
|
||||||
|
while time.time() < deadline:
|
||||||
|
loaded = get_loaded_model(target["port"])
|
||||||
|
|
||||||
|
if loaded == model_name:
|
||||||
|
print(
|
||||||
|
f"[gateway] {model_name} is ready "
|
||||||
|
f"on port {target["port"]}",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
return target["port"]
|
||||||
|
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Model switch timeout: "
|
||||||
|
f"expected {model_name}, "
|
||||||
|
f"got {loaded}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class GatewayHandler(BaseHTTPRequestHandler):
|
||||||
|
|
||||||
|
protocol_version = "HTTP/1.1"
|
||||||
|
|
||||||
|
def log_message(self, format, *args):
|
||||||
|
print(
|
||||||
|
f"[gateway] {self.address_string()} - "
|
||||||
|
f"{format % args}",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
def send_json(self, status, data):
|
||||||
|
body = json.dumps(
|
||||||
|
data,
|
||||||
|
ensure_ascii=False,
|
||||||
|
).encode("utf-8")
|
||||||
|
|
||||||
|
self.send_response(status)
|
||||||
|
self.send_header(
|
||||||
|
"Content-Type",
|
||||||
|
"application/json; charset=utf-8",
|
||||||
|
)
|
||||||
|
self.send_header(
|
||||||
|
"Content-Length",
|
||||||
|
str(len(body)),
|
||||||
|
)
|
||||||
|
self.send_header(
|
||||||
|
"Connection",
|
||||||
|
"close",
|
||||||
|
)
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
self.wfile.write(body)
|
||||||
|
|
||||||
|
def do_GET(self):
|
||||||
|
|
||||||
|
if self.path.startswith("/props"):
|
||||||
|
try:
|
||||||
|
parsed = urlparse(self.path)
|
||||||
|
params = parse_qs(parsed.query)
|
||||||
|
requested_model = params.get("model", [None])[0]
|
||||||
|
|
||||||
|
if requested_model:
|
||||||
|
models = get_models()
|
||||||
|
|
||||||
|
if requested_model not in models:
|
||||||
|
self.send_json(
|
||||||
|
404,
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"message": f"Unknown model: {requested_model}",
|
||||||
|
"type": "not_found",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
port = switch_model(requested_model)
|
||||||
|
|
||||||
|
else:
|
||||||
|
current = get_current_model()
|
||||||
|
|
||||||
|
if not current:
|
||||||
|
self.send_json(
|
||||||
|
503,
|
||||||
|
{"error": "No model is currently loaded"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
models = get_models()
|
||||||
|
|
||||||
|
if current not in models:
|
||||||
|
self.send_json(
|
||||||
|
500,
|
||||||
|
{
|
||||||
|
"error": (
|
||||||
|
f"Current model is not registered: {current}"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
port = models[current]["port"]
|
||||||
|
|
||||||
|
with urllib.request.urlopen(
|
||||||
|
f"{model_url(port)}/props",
|
||||||
|
timeout=3,
|
||||||
|
) as response:
|
||||||
|
data = json.loads(response.read())
|
||||||
|
|
||||||
|
self.send_json(200, data)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.send_json(
|
||||||
|
502,
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"message": str(e),
|
||||||
|
"type": "upstream_error",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.path == "/health":
|
||||||
|
self.send_json(
|
||||||
|
200,
|
||||||
|
{
|
||||||
|
"status": "ok",
|
||||||
|
"model": get_current_model(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.path == "/v1/models":
|
||||||
|
models = get_models()
|
||||||
|
|
||||||
|
data = []
|
||||||
|
|
||||||
|
for name in models:
|
||||||
|
data.append(
|
||||||
|
{
|
||||||
|
"id": name,
|
||||||
|
"object": "model",
|
||||||
|
"owned_by": "model-gateway",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.send_json(
|
||||||
|
200,
|
||||||
|
{
|
||||||
|
"object": "list",
|
||||||
|
"data": data,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.send_json(
|
||||||
|
404,
|
||||||
|
{"error": "Not found"},
|
||||||
|
)
|
||||||
|
|
||||||
|
def do_POST(self):
|
||||||
|
|
||||||
|
if self.path == "/api/show":
|
||||||
|
try:
|
||||||
|
length = int(
|
||||||
|
self.headers.get(
|
||||||
|
"Content-Length",
|
||||||
|
"0",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
body = self.rfile.read(length)
|
||||||
|
request = json.loads(body)
|
||||||
|
|
||||||
|
model_name = request.get("name")
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"[gateway] API SHOW: {model_name}",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
models = get_models()
|
||||||
|
|
||||||
|
if model_name not in models:
|
||||||
|
self.send_json(
|
||||||
|
404,
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"message":
|
||||||
|
f"Unknown model: {model_name}",
|
||||||
|
"type": "not_found",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.send_json(
|
||||||
|
200,
|
||||||
|
{
|
||||||
|
"name": model_name,
|
||||||
|
"model": model_name,
|
||||||
|
"details": {
|
||||||
|
"family": "model-gateway",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.send_json(
|
||||||
|
400,
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"message": str(e),
|
||||||
|
"type": "invalid_request_error",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.path not in (
|
||||||
|
"/v1/chat/completions",
|
||||||
|
"/v1/completions",
|
||||||
|
):
|
||||||
|
self.send_json(
|
||||||
|
404,
|
||||||
|
{"error": "Not found"},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
length = int(
|
||||||
|
self.headers.get(
|
||||||
|
"Content-Length",
|
||||||
|
"0",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
body = self.rfile.read(length)
|
||||||
|
request = json.loads(body)
|
||||||
|
|
||||||
|
model_name = request.get("model")
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"[gateway] REQUESTED MODEL: {model_name}",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not model_name:
|
||||||
|
self.send_json(
|
||||||
|
400,
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"message": "Missing model",
|
||||||
|
"type": "invalid_request_error",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
models = get_models()
|
||||||
|
|
||||||
|
if model_name not in models:
|
||||||
|
self.send_json(
|
||||||
|
400,
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"message":
|
||||||
|
f"Unknown model: {model_name}",
|
||||||
|
"type": "invalid_request_error",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
port = switch_model(model_name)
|
||||||
|
|
||||||
|
target_url = (
|
||||||
|
f"{model_url(port)}{self.path}"
|
||||||
|
)
|
||||||
|
|
||||||
|
proxy_request = urllib.request.Request(
|
||||||
|
target_url,
|
||||||
|
data=body,
|
||||||
|
method="POST",
|
||||||
|
headers={
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Connection": "close",
|
||||||
|
"Accept": "application/json",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
with urllib.request.urlopen(
|
||||||
|
proxy_request,
|
||||||
|
timeout=600,
|
||||||
|
) as response:
|
||||||
|
|
||||||
|
response_body = response.read()
|
||||||
|
|
||||||
|
self.send_response(
|
||||||
|
response.status
|
||||||
|
)
|
||||||
|
|
||||||
|
for header, value in response.headers.items():
|
||||||
|
if header.lower() in (
|
||||||
|
"content-length",
|
||||||
|
"connection",
|
||||||
|
"transfer-encoding",
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
|
||||||
|
self.send_header(
|
||||||
|
header,
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.send_header(
|
||||||
|
"Content-Length",
|
||||||
|
str(len(response_body)),
|
||||||
|
)
|
||||||
|
|
||||||
|
self.send_header(
|
||||||
|
"Connection",
|
||||||
|
"close",
|
||||||
|
)
|
||||||
|
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
self.wfile.write(response_body)
|
||||||
|
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
|
||||||
|
error_body = e.read()
|
||||||
|
|
||||||
|
self.send_response(e.code)
|
||||||
|
|
||||||
|
self.send_header(
|
||||||
|
"Content-Type",
|
||||||
|
"application/json",
|
||||||
|
)
|
||||||
|
self.send_header(
|
||||||
|
"Content-Length",
|
||||||
|
str(len(error_body)),
|
||||||
|
)
|
||||||
|
self.send_header(
|
||||||
|
"Connection",
|
||||||
|
"close",
|
||||||
|
)
|
||||||
|
|
||||||
|
self.end_headers()
|
||||||
|
|
||||||
|
self.wfile.write(error_body)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"[gateway] ERROR: {e}",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.send_json(
|
||||||
|
500,
|
||||||
|
{
|
||||||
|
"error": {
|
||||||
|
"message": str(e),
|
||||||
|
"type": "gateway_error",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
server = ThreadingHTTPServer(
|
||||||
|
(HOST, PORT),
|
||||||
|
GatewayHandler,
|
||||||
|
)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"[gateway] listening on "
|
||||||
|
f"{HOST}:{PORT}",
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
server.serve_forever()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user