9a0b3ba92c
- Страница /settings: ввод и проверка API ключа DeepSeek - POST /api/llm/generate — генерация вопросов по названию теста - POST /api/llm/improve — улучшение формулировки вопроса + ответов (модал с галочками) - POST /api/llm/distractors — генерация дистракторов - POST /api/llm/review — рецензия теста + кнопка «Предложить вариант» - POST /api/llm/improve_all — улучшение всего теста с постатейным сравнением - Миграция 004: таблица settings (key-value) - Шапка приложения с навигацией на /settings Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
130 lines
3.9 KiB
Python
130 lines
3.9 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
|
|
|
|
|
|
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)
|
|
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)}")
|