File size: 2,144 Bytes
a04ac86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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

@app.post("/chat", response_model=ChatResponse)
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))

@app.delete("/chat/{session_id}")
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))

@app.get("/")
def home():
    return {
        "message": "BioMistral AI Chat API is running πŸš€",
        "features": [
            "Conversational AI with session management",
            "Multi-turn medical conversations",
            "Session-based conversation history"
        ]
    }

@app.get("/health")
def health():
    """Health check endpoint."""
    return {"status": "healthy"}