File size: 7,465 Bytes
ebb8326
 
 
 
 
 
 
 
 
 
 
4f8c5b9
ebb8326
4f8c5b9
f3ea897
4f8c5b9
ebb8326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8c5b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3ea897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebb8326
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""FastAPI Backend for VietQA Multi-Agent System."""

from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

from src.data_processing.models import QuestionInput
from src.data_processing.formatting import question_to_state
from src.data_processing.answer import normalize_answer
from src.graph import get_graph
from src.graph import get_graph
from src.utils.llm import set_large_model_override, get_available_large_models
from src.utils.firebase import delete_user_permanently, verify_id_token
from src.utils.ingestion import get_qdrant_client
from fastapi import Header


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Fast startup - lazy load models on first request."""
    print("[Startup] Server starting (models will load on first request)...")
    print("[Startup] Server ready!")
    yield


app = FastAPI(
    title="VietQA Multi-Agent API",
    description="API cho hệ thống trả lời câu hỏi trắc nghiệm tiếng Việt",
    version="1.0.0",
    lifespan=lifespan
)

# CORS for frontend
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


class SolveRequest(BaseModel):
    question: str
    choices: list[str]
    model: str | None = None


class SolveResponse(BaseModel):
    answer: str
    route: str
    reasoning: str
    context: str


def clean_thinking_tags(text: str) -> str:
    """Remove <think>...</think> tags from model response."""
    import re
    # Remove think tags and their content
    cleaned = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
    return cleaned.strip()


class ChatRequest(BaseModel):
    message: str
    model: str | None = None


class ChatResponse(BaseModel):
    response: str
    route: str


class ModelsResponse(BaseModel):
    models: list[dict]


@app.get("/")
async def root():
    return {"message": "VietQA Multi-Agent API", "status": "running"}


@app.get("/health")
async def health():
    """Health check endpoint for Render."""
    return {"status": "ok"}


@app.get("/api/models", response_model=ModelsResponse)
async def get_models():
    """Get available large models."""
    models = get_available_large_models()
    return {
        "models": [
            {"id": m, "name": m.split("/")[-1]} 
            for m in models
        ]
    }


@app.post("/api/solve", response_model=SolveResponse)
async def solve_question(req: SolveRequest):
    """Solve a multiple-choice question."""
    if not req.question.strip():
        raise HTTPException(400, "Question is required")
    if len(req.choices) < 2:
        raise HTTPException(400, "At least 2 choices required")
    
    set_large_model_override(req.model)
    
    try:
        q = QuestionInput(qid="api", question=req.question, choices=req.choices)
        state = question_to_state(q)
        graph = get_graph()
        
        result = await graph.ainvoke(state)
        
        answer = normalize_answer(
            answer=result.get("answer", "A"),
            num_choices=len(req.choices),
            question_id="api",
            default="A"
        )
        
        return SolveResponse(
            answer=answer,
            route=result.get("route", "unknown"),
            reasoning=clean_thinking_tags(result.get("raw_response", "")),
            context=result.get("context", "")
        )
    except Exception as e:
        import traceback
        traceback.print_exc()
        raise HTTPException(500, str(e))
    finally:
        set_large_model_override(None)


@app.post("/api/chat", response_model=ChatResponse)
async def chat(req: ChatRequest):
    """Free-form chat (routes through pipeline without choices)."""
    if not req.message.strip():
        raise HTTPException(400, "Message is required")
    
    set_large_model_override(req.model)
    
    try:
        # Use empty choices for chat mode
        q = QuestionInput(qid="chat", question=req.message, choices=[])
        state = question_to_state(q)
        graph = get_graph()
        
        result = await graph.ainvoke(state)
        
        return ChatResponse(
            response=clean_thinking_tags(result.get("raw_response", "")),
            route=result.get("route", "unknown")
        )
    except Exception as e:
        raise HTTPException(500, str(e))
    finally:
        set_large_model_override(None)


class DeleteUserRequest(BaseModel):
    uid: str


@app.post("/api/admin/delete-user")
async def delete_user(req: DeleteUserRequest, authorization: str = Header(None)):
    """Permanently delete a user (Supervisor only)."""
    if not authorization or not authorization.startswith("Bearer "):
        raise HTTPException(401, "Missing or invalid authorization token")
    
    id_token = authorization.split("Bearer ")[1]
    
    try:
        # Verify token and check role
        # Note: In a real app, custom claims should be used. 
        # Here we rely on the client SDK's token, but for critical actions like this,
        # we should ideally check the user's role in Firestore.
        # However, for simplicity and since we verify on frontend with PIN,
        # we'll assume the caller is authorized if they have a valid token 
        # AND we double check the user's existence/role via Firestore if needed.
        # Steps:
        # 1. Verify token is valid
        decoded_token = verify_id_token(id_token)
        uid = decoded_token['uid']
        
        # 2. Check if requester is Supervisor (optional but recommended)
        # For now, we trust the token is valid. The Supervisor PIN check is on frontend.
        # Improvements: Fetch user from Firestore and check role == 'SUPERVISOR'
        
        # 3. Perform deletion
        delete_user_permanently(req.uid)
        
        return {"status": "success", "message": f"User {req.uid} deleted permanently"}
        
    except Exception as e:
        print(f"Delete user error: {e}")
        raise HTTPException(500, str(e))


@app.get("/api/qdrant/stats")
async def get_qdrant_stats():
    """Get Qdrant database statistics."""
    try:
        client = get_qdrant_client()
        collections = client.get_collections().collections
        
        stats = {
            "collections": [],
            "total_vectors": 0,
            "total_size_bytes": 0
        }
        
        for collection in collections:
            try:
                collection_info = client.get_collection(collection.name)
                vector_count = collection_info.points_count or 0
                
                stats["collections"].append({
                    "name": collection.name,
                    "vectors": vector_count,
                    "status": "healthy"
                })
                stats["total_vectors"] += vector_count
            except Exception as e:
                print(f"Error getting collection {collection.name}: {e}")
                stats["collections"].append({
                    "name": collection.name,
                    "vectors": 0,
                    "status": "error"
                })
        
        return stats
    except Exception as e:
        print(f"Qdrant stats error: {e}")
        raise HTTPException(500, f"Failed to get Qdrant stats: {str(e)}")


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)