Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
-
from fastapi.responses import
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
|
| 6 |
-
# Default model used internally; client
|
| 7 |
-
DEFAULT_MODEL = "
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
@@ -14,21 +14,15 @@ def health():
|
|
| 14 |
|
| 15 |
@app.post("/chat")
|
| 16 |
async def chat(request: Request):
|
| 17 |
-
# Accept plain text body;
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
raw = await request.body()
|
| 21 |
-
text = (raw or b"").decode().strip()
|
| 22 |
-
except Exception:
|
| 23 |
-
pass
|
| 24 |
-
|
| 25 |
if not text:
|
| 26 |
try:
|
| 27 |
data = await request.json()
|
| 28 |
text = str(data.get("text", "")).strip()
|
| 29 |
except Exception:
|
| 30 |
pass
|
| 31 |
-
|
| 32 |
if not text:
|
| 33 |
raise HTTPException(status_code=400, detail="Send plain text body or JSON {'text': '...'}")
|
| 34 |
|
|
@@ -36,10 +30,9 @@ async def chat(request: Request):
|
|
| 36 |
if not api_key:
|
| 37 |
raise HTTPException(status_code=500, detail="OPENROUTER_API_KEY not set")
|
| 38 |
|
| 39 |
-
# Forward to OpenRouter chat completions
|
| 40 |
try:
|
| 41 |
r = requests.post(
|
| 42 |
-
"https://openrouter.ai/api/v1/chat/completions", # [web:21]
|
| 43 |
headers={
|
| 44 |
"Authorization": f"Bearer {api_key}",
|
| 45 |
"Content-Type": "application/json",
|
|
@@ -53,21 +46,15 @@ async def chat(request: Request):
|
|
| 53 |
except requests.RequestException as e:
|
| 54 |
raise HTTPException(status_code=502, detail=str(e))
|
| 55 |
|
| 56 |
-
#
|
|
|
|
| 57 |
try:
|
| 58 |
data = r.json()
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if r.status_code >= 400:
|
| 63 |
-
return JSONResponse(content=data, status_code=r.status_code)
|
| 64 |
-
|
| 65 |
-
content = ""
|
| 66 |
-
try:
|
| 67 |
content = data["choices"][0]["message"]["content"]
|
| 68 |
except Exception:
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
return {"content": content}
|
| 73 |
-
return data
|
|
|
|
| 1 |
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
+
from fastapi.responses import PlainTextResponse
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
# Default model used internally; the client only sends plain text
|
| 7 |
+
DEFAULT_MODEL = "kwaipilot/kat-coder-pro:free"
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 14 |
|
| 15 |
@app.post("/chat")
|
| 16 |
async def chat(request: Request):
|
| 17 |
+
# Accept plain text body; fallback to JSON {"text": "..."}
|
| 18 |
+
body = await request.body()
|
| 19 |
+
text = (body or b"").decode().strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
if not text:
|
| 21 |
try:
|
| 22 |
data = await request.json()
|
| 23 |
text = str(data.get("text", "")).strip()
|
| 24 |
except Exception:
|
| 25 |
pass
|
|
|
|
| 26 |
if not text:
|
| 27 |
raise HTTPException(status_code=400, detail="Send plain text body or JSON {'text': '...'}")
|
| 28 |
|
|
|
|
| 30 |
if not api_key:
|
| 31 |
raise HTTPException(status_code=500, detail="OPENROUTER_API_KEY not set")
|
| 32 |
|
|
|
|
| 33 |
try:
|
| 34 |
r = requests.post(
|
| 35 |
+
"https://openrouter.ai/api/v1/chat/completions", # OpenRouter chat completions [web:21]
|
| 36 |
headers={
|
| 37 |
"Authorization": f"Bearer {api_key}",
|
| 38 |
"Content-Type": "application/json",
|
|
|
|
| 46 |
except requests.RequestException as e:
|
| 47 |
raise HTTPException(status_code=502, detail=str(e))
|
| 48 |
|
| 49 |
+
# Try to extract assistant text; if not possible, return the raw body from OpenRouter
|
| 50 |
+
content = None
|
| 51 |
try:
|
| 52 |
data = r.json()
|
| 53 |
+
if r.status_code >= 400:
|
| 54 |
+
return PlainTextResponse(str(data), status_code=r.status_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
content = data["choices"][0]["message"]["content"]
|
| 56 |
except Exception:
|
| 57 |
+
# Fall back to raw text (may be JSON or error text)
|
| 58 |
+
return PlainTextResponse(r.text, status_code=r.status_code)
|
| 59 |
|
| 60 |
+
return PlainTextResponse(content)
|
|
|
|
|
|