File size: 2,045 Bytes
723ec33
9dc2067
723ec33
 
 
9dc2067
 
add72f2
723ec33
 
 
 
 
add72f2
 
723ec33
 
 
 
9dc2067
 
 
723ec33
 
 
 
 
 
 
 
 
 
 
 
 
 
9dc2067
723ec33
 
 
 
 
 
 
 
 
 
 
 
 
9dc2067
 
723ec33
 
9dc2067
 
723ec33
 
9dc2067
 
723ec33
9dc2067
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import PlainTextResponse
import os
import requests

# Default model used internally; the client only sends plain text
DEFAULT_MODEL = "kwaipilot/kat-coder-pro:free"
api_key = os.getenv("OPENROUTER_API_KEY")

app = FastAPI()

@app.get("/")
def health():
    if not api_key:
        return {"status": "no key"}
    return {"status": "ok"}

@app.post("/chat")
async def chat(request: Request):
    # Accept plain text body; fallback to JSON {"text": "..."}
    body = await request.body()
    text = (body or b"").decode().strip()
    if not text:
        try:
            data = await request.json()
            text = str(data.get("text", "")).strip()
        except Exception:
            pass
    if not text:
        raise HTTPException(status_code=400, detail="Send plain text body or JSON {'text': '...'}")

    if not api_key:
        raise HTTPException(status_code=500, detail="OPENROUTER_API_KEY not set")

    try:
        r = requests.post(
            "https://openrouter.ai/api/v1/chat/completions",  # OpenRouter chat completions [web:21]
            headers={
                "Authorization": f"Bearer {api_key}",
                "Content-Type": "application/json",
            },
            json={
                "model": DEFAULT_MODEL,
                "messages": [{"role": "user", "content": text}],
            },
            timeout=60,
        )
    except requests.RequestException as e:
        raise HTTPException(status_code=502, detail=str(e))

    # Try to extract assistant text; if not possible, return the raw body from OpenRouter
    content = None
    try:
        data = r.json()
        if r.status_code >= 400:
            return PlainTextResponse(str(data), status_code=r.status_code)
        content = data["choices"][0]["message"]["content"]
    except Exception:
        # Fall back to raw text (may be JSON or error text)
        return PlainTextResponse(r.text, status_code=r.status_code)

    return PlainTextResponse(content)