51df045220
- TestForm: смысловые блоки «Метаинформация» / «Версии теста» / «Содержание» / команды - AI-генерация: мини-форма из 3 полей (тема, число вопросов, число вариантов) - Кнопка «Проверить тест» переехала в нижнюю панель команд - Backend: GenerateRequest принимает answers_count, передаётся в промпт - Убрано упоминание API-ключа в fallback-сообщении формы Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
133 lines
4.0 KiB
Python
133 lines
4.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database import get_db
|
|
from app.services import llm as llm_service
|
|
|
|
router = APIRouter(tags=["llm"])
|
|
|
|
|
|
class CheckResponse(BaseModel):
|
|
ok: bool
|
|
message: str
|
|
|
|
|
|
class GenerateRequest(BaseModel):
|
|
topic: str
|
|
count: int = 7
|
|
answers_count: int = 3
|
|
|
|
|
|
class GenerateResponse(BaseModel):
|
|
questions: list[dict]
|
|
|
|
|
|
class ImproveRequest(BaseModel):
|
|
question: str
|
|
answers: list[str]
|
|
|
|
|
|
class ImproveResponse(BaseModel):
|
|
improved_question: str
|
|
improved_answers: list[str]
|
|
|
|
|
|
class DistractorsRequest(BaseModel):
|
|
question: str
|
|
answers: list[str]
|
|
|
|
|
|
class DistractorsResponse(BaseModel):
|
|
distractors: list[str]
|
|
|
|
|
|
class ReviewRequest(BaseModel):
|
|
title: str
|
|
questions: list[dict]
|
|
|
|
|
|
class ReviewResponse(BaseModel):
|
|
review: str
|
|
|
|
|
|
class ImproveAllRequest(BaseModel):
|
|
title: str
|
|
questions: list[dict]
|
|
|
|
|
|
class ImproveAllResponse(BaseModel):
|
|
questions: list[dict]
|
|
|
|
|
|
@router.post("/api/llm/check", response_model=CheckResponse)
|
|
async def check_connection(db: AsyncSession = Depends(get_db)):
|
|
try:
|
|
result = await llm_service.check_connection(db)
|
|
return {"ok": True, "message": f"Подключение успешно: {result}"}
|
|
except ValueError as e:
|
|
return {"ok": False, "message": str(e)}
|
|
except Exception as e:
|
|
return {"ok": False, "message": f"Ошибка подключения: {str(e)}"}
|
|
|
|
|
|
@router.post("/api/llm/generate", response_model=GenerateResponse)
|
|
async def generate_questions(req: GenerateRequest, db: AsyncSession = Depends(get_db)):
|
|
try:
|
|
questions = await llm_service.generate_questions(
|
|
db, req.topic, req.count, req.answers_count
|
|
)
|
|
return {"questions": questions}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Ошибка AI: {str(e)}")
|
|
|
|
|
|
@router.post("/api/llm/improve", response_model=ImproveResponse)
|
|
async def improve_question(req: ImproveRequest, db: AsyncSession = Depends(get_db)):
|
|
try:
|
|
data = await llm_service.improve_question(db, req.question, req.answers)
|
|
return {"improved_question": data["question"], "improved_answers": data["answers"]}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Ошибка AI: {str(e)}")
|
|
|
|
|
|
@router.post("/api/llm/distractors", response_model=DistractorsResponse)
|
|
async def generate_distractors(
|
|
req: DistractorsRequest, db: AsyncSession = Depends(get_db)
|
|
):
|
|
try:
|
|
distractors = await llm_service.generate_distractors(
|
|
db, req.question, req.answers
|
|
)
|
|
return {"distractors": distractors}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Ошибка AI: {str(e)}")
|
|
|
|
|
|
@router.post("/api/llm/review", response_model=ReviewResponse)
|
|
async def review_test(req: ReviewRequest, db: AsyncSession = Depends(get_db)):
|
|
try:
|
|
review = await llm_service.review_test(db, req.title, req.questions)
|
|
return {"review": review}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Ошибка AI: {str(e)}")
|
|
|
|
|
|
@router.post("/api/llm/improve_all", response_model=ImproveAllResponse)
|
|
async def improve_all(req: ImproveAllRequest, db: AsyncSession = Depends(get_db)):
|
|
try:
|
|
questions = await llm_service.improve_all(db, req.title, req.questions)
|
|
return {"questions": questions}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Ошибка AI: {str(e)}")
|