24 lines
706 B
Python
24 lines
706 B
Python
from fastapi import APIRouter, HTTPException
|
|
from schema.ai_schema import AIChatRequest, AIChatResponse
|
|
from service.ai_service import ask_ai, ai_status
|
|
|
|
# router = APIRouter(prefix="/ai", tags=["AI助手"])
|
|
router = APIRouter()
|
|
|
|
@router.get("/status")
|
|
def status():
|
|
try:
|
|
return ai_status()
|
|
except RuntimeError as error:
|
|
raise HTTPException(503, str(error)) from None
|
|
|
|
|
|
@router.post("/chat", response_model=AIChatResponse)
|
|
def chat(request: AIChatRequest):
|
|
try:
|
|
return ask_ai(request.question)
|
|
except RuntimeError as error:
|
|
raise HTTPException(503, str(error)) from None
|
|
except ValueError as error:
|
|
raise HTTPException(502, str(error)) from None
|