Download app/api/chat.py from nvikou/ReplyFlow: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/nvikou/ReplyFlow/resolve/main/app/api/chat.py
- Command line
-
hf download hf://spaces/nvikou/ReplyFlow/app/api/chat.py
-
curl -L -o chat.py https://huggingface.co/spaces/nvikou/ReplyFlow/resolve/main/app/api/chat.py
1.37 kB
| """Chat API routes.""" | |
| from fastapi import APIRouter, Depends | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.db import get_db | |
| from app.schemas import ( | |
| ChatMessageRequest, | |
| ChatMessageResponse, | |
| ChatStartResponse, | |
| ) | |
| from app.services.chat import handle_user_message, start_conversation | |
| from app.services.language import normalize_ui_language | |
| router = APIRouter(prefix="/api/chat", tags=["chat"]) | |
| async def chat_start( | |
| ui_language: str = "en", | |
| db: AsyncSession = Depends(get_db), | |
| ) -> ChatStartResponse: | |
| """Start a new web conversation.""" | |
| lang = normalize_ui_language(ui_language) | |
| conv, greeting = await start_conversation( | |
| db, channel="web", ui_language=lang | |
| ) | |
| return ChatStartResponse( | |
| conversation_id=conv.id, | |
| greeting=greeting, | |
| language=lang, | |
| ) | |
| async def chat_message( | |
| body: ChatMessageRequest, | |
| db: AsyncSession = Depends(get_db), | |
| ) -> ChatMessageResponse: | |
| """Send a user message and receive the bot reply.""" | |
| data = await handle_user_message( | |
| db, | |
| conversation_id=body.conversation_id, | |
| message=body.message, | |
| ui_language=normalize_ui_language(body.ui_language), | |
| ) | |
| return ChatMessageResponse(**data) | |