Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from typing import Optional | |
| from model.biomistral_service import chat_with_biomistral, clear_session | |
| app = FastAPI( | |
| title="Quickcare", | |
| description="AI medical education and symptom assistant with conversation support", | |
| version="1.0.0" | |
| ) | |
| class ChatRequest(BaseModel): | |
| prompt: str | |
| session_id: Optional[str] = None | |
| class ChatResponse(BaseModel): | |
| response: str | |
| session_id: str | |
| async def chat_endpoint(request: ChatRequest): | |
| """ | |
| Chat endpoint that maintains conversation context using session_id. | |
| If session_id is not provided, a new session will be created. | |
| The same session_id can be used to continue a conversation. | |
| """ | |
| try: | |
| user_prompt = request.prompt.strip() | |
| if not user_prompt: | |
| raise HTTPException(status_code=400, detail="Prompt cannot be empty") | |
| response, session_id = chat_with_biomistral( | |
| user_prompt=user_prompt, | |
| session_id=request.session_id | |
| ) | |
| return ChatResponse(response=response, session_id=session_id) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def clear_chat_session(session_id: str): | |
| """Clear conversation history for a specific session.""" | |
| try: | |
| cleared = clear_session(session_id) | |
| if cleared: | |
| return {"message": f"Session {session_id} cleared successfully"} | |
| else: | |
| raise HTTPException(status_code=404, detail="Session not found") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def home(): | |
| return { | |
| "message": "BioMistral AI Chat API is running π", | |
| "features": [ | |
| "Conversational AI with session management", | |
| "Multi-turn medical conversations", | |
| "Session-based conversation history" | |
| ] | |
| } | |
| def health(): | |
| """Health check endpoint.""" | |
| return {"status": "healthy"} | |