Upload 3 files
Browse files- app/routers/__init__.py +0 -0
- app/routers/chat.py +87 -0
- app/routers/health.py +19 -0
app/routers/__init__.py
ADDED
|
File without changes
|
app/routers/chat.py
CHANGED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Endpoints الدردشة: رد كامل دفعة واحدة أو بث لحظي (SSE)
|
| 3 |
+
"""
|
| 4 |
+
import json
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
from fastapi import APIRouter, Depends
|
| 8 |
+
from fastapi.responses import StreamingResponse
|
| 9 |
+
|
| 10 |
+
from app.config import Settings, get_settings
|
| 11 |
+
from app.dependencies import get_hf_service, verify_api_key
|
| 12 |
+
from app.schemas import ChatRequest, ChatResponse, ErrorResponse
|
| 13 |
+
from app.services.hf_client import HFClientError, HFInferenceService
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger("hf_chat_backend.chat")
|
| 16 |
+
|
| 17 |
+
router = APIRouter(
|
| 18 |
+
prefix="/chat",
|
| 19 |
+
tags=["Chat"],
|
| 20 |
+
dependencies=[Depends(verify_api_key)],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@router.post(
|
| 25 |
+
"",
|
| 26 |
+
response_model=ChatResponse,
|
| 27 |
+
responses={502: {"model": ErrorResponse}},
|
| 28 |
+
summary="إرسال محادثة والحصول على رد كامل من النموذج",
|
| 29 |
+
)
|
| 30 |
+
async def chat(
|
| 31 |
+
payload: ChatRequest,
|
| 32 |
+
hf_service: HFInferenceService = Depends(get_hf_service),
|
| 33 |
+
settings: Settings = Depends(get_settings),
|
| 34 |
+
) -> ChatResponse | StreamingResponse:
|
| 35 |
+
if payload.stream:
|
| 36 |
+
return await chat_stream(payload, hf_service, settings)
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
reply, finish_reason = await hf_service.chat_completion(
|
| 40 |
+
messages=payload.messages,
|
| 41 |
+
max_new_tokens=payload.max_new_tokens,
|
| 42 |
+
temperature=payload.temperature,
|
| 43 |
+
top_p=payload.top_p,
|
| 44 |
+
)
|
| 45 |
+
except HFClientError as exc:
|
| 46 |
+
logger.error("HF client error: %s", exc)
|
| 47 |
+
return _error_response(exc)
|
| 48 |
+
|
| 49 |
+
return ChatResponse(
|
| 50 |
+
model=settings.hf_model_id,
|
| 51 |
+
reply=reply,
|
| 52 |
+
finish_reason=finish_reason,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
async def chat_stream(
|
| 57 |
+
payload: ChatRequest,
|
| 58 |
+
hf_service: HFInferenceService,
|
| 59 |
+
settings: Settings,
|
| 60 |
+
) -> StreamingResponse:
|
| 61 |
+
async def event_generator():
|
| 62 |
+
try:
|
| 63 |
+
async for delta in hf_service.chat_completion_stream(
|
| 64 |
+
messages=payload.messages,
|
| 65 |
+
max_new_tokens=payload.max_new_tokens,
|
| 66 |
+
temperature=payload.temperature,
|
| 67 |
+
top_p=payload.top_p,
|
| 68 |
+
):
|
| 69 |
+
yield f"data: {json.dumps({'delta': delta}, ensure_ascii=False)}\n\n"
|
| 70 |
+
yield "data: [DONE]\n\n"
|
| 71 |
+
except HFClientError as exc:
|
| 72 |
+
logger.error("HF client stream error: %s", exc)
|
| 73 |
+
error_payload = json.dumps({"error": str(exc)}, ensure_ascii=False)
|
| 74 |
+
yield f"data: {error_payload}\n\n"
|
| 75 |
+
|
| 76 |
+
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def _error_response(exc: HFClientError):
|
| 80 |
+
from fastapi.responses import JSONResponse
|
| 81 |
+
|
| 82 |
+
return JSONResponse(
|
| 83 |
+
status_code=502,
|
| 84 |
+
content=ErrorResponse(
|
| 85 |
+
error="hf_inference_error", detail=str(exc)
|
| 86 |
+
).model_dump(),
|
| 87 |
+
)
|
app/routers/health.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Endpoint فحص الصحة (Health Check)
|
| 3 |
+
"""
|
| 4 |
+
from fastapi import APIRouter, Depends
|
| 5 |
+
|
| 6 |
+
from app.config import Settings, get_settings
|
| 7 |
+
from app.schemas import HealthResponse
|
| 8 |
+
|
| 9 |
+
router = APIRouter(tags=["Health"])
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@router.get("/health", response_model=HealthResponse, summary="فحص صحة الخدمة")
|
| 13 |
+
async def health_check(settings: Settings = Depends(get_settings)) -> HealthResponse:
|
| 14 |
+
return HealthResponse(
|
| 15 |
+
status="ok",
|
| 16 |
+
app_name=settings.app_name,
|
| 17 |
+
app_env=settings.app_env,
|
| 18 |
+
model=settings.hf_model_id,
|
| 19 |
+
)
|