You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.0 KiB
132 lines
4.0 KiB
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)}")
|
|
|