"""Re-generate AI trio (knowledge_reminder, ai_hint, solution) in English for existing questions.""" import json import asyncio from app.services.supabase_client import get_supabase from app.services.llm_clients import get_qwen_client from app.services.paper_processor import ANALYSIS_PROMPT async def regenerate_for_paper(paper_id: str): sb = get_supabase() qwen = get_qwen_client() questions = sb.table("paper_questions").select("*").eq("paper_id", paper_id).order("display_order").execute().data print(f"Found {len(questions)} questions for paper {paper_id[:8]}") for q in questions: qnum = q["question_number"] print(f" Regenerating Q{qnum}...", end=" ", flush=True) answer_section = "" if q.get("raw_answer_text"): answer_section = f"- Reference answer: {q['raw_answer_text']}" elif q.get("correct_option"): answer_section = f"- Correct option: {q['correct_option']}" elif q.get("correct_answer"): answer_section = f"- Correct answer: {q['correct_answer']}" resp = qwen.chat.completions.create( model="qwen-plus", messages=[ {"role": "system", "content": ANALYSIS_PROMPT.format( question_number=qnum, question_type=q["question_type"], score=q.get("score", "unknown"), question_text=q["question_text"], topics=", ".join(q.get("topics", [])), answer_section=answer_section, )}, ], temperature=0.3, response_format={"type": "json_object"}, ) analysis = json.loads(resp.choices[0].message.content) sb.table("paper_questions").update({ "knowledge_reminder": analysis.get("knowledge_reminder", ""), "ai_hint": analysis.get("ai_hint", ""), "solution": analysis.get("solution", ""), }).eq("id", q["id"]).execute() print("done") print(f"All questions regenerated for paper {paper_id[:8]}") async def main(): sb = get_supabase() papers = sb.table("papers").select("id,course_code,year,term").eq("status", "ready").order("created_at", desc=True).execute().data for p in papers: print(f"\n=== {p['course_code']} {p['year']} {p['term']} ===") await regenerate_for_paper(p["id"]) print("\nAll done!") if __name__ == "__main__": asyncio.run(main())