Spaces:
Sleeping
Sleeping
File size: 16,927 Bytes
a02d20d | 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | from fastapi import FastAPI, HTTPException, Depends, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel, EmailStr
from typing import Optional, List
import os
import jwt
import httpx
import hashlib
import json
from datetime import datetime, timedelta
app = FastAPI(title="LearnFlow API Gateway")
# CORS configuration
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://localhost:3001", "*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Configuration
SECRET_KEY = os.getenv("JWT_SECRET_KEY", "learnflow-secret-key-change-in-production")
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_HOURS = 24
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
OPENROUTER_BASE_URL = os.getenv("OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1")
LLM_MODEL = os.getenv("LLM_MODEL", "openai/gpt-3.5-turbo")
# Service URLs
SERVICES = {
"triage": os.getenv("TRIAGE_URL", "http://localhost:8001"),
"concepts": os.getenv("CONCEPTS_URL", "http://localhost:8002"),
"code_review": os.getenv("CODE_REVIEW_URL", "http://localhost:8003"),
"debug": os.getenv("DEBUG_URL", "http://localhost:8004"),
"exercise": os.getenv("EXERCISE_URL", "http://localhost:8005"),
"progress": os.getenv("PROGRESS_URL", "http://localhost:8006"),
}
# In-memory user store (replace with PostgreSQL in production)
users_db = {}
security = HTTPBearer()
# ==================== MODELS ====================
class UserRegister(BaseModel):
name: str
email: EmailStr
password: str
role: str = "student"
class UserLogin(BaseModel):
email: EmailStr
password: str
class UserResponse(BaseModel):
id: str
name: str
email: str
role: str
class AuthResponse(BaseModel):
token: str
user: UserResponse
class ChatMessage(BaseModel):
role: str
content: str
class ChatRequest(BaseModel):
messages: List[ChatMessage]
user_id: Optional[str] = None
context: Optional[str] = None
class ChatResponse(BaseModel):
response: str
agent_used: str
class CodeExecuteRequest(BaseModel):
code: str
user_id: Optional[str] = None
class CodeExecuteResponse(BaseModel):
output: str
error: Optional[str] = None
execution_time_ms: int
class ExplainRequest(BaseModel):
topic: str
level: str = "intermediate"
# ==================== AUTH HELPERS ====================
def hash_password(password: str) -> str:
return hashlib.sha256(password.encode()).hexdigest()
def create_token(user_id: str, email: str, role: str) -> str:
expire = datetime.utcnow() + timedelta(hours=ACCESS_TOKEN_EXPIRE_HOURS)
payload = {
"sub": user_id,
"email": email,
"role": role,
"exp": expire
}
return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
try:
payload = jwt.decode(credentials.credentials, SECRET_KEY, algorithms=[ALGORITHM])
return payload
except jwt.ExpiredSignatureError:
raise HTTPException(status_code=401, detail="Token expired")
except jwt.InvalidTokenError:
raise HTTPException(status_code=401, detail="Invalid token")
# ==================== AUTH ENDPOINTS ====================
@app.post("/auth/register", response_model=AuthResponse)
async def register(data: UserRegister):
if data.email in users_db:
raise HTTPException(status_code=400, detail="Email already registered")
user_id = hashlib.md5(data.email.encode()).hexdigest()[:12]
hashed_password = hash_password(data.password)
users_db[data.email] = {
"id": user_id,
"name": data.name,
"email": data.email,
"password": hashed_password,
"role": data.role,
"created_at": datetime.utcnow().isoformat()
}
token = create_token(user_id, data.email, data.role)
return AuthResponse(
token=token,
user=UserResponse(
id=user_id,
name=data.name,
email=data.email,
role=data.role
)
)
@app.post("/auth/login", response_model=AuthResponse)
async def login(data: UserLogin):
user = users_db.get(data.email)
if not user or user["password"] != hash_password(data.password):
raise HTTPException(status_code=401, detail="Invalid email or password")
token = create_token(user["id"], user["email"], user["role"])
return AuthResponse(
token=token,
user=UserResponse(
id=user["id"],
name=user["name"],
email=user["email"],
role=user["role"]
)
)
@app.get("/auth/me", response_model=UserResponse)
async def get_current_user(payload: dict = Depends(verify_token)):
user = users_db.get(payload["email"])
if not user:
raise HTTPException(status_code=404, detail="User not found")
return UserResponse(
id=user["id"],
name=user["name"],
email=user["email"],
role=user["role"]
)
# ==================== AI CHAT ENDPOINT ====================
@app.post("/chat", response_model=ChatResponse)
async def chat_with_ai(data: ChatRequest, payload: dict = Depends(verify_token)):
"""
Chat with AI tutor using OpenRouter API
"""
if not OPENROUTER_API_KEY:
# Fallback to simulated response if no API key
return ChatResponse(
response=get_simulated_response(data.messages[-1].content if data.messages else ""),
agent_used="simulated"
)
try:
async with httpx.AsyncClient() as client:
# Build system prompt for Python tutor
system_prompt = """You are a friendly and knowledgeable Python programming tutor.
Your goal is to help students learn Python effectively.
- Explain concepts clearly with examples
- When students share code, provide constructive feedback
- If they have errors, help them understand why and how to fix them
- Encourage good coding practices
- Keep responses concise but informative
- Use code blocks for code examples"""
messages = [{"role": "system", "content": system_prompt}]
for msg in data.messages:
messages.append({"role": msg.role, "content": msg.content})
response = await client.post(
f"{OPENROUTER_BASE_URL}/chat/completions",
headers={
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json",
"HTTP-Referer": "http://localhost:3000",
"X-Title": "LearnFlow Python Tutor"
},
json={
"model": LLM_MODEL,
"messages": messages,
"max_tokens": 1000,
"temperature": 0.7
},
timeout=30.0
)
if response.status_code == 200:
result = response.json()
ai_response = result["choices"][0]["message"]["content"]
return ChatResponse(
response=ai_response,
agent_used="openrouter"
)
else:
# Fallback to simulated
return ChatResponse(
response=get_simulated_response(data.messages[-1].content if data.messages else ""),
agent_used="simulated-fallback"
)
except Exception as e:
return ChatResponse(
response=get_simulated_response(data.messages[-1].content if data.messages else ""),
agent_used="simulated-error"
)
def get_simulated_response(query: str) -> str:
"""Simulated AI response when API is unavailable"""
query_lower = query.lower()
if "for loop" in query_lower or "loop" in query_lower:
return """**For Loops in Python**
For loops iterate over sequences (lists, strings, ranges, etc.):
```python
# Loop through a list
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
# Loop with range
for i in range(5):
print(i) # Prints 0, 1, 2, 3, 4
# Loop with enumerate
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
```
Key points:
- `range(n)` generates numbers from 0 to n-1
- Use `enumerate()` when you need both index and value
- `break` exits the loop, `continue` skips to next iteration"""
elif "function" in query_lower or "def" in query_lower:
return """**Functions in Python**
Functions are reusable blocks of code:
```python
# Basic function
def greet(name):
return f"Hello, {name}!"
# Function with default parameter
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
# Function with multiple return values
def get_stats(numbers):
return min(numbers), max(numbers), sum(numbers)/len(numbers)
minimum, maximum, average = get_stats([1, 2, 3, 4, 5])
```
Key points:
- Use `def` keyword to define functions
- Parameters can have default values
- Use `return` to send back values
- Functions can return multiple values as tuples"""
elif "list" in query_lower:
return """**Lists in Python**
Lists are ordered, mutable collections:
```python
# Create a list
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]
# Access elements
first = numbers[0] # 1
last = numbers[-1] # 5
# Modify lists
numbers.append(6) # Add to end
numbers.insert(0, 0) # Insert at index
numbers.remove(3) # Remove value
popped = numbers.pop() # Remove and return last
# List comprehension
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
```
Key points:
- Lists are zero-indexed
- Use negative indices to access from the end
- List comprehensions are concise ways to create lists"""
elif "error" in query_lower or "debug" in query_lower:
return """**Debugging Python Errors**
Common errors and fixes:
1. **SyntaxError**: Check for missing colons, parentheses, or quotes
2. **NameError**: Variable not defined - check spelling
3. **TypeError**: Wrong type - check your data types
4. **IndexError**: List index out of range - check list length
5. **KeyError**: Dictionary key not found - use `.get()` method
```python
# Use try-except for error handling
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
# Debug with print statements
print(f"Variable value: {my_var}")
# Use type() to check types
print(type(my_var))
```
Share your error message and I'll help you fix it!"""
else:
return """I'm your Python tutor! I can help you with:
- **Concepts**: Variables, loops, functions, classes, etc.
- **Code Review**: Share your code for feedback
- **Debugging**: Paste your error and I'll explain the fix
- **Exercises**: Practice problems to build skills
What would you like to learn about? Try asking:
- "How do for loops work?"
- "Explain functions in Python"
- "Help me fix this error: [paste error]"
- "Review my code: [paste code]" """
# ==================== CODE EXECUTION ENDPOINT ====================
@app.post("/execute", response_model=CodeExecuteResponse)
async def execute_code(data: CodeExecuteRequest, payload: dict = Depends(verify_token)):
"""
Execute Python code in a sandboxed environment
"""
import subprocess
import tempfile
import time
try:
# Create temporary file
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write(data.code)
temp_file = f.name
start_time = time.time()
# Execute with timeout
result = subprocess.run(
['python3', temp_file],
capture_output=True,
text=True,
timeout=5 # 5 second timeout
)
execution_time = int((time.time() - start_time) * 1000)
# Cleanup
os.unlink(temp_file)
if result.returncode == 0:
return CodeExecuteResponse(
output=result.stdout,
error=None,
execution_time_ms=execution_time
)
else:
return CodeExecuteResponse(
output=result.stdout,
error=result.stderr,
execution_time_ms=execution_time
)
except subprocess.TimeoutExpired:
os.unlink(temp_file)
return CodeExecuteResponse(
output="",
error="Execution timed out (5 second limit)",
execution_time_ms=5000
)
except Exception as e:
return CodeExecuteResponse(
output="",
error=str(e),
execution_time_ms=0
)
# ==================== CONCEPTS ENDPOINT ====================
@app.post("/explain")
async def explain_concept(data: ExplainRequest, payload: dict = Depends(verify_token)):
"""
Get explanation for a Python concept
"""
if OPENROUTER_API_KEY:
try:
async with httpx.AsyncClient() as client:
response = await client.post(
f"{OPENROUTER_BASE_URL}/chat/completions",
headers={
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json",
},
json={
"model": LLM_MODEL,
"messages": [
{"role": "system", "content": f"You are a Python tutor. Explain concepts at a {data.level} level with examples."},
{"role": "user", "content": f"Explain {data.topic} in Python"}
],
"max_tokens": 800
},
timeout=30.0
)
if response.status_code == 200:
result = response.json()
return {
"topic": data.topic,
"explanation": result["choices"][0]["message"]["content"],
"level": data.level
}
except:
pass
# Fallback
return {
"topic": data.topic,
"explanation": get_simulated_response(data.topic),
"level": data.level
}
# ==================== PROGRESS ENDPOINTS ====================
# In-memory progress store
progress_db = {}
@app.get("/progress/{user_id}")
async def get_progress(user_id: str, payload: dict = Depends(verify_token)):
"""Get user's learning progress"""
if user_id not in progress_db:
progress_db[user_id] = {
"user_id": user_id,
"overall_mastery": 0,
"modules_completed": 0,
"current_module": "Basics",
"modules": {
"Basics": {"variables": {"mastery_score": 0}, "operators": {"mastery_score": 0}},
"Control Flow": {"if_statements": {"mastery_score": 0}, "loops": {"mastery_score": 0}},
"Functions": {"basic": {"mastery_score": 0}, "advanced": {"mastery_score": 0}},
},
"quiz_scores": [],
"total_time_spent": 0
}
return progress_db[user_id]
@app.post("/progress")
async def update_progress(data: dict, payload: dict = Depends(verify_token)):
"""Update user's learning progress"""
user_id = data.get("user_id")
module = data.get("module")
topic = data.get("topic")
score = data.get("score", 0)
if user_id not in progress_db:
await get_progress(user_id, payload)
# Update module/topic score
if module in progress_db[user_id]["modules"]:
if topic in progress_db[user_id]["modules"][module]:
old_score = progress_db[user_id]["modules"][module][topic]["mastery_score"]
# Weighted average with new score
progress_db[user_id]["modules"][module][topic]["mastery_score"] = (old_score * 0.7) + (score * 100 * 0.3)
# Recalculate overall mastery
total_score = 0
count = 0
for mod in progress_db[user_id]["modules"].values():
for top in mod.values():
total_score += top["mastery_score"]
count += 1
progress_db[user_id]["overall_mastery"] = total_score / count if count > 0 else 0
return progress_db[user_id]
# ==================== HEALTH CHECK ====================
@app.get("/health")
async def health_check():
return {
"status": "healthy",
"service": "api-gateway",
"timestamp": datetime.utcnow().isoformat()
}
@app.get("/")
async def root():
return {
"message": "Welcome to LearnFlow API Gateway",
"docs": "/docs",
"health": "/health"
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
|