from fastapi import APIRouter
from fastapi.responses import StreamingResponse
from app.constants.model_data import base_url, headers, tip_message
from app.services.llm_request import stream_post_request
from app.schemas import ChatRequest

router = APIRouter()


@router.post("/completions")
async def chat(data: ChatRequest):
    all_messages = [tip_message] + data.messages
    all_messages_dict = [
        m.model_dump() if hasattr(m, "model_dump") else m.dict() if hasattr(m, "dict") else m
        for m in all_messages
    ]
    payload = {"model": data.model, "messages": all_messages_dict, "stream": True}
    print(payload)
    return StreamingResponse(
        stream_post_request(
            url=base_url,
            headers=headers,
            json=payload,
        ),
        media_type="text/event-stream"
    )
