Initial commit for deployment
This commit is contained in:
+110
@@ -0,0 +1,110 @@
|
||||
"""
|
||||
FastAPI routes for the application.
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
from app.services.chatbot_service import chatbot_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class MessageRequest(BaseModel):
|
||||
"""Request model for sending a message."""
|
||||
message: str
|
||||
user_id: str = "default_user"
|
||||
|
||||
class MessageResponse(BaseModel):
|
||||
"""Response model for a message."""
|
||||
content: str
|
||||
is_user: bool
|
||||
timestamp: str
|
||||
|
||||
class ChatResponse(BaseModel):
|
||||
"""Response model for a chat."""
|
||||
chat_id: int
|
||||
messages: List[MessageResponse]
|
||||
|
||||
@router.get("/health")
|
||||
async def health_check():
|
||||
"""
|
||||
Health check endpoint.
|
||||
|
||||
Returns:
|
||||
JSON response with health status.
|
||||
"""
|
||||
return {"status": "healthy"}
|
||||
|
||||
@router.post("/chat", response_model=ChatResponse)
|
||||
async def create_chat(user_id: str = "default_user"):
|
||||
"""
|
||||
Create a new chat.
|
||||
|
||||
Args:
|
||||
user_id: ID of the user creating the chat.
|
||||
|
||||
Returns:
|
||||
Created chat.
|
||||
"""
|
||||
chat_id = chatbot_service.create_chat(user_id)
|
||||
|
||||
return {
|
||||
"chat_id": chat_id,
|
||||
"messages": []
|
||||
}
|
||||
|
||||
@router.post("/chat/{chat_id}/message", response_model=MessageResponse)
|
||||
async def send_message(chat_id: int, request: MessageRequest):
|
||||
"""
|
||||
Send a message to the chatbot.
|
||||
|
||||
Args:
|
||||
chat_id: ID of the chat.
|
||||
request: Message request.
|
||||
|
||||
Returns:
|
||||
Bot response.
|
||||
"""
|
||||
try:
|
||||
response = chatbot_service.get_response(chat_id, request.message)
|
||||
|
||||
# Get the last message (bot response)
|
||||
messages = chatbot_service.get_chat_messages(chat_id)
|
||||
last_message = messages[-1]
|
||||
|
||||
return last_message
|
||||
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
|
||||
@router.get("/chat/{chat_id}", response_model=ChatResponse)
|
||||
async def get_chat(chat_id: int):
|
||||
"""
|
||||
Get a chat by ID.
|
||||
|
||||
Args:
|
||||
chat_id: ID of the chat.
|
||||
|
||||
Returns:
|
||||
Chat with messages.
|
||||
"""
|
||||
try:
|
||||
messages = chatbot_service.get_chat_messages(chat_id)
|
||||
|
||||
return {
|
||||
"chat_id": chat_id,
|
||||
"messages": messages
|
||||
}
|
||||
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
|
||||
def init_app(app):
|
||||
"""
|
||||
Initialize FastAPI application with routes.
|
||||
|
||||
Args:
|
||||
app: FastAPI application instance.
|
||||
"""
|
||||
app.include_router(router, prefix="/api")
|
||||
Reference in New Issue
Block a user