Skip to content

Instantly share code, notes, and snippets.

@voxels
Created July 4, 2026 18:56
Show Gist options
  • Select an option

  • Save voxels/65b15f432009d13ad8d968be4f5c6580 to your computer and use it in GitHub Desktop.

Select an option

Save voxels/65b15f432009d13ad8d968be4f5c6580 to your computer and use it in GitHub Desktop.
fm_proxy
from http.server import BaseHTTPRequestHandler, HTTPServer
from http.client import HTTPConnection
import time
import json
UPSTREAM_HOST = "127.0.0.1"
UPSTREAM_PORT = 1976
class Proxy(BaseHTTPRequestHandler):
def do_GET(self):
path = self.path
if path == "/v1/models?":
path = "/v1/models"
conn = HTTPConnection(UPSTREAM_HOST, UPSTREAM_PORT)
conn.request("GET", path, headers=self.headers)
resp = conn.getresponse()
self.send_response(resp.status)
for k, v in resp.getheaders():
if k.lower() != "transfer-encoding":
self.send_header(k, v)
self.end_headers()
self.wfile.write(resp.read())
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(length)
path = self.path
conn = HTTPConnection(UPSTREAM_HOST, UPSTREAM_PORT)
conn.request("POST", path, body=body, headers=dict(self.headers))
resp = conn.getresponse()
self.send_response(resp.status)
for k, v in resp.getheaders():
if k.lower() == "transfer-encoding":
continue
self.send_header(k, v)
self.end_headers()
while True:
chunk = resp.read(4096)
if not chunk:
break
text = chunk.decode("utf-8", errors="ignore")
for line in text.splitlines():
if not line.startswith("data:"):
continue
payload = line[6:].strip()
if payload == "[DONE]":
continue
obj = json.loads(payload)
# 🔥 PUT IT HERE
obj.setdefault("object", "chat.completion.chunk")
obj.setdefault("created", int(time.time()))
choice = obj["choices"][0]
choice.setdefault("index", 0)
choice.setdefault("finish_reason", None)
delta = choice.setdefault("delta", {})
delta.setdefault("content", None)
# re-emit SSE
self.wfile.write(f"data: {json.dumps(obj)}\n\n".encode())
self.wfile.flush()
HTTPServer(("127.0.0.1", 1977), Proxy).serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment