elkay: api.py for qwen
Browse files- utils/api.py +8 -4
utils/api.py
CHANGED
|
@@ -380,15 +380,19 @@ def generate_quiz(*, lesson_id: int | None, level_slug: str | None, lesson_title
|
|
| 380 |
try:
|
| 381 |
resp = _req("POST", "/quiz/auto", json=payload)
|
| 382 |
logger.debug(f"Raw response from /quiz/auto: {resp.text}")
|
|
|
|
| 383 |
result = _json_or_raise(resp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
except RuntimeError as e:
|
| 385 |
logger.error(f"Quiz generation failed: {str(e)}")
|
| 386 |
return [] # Return empty list to prevent app crash
|
| 387 |
|
| 388 |
-
if isinstance(result, dict):
|
| 389 |
-
return result.get("items") or result.get("quiz") or []
|
| 390 |
-
return result if isinstance(result, list) else []
|
| 391 |
-
|
| 392 |
# ---- Legacy agent endpoints (keep) ----
|
| 393 |
def start_agent(student_id: int, lesson_id: int, level_slug: str):
|
| 394 |
return _json_or_raise(_req("POST", "/agent/start",
|
|
|
|
| 380 |
try:
|
| 381 |
resp = _req("POST", "/quiz/auto", json=payload)
|
| 382 |
logger.debug(f"Raw response from /quiz/auto: {resp.text}")
|
| 383 |
+
logger.debug(f"Response status code: {resp.status_code}")
|
| 384 |
result = _json_or_raise(resp)
|
| 385 |
+
# Ensure result is a list of quiz items, handling Qwen's potential JSON structure
|
| 386 |
+
if isinstance(result, dict) and "items" in result:
|
| 387 |
+
return result["items"]
|
| 388 |
+
return result if isinstance(result, list) else []
|
| 389 |
+
except requests.HTTPError as e:
|
| 390 |
+
logger.error(f"HTTP Error in quiz generation: {e.response.status_code} - {e.response.text}")
|
| 391 |
+
return []
|
| 392 |
except RuntimeError as e:
|
| 393 |
logger.error(f"Quiz generation failed: {str(e)}")
|
| 394 |
return [] # Return empty list to prevent app crash
|
| 395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
# ---- Legacy agent endpoints (keep) ----
|
| 397 |
def start_agent(student_id: int, lesson_id: int, level_slug: str):
|
| 398 |
return _json_or_raise(_req("POST", "/agent/start",
|