Install modular API, voice and embedding extras and use the supported single-process server entrypoint. Drain worker SIGTERM shutdown within the Compose grace period. Add isolated HTTPS embedding fixtures, bounded intake execution and reproducible account, publication, restart, lease-recovery and shutdown acceptance. Eight operational scenarios passed with zero real-provider calls.
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
"""Loopback/internal-network fixture, never a production embedding provider."""
|
|
import hashlib
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
import json
|
|
import ssl
|
|
from threading import Event
|
|
|
|
CALLS = 0
|
|
RELEASE = Event()
|
|
RELEASE.set()
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def log_message(self, *_):
|
|
pass # Never log inputs or Authorization headers.
|
|
|
|
def reply(self, data):
|
|
body = json.dumps(data).encode()
|
|
self.send_response(200)
|
|
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 == '/block':
|
|
RELEASE.clear()
|
|
elif self.path == '/release':
|
|
RELEASE.set()
|
|
self.reply({'fixture_calls': CALLS, 'real_provider_calls': 0})
|
|
|
|
def do_POST(self):
|
|
global CALLS
|
|
length = int(self.headers.get('Content-Length', '0'))
|
|
if self.path != '/v1/embeddings' or not 0 < length <= 1024 * 1024:
|
|
self.send_error(400)
|
|
return
|
|
data = json.loads(self.rfile.read(length))
|
|
CALLS += 1
|
|
RELEASE.wait(30)
|
|
self.reply({'model': data['model'], 'data': [dict(index=i, embedding=[float(1 + n) for n in hashlib.sha256(value.encode()).digest()[:3]])
|
|
for i, value in enumerate(data['input'])]})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
server=ThreadingHTTPServer(('0.0.0.0', 8090), Handler)
|
|
context=ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
context.load_cert_chain('/fixtures/tls/cert.pem','/fixtures/tls/key.pem')
|
|
server.socket=context.wrap_socket(server.socket,server_side=True)
|
|
server.serve_forever()
|