Просмотр исходного кода

feat: 注册文章答题出题路由与提示词

Sisyphus 1 месяц назад
Родитель
Сommit
47cbf61422

+ 24 - 0
cfc-langgraph/app/api/article_quiz.py

@@ -0,0 +1,24 @@
+from fastapi import APIRouter
+from app.models.article_quiz import ArticleQuizRequest, ArticleQuizResponse
+from app.graphs.article_quiz_graph import ArticleQuizAgent
+import logging
+
+logger = logging.getLogger(__name__)
+router = APIRouter(prefix="/api/v1", tags=["article_quiz"])
+
+_agent = None
+
+
+def get_agent() -> ArticleQuizAgent:
+    global _agent
+    if _agent is None:
+        _agent = ArticleQuizAgent()
+    return _agent
+
+
+@router.post("/article/quiz/generate", response_model=ArticleQuizResponse)
+async def generate_article_quiz(req: ArticleQuizRequest):
+    agent = get_agent()
+    result = await agent.run(req.article_title, req.article_content, req.profile_summary)
+    return ArticleQuizResponse(questions=result.get("questions", []),
+                               fallback_used=result.get("fallback_used", True))

+ 2 - 1
cfc-langgraph/app/main.py

@@ -3,7 +3,7 @@ import time
 import asyncio
 import logging
 from fastapi import FastAPI, Request
-from app.api import health, recommend, chat, analyze, tongue, adapter, report_parse, meal, logs, audio, innate_portrait, self_check, knowledge_base, portrait
+from app.api import health, recommend, chat, analyze, tongue, adapter, report_parse, meal, logs, audio, innate_portrait, self_check, knowledge_base, portrait, article_quiz
 from app import monitoring
 from src.app import router as questionnaire_router
 from app.middleware import RequestLogMiddleware
@@ -32,6 +32,7 @@ app.include_router(self_check.router)
 app.include_router(self_check.router)
 app.include_router(knowledge_base.router)
 app.include_router(portrait.router)
+app.include_router(article_quiz.router)
 
 
 @app.on_event("startup")

+ 13 - 0
cfc-langgraph/app/models/article_quiz.py

@@ -0,0 +1,13 @@
+from pydantic import BaseModel
+from typing import List, Dict, Any
+
+
+class ArticleQuizRequest(BaseModel):
+    article_title: str
+    article_content: str = ""
+    profile_summary: str = ""
+
+
+class ArticleQuizResponse(BaseModel):
+    questions: List[Dict[str, Any]] = []
+    fallback_used: bool = True

+ 13 - 0
cfc-langgraph/app/prompt_service.py

@@ -80,6 +80,19 @@ DEFAULT_PROMPTS: dict[str, str] = {
         }
     ]
 }
+""",
+    "article_quiz": """你是一位家庭教育出题助手。根据给定的文章内容,为读者生成 3 道单项选择题。
+
+要求:
+1. 每题 4 个选项,选项以 "A. "、"B. "、"C. "、"D. " 开头
+2. 题目考察文章核心内容的理解,适合青少年读者
+3. 每题标注所属五维(body/mind/wisdom/action/wealth),缺省为 wisdom
+4. 以 JSON 数组格式返回,不要返回其他文字,只返回 JSON 数组
+
+输出格式:
+[
+  {"question": "问题", "options": ["A. xxx", "B. xxx", "C. xxx", "D. xxx"], "answer": "A", "dimension": "wisdom", "explanation": "解析"}
+]
 """,
 }