Update app/main.py
Browse files- app/main.py +31 -18
app/main.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
-
import json
|
| 3 |
from pathlib import Path
|
| 4 |
from fastapi import FastAPI, Request, HTTPException
|
| 5 |
from fastapi.responses import StreamingResponse, HTMLResponse, JSONResponse
|
| 6 |
from fastapi.staticfiles import StaticFiles
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
-
from agent_system import orchestrator
|
| 9 |
|
| 10 |
-
app = FastAPI(title="PraisonChat", version="
|
| 11 |
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
|
@@ -17,6 +16,7 @@ app.add_middleware(
|
|
| 17 |
)
|
| 18 |
|
| 19 |
STATIC_DIR = Path(__file__).parent / "static"
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
@app.get("/", response_class=HTMLResponse)
|
|
@@ -26,18 +26,28 @@ async def root():
|
|
| 26 |
|
| 27 |
@app.get("/api/health")
|
| 28 |
def health():
|
| 29 |
-
return {"status": "ok", "model": "LongCat-Flash-Lite", "version": "
|
| 30 |
|
| 31 |
|
| 32 |
@app.get("/api/models")
|
| 33 |
def models():
|
| 34 |
-
return {
|
| 35 |
-
"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
@app.post("/api/chat")
|
|
@@ -47,12 +57,12 @@ async def chat(request: Request):
|
|
| 47 |
except Exception:
|
| 48 |
raise HTTPException(400, "Invalid JSON body")
|
| 49 |
|
| 50 |
-
messages
|
| 51 |
-
api_key
|
|
|
|
| 52 |
|
| 53 |
if not api_key:
|
| 54 |
-
raise HTTPException(400, "LongCat API key
|
| 55 |
-
|
| 56 |
if not messages:
|
| 57 |
raise HTTPException(400, "No messages provided")
|
| 58 |
|
|
@@ -60,8 +70,11 @@ async def chat(request: Request):
|
|
| 60 |
history = messages[:-1]
|
| 61 |
|
| 62 |
async def event_stream():
|
| 63 |
-
async for chunk in orchestrator.stream_response(user_message, history, api_key):
|
| 64 |
yield f"data: {chunk}\n\n"
|
| 65 |
|
| 66 |
-
return StreamingResponse(
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.responses import StreamingResponse, HTMLResponse, JSONResponse
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from agent_system import orchestrator, BUILTIN_TOOLS
|
| 8 |
|
| 9 |
+
app = FastAPI(title="PraisonChat", version="2.0.0")
|
| 10 |
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
STATIC_DIR = Path(__file__).parent / "static"
|
| 19 |
+
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 20 |
|
| 21 |
|
| 22 |
@app.get("/", response_class=HTMLResponse)
|
|
|
|
| 26 |
|
| 27 |
@app.get("/api/health")
|
| 28 |
def health():
|
| 29 |
+
return {"status": "ok", "model": "LongCat-Flash-Lite", "version": "2.0.0"}
|
| 30 |
|
| 31 |
|
| 32 |
@app.get("/api/models")
|
| 33 |
def models():
|
| 34 |
+
return {"models": [
|
| 35 |
+
{"id": "LongCat-Flash-Lite", "name": "LongCat Flash Lite", "context": "320K", "speed": "β‘ Fastest", "quota": "50M/day"},
|
| 36 |
+
{"id": "LongCat-Flash-Chat", "name": "LongCat Flash Chat", "context": "256K", "speed": "π Fast", "quota": "500K/day"},
|
| 37 |
+
{"id": "LongCat-Flash-Thinking-2601", "name": "LongCat Flash Thinking", "context": "256K", "speed": "π§ Deep", "quota": "500K/day"},
|
| 38 |
+
]}
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@app.get("/api/builtin-tools")
|
| 42 |
+
def builtin_tools_list():
|
| 43 |
+
tools = [
|
| 44 |
+
{"name": "get_current_datetime", "description": "Returns the current date and time", "icon": "π"},
|
| 45 |
+
{"name": "calculate_math", "description": "Evaluates mathematical expressions", "icon": "π’"},
|
| 46 |
+
{"name": "run_python_code", "description": "Executes Python code in a sandbox", "icon": "π"},
|
| 47 |
+
{"name": "create_voice_response", "description": "Converts text to spoken audio (gTTS)","icon": "π"},
|
| 48 |
+
{"name": "search_information", "description": "Searches for information", "icon": "π"},
|
| 49 |
+
]
|
| 50 |
+
return {"tools": tools}
|
| 51 |
|
| 52 |
|
| 53 |
@app.post("/api/chat")
|
|
|
|
| 57 |
except Exception:
|
| 58 |
raise HTTPException(400, "Invalid JSON body")
|
| 59 |
|
| 60 |
+
messages = body.get("messages", [])
|
| 61 |
+
api_key = body.get("api_key") or os.getenv("LONGCAT_API_KEY", "")
|
| 62 |
+
model = body.get("model", "LongCat-Flash-Lite")
|
| 63 |
|
| 64 |
if not api_key:
|
| 65 |
+
raise HTTPException(400, "LongCat API key required. Add it in Settings or set LONGCAT_API_KEY env var.")
|
|
|
|
| 66 |
if not messages:
|
| 67 |
raise HTTPException(400, "No messages provided")
|
| 68 |
|
|
|
|
| 70 |
history = messages[:-1]
|
| 71 |
|
| 72 |
async def event_stream():
|
| 73 |
+
async for chunk in orchestrator.stream_response(user_message, history, api_key, model):
|
| 74 |
yield f"data: {chunk}\n\n"
|
| 75 |
|
| 76 |
+
return StreamingResponse(
|
| 77 |
+
event_stream(),
|
| 78 |
+
media_type="text/event-stream",
|
| 79 |
+
headers={"X-Accel-Buffering": "no", "Cache-Control": "no-cache", "Connection": "keep-alive"},
|
| 80 |
+
)
|