feat: OpenAI-kompatible API mit Keys, Registry und serve-Befehl (M2.3)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -7,7 +7,11 @@ und verhält sich nach FAKE_LLAMA_MODE:
|
||||
crash schreibt einen Fehler nach stderr und endet mit Exit-Code 3
|
||||
hang wird nie bereit (immer 503)
|
||||
stubborn wie ok, ignoriert aber SIGTERM (zwingt den Adapter zum harten Beenden)
|
||||
|
||||
Antwortet außerdem auf GET /v1/models und POST /v1/chat/completions (mit und
|
||||
ohne SSE-Streaming) im OpenAI-Format, damit SDK-Tests das Gateway prüfen können.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
@ -39,22 +43,63 @@ started = time.time()
|
||||
load_seconds = float(os.environ.get("FAKE_LLAMA_LOAD_SECONDS", "1"))
|
||||
|
||||
|
||||
ANSWER = ["Hallo", " vom", " Attrappen-", "Modell."]
|
||||
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def log_message(self, *_):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
if self.path == "/health":
|
||||
ready = mode in ("ok", "stubborn") and time.time() - started >= load_seconds
|
||||
code = 200 if ready else 503
|
||||
body = b'{"status":"ok"}' if ready else b'{"error":{"message":"Loading model"}}'
|
||||
else:
|
||||
code, body = 404, b"{}"
|
||||
def _send_json(self, code, payload):
|
||||
body = json.dumps(payload).encode()
|
||||
self.send_response(code)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_GET(self):
|
||||
if self.path == "/health":
|
||||
ready = mode in ("ok", "stubborn") and time.time() - started >= load_seconds
|
||||
if ready:
|
||||
self._send_json(200, {"status": "ok"})
|
||||
else:
|
||||
self._send_json(503, {"error": {"message": "Loading model"}})
|
||||
elif self.path == "/v1/models":
|
||||
alias = opt("--alias") or "modell"
|
||||
self._send_json(200, {"object": "list", "data": [{"id": alias, "object": "model", "created": 0, "owned_by": "fake"}]})
|
||||
else:
|
||||
self._send_json(404, {})
|
||||
|
||||
def do_POST(self):
|
||||
length = int(self.headers.get("Content-Length", "0"))
|
||||
request = json.loads(self.rfile.read(length) or b"{}")
|
||||
if self.path != "/v1/chat/completions":
|
||||
self._send_json(404, {})
|
||||
return
|
||||
model = request.get("model", "modell")
|
||||
if request.get("stream"):
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/event-stream")
|
||||
self.send_header("Cache-Control", "no-cache")
|
||||
self.end_headers()
|
||||
for i, part in enumerate(ANSWER):
|
||||
chunk = {"id": "chatcmpl-fake", "object": "chat.completion.chunk", "created": 1, "model": model,
|
||||
"choices": [{"index": 0, "delta": {"role": "assistant", "content": part} if i == 0 else {"content": part}, "finish_reason": None}]}
|
||||
self.wfile.write(("data: " + json.dumps(chunk) + "\n\n").encode())
|
||||
self.wfile.flush()
|
||||
time.sleep(0.05)
|
||||
last = {"id": "chatcmpl-fake", "object": "chat.completion.chunk", "created": 1, "model": model,
|
||||
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}
|
||||
self.wfile.write(("data: " + json.dumps(last) + "\n\ndata: [DONE]\n\n").encode())
|
||||
self.wfile.flush()
|
||||
else:
|
||||
self._send_json(200, {
|
||||
"id": "chatcmpl-fake", "object": "chat.completion", "created": 1, "model": model,
|
||||
"choices": [{"index": 0, "finish_reason": "stop",
|
||||
"message": {"role": "assistant", "content": "".join(ANSWER)}}],
|
||||
"usage": {"prompt_tokens": 3, "completion_tokens": 4, "total_tokens": 7},
|
||||
})
|
||||
|
||||
|
||||
ThreadingHTTPServer((opt("--host"), int(opt("--port"))), Handler).serve_forever()
|
||||
|
||||
Reference in New Issue
Block a user