Use stdlib HTTP proxy for /v1/responses compatibility
Browse files- app/routes.py +16 -9
app/routes.py
CHANGED
|
@@ -5,11 +5,11 @@ import queue
|
|
| 5 |
import re
|
| 6 |
import threading
|
| 7 |
import time
|
|
|
|
| 8 |
from uuid import uuid4
|
| 9 |
|
| 10 |
from fastapi import APIRouter, HTTPException, Request
|
| 11 |
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse, StreamingResponse
|
| 12 |
-
from curl_cffi import requests as curl_requests
|
| 13 |
|
| 14 |
from . import chat, config, constants, session as session_module
|
| 15 |
from .account import (
|
|
@@ -175,18 +175,25 @@ def _call_local_chat_completions(chat_body: dict, auth_header: str, x_api_key: s
|
|
| 175 |
headers["Authorization"] = auth_header
|
| 176 |
if x_api_key:
|
| 177 |
headers["x-api-key"] = x_api_key
|
| 178 |
-
|
| 179 |
f"http://127.0.0.1:{local_port}/v1/chat/completions",
|
|
|
|
| 180 |
headers=headers,
|
| 181 |
-
|
| 182 |
-
impersonate="safari15_3",
|
| 183 |
-
timeout=180,
|
| 184 |
)
|
| 185 |
try:
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
def _chat_completion_to_response_payload(chat_payload: dict) -> dict:
|
|
|
|
| 5 |
import re
|
| 6 |
import threading
|
| 7 |
import time
|
| 8 |
+
import urllib.request
|
| 9 |
from uuid import uuid4
|
| 10 |
|
| 11 |
from fastapi import APIRouter, HTTPException, Request
|
| 12 |
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse, StreamingResponse
|
|
|
|
| 13 |
|
| 14 |
from . import chat, config, constants, session as session_module
|
| 15 |
from .account import (
|
|
|
|
| 175 |
headers["Authorization"] = auth_header
|
| 176 |
if x_api_key:
|
| 177 |
headers["x-api-key"] = x_api_key
|
| 178 |
+
req = urllib.request.Request(
|
| 179 |
f"http://127.0.0.1:{local_port}/v1/chat/completions",
|
| 180 |
+
data=json.dumps(chat_body).encode("utf-8"),
|
| 181 |
headers=headers,
|
| 182 |
+
method="POST",
|
|
|
|
|
|
|
| 183 |
)
|
| 184 |
try:
|
| 185 |
+
with urllib.request.urlopen(req, timeout=180) as resp:
|
| 186 |
+
raw = resp.read().decode("utf-8")
|
| 187 |
+
return resp.status, json.loads(raw)
|
| 188 |
+
except urllib.error.HTTPError as exc:
|
| 189 |
+
raw = exc.read().decode("utf-8", errors="replace")
|
| 190 |
+
try:
|
| 191 |
+
payload = json.loads(raw)
|
| 192 |
+
except Exception:
|
| 193 |
+
payload = {"error": raw}
|
| 194 |
+
return exc.code, payload
|
| 195 |
+
except Exception as exc:
|
| 196 |
+
return 500, {"error": str(exc)}
|
| 197 |
|
| 198 |
|
| 199 |
def _chat_completion_to_response_payload(chat_payload: dict) -> dict:
|