Spaces:
Sleeping
Sleeping
update quizgenerator
Browse files- app/services/quiz_generator.py +85 -106
app/services/quiz_generator.py
CHANGED
|
@@ -1,107 +1,86 @@
|
|
| 1 |
-
from typing import List,
|
| 2 |
-
import json
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
cleaned_response = self.clean_json_response(response_text)
|
| 87 |
-
|
| 88 |
-
try:
|
| 89 |
-
response_json = json.loads(cleaned_response)
|
| 90 |
-
except json.JSONDecodeError as e:
|
| 91 |
-
logger.error(f"Réponse brute: {response_text}")
|
| 92 |
-
logger.error(f"Réponse nettoyée: {cleaned_response}")
|
| 93 |
-
raise Exception(f"Erreur de parsing JSON: {str(e)}")
|
| 94 |
-
|
| 95 |
-
questions = []
|
| 96 |
-
for q in response_json["questions"]:
|
| 97 |
-
questions.append(Question(
|
| 98 |
-
question=q["question"],
|
| 99 |
-
options=q["options"],
|
| 100 |
-
correct_answer=q["correct_answer"]
|
| 101 |
-
))
|
| 102 |
-
|
| 103 |
-
return questions
|
| 104 |
-
|
| 105 |
-
except Exception as e:
|
| 106 |
-
print(f"Erreur dans generate_quiz: {str(e)}")
|
| 107 |
raise e
|
|
|
|
| 1 |
+
from typing import List, Literal
|
| 2 |
+
import json
|
| 3 |
+
from langchain_openai import ChatOpenAI
|
| 4 |
+
from langchain.prompts import ChatPromptTemplate
|
| 5 |
+
from app.core.config import settings
|
| 6 |
+
from app.models.quiz import Question
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
class QuizGenerator:
|
| 12 |
+
def __init__(self, provider: Literal["openai", "deepseek"] = "openai"):
|
| 13 |
+
# Configuration selon le provider
|
| 14 |
+
if provider == "deepseek":
|
| 15 |
+
self.llm = ChatOpenAI(
|
| 16 |
+
model_name="deepseek-chat",
|
| 17 |
+
temperature=0.7,
|
| 18 |
+
openai_api_key=settings.DEEPSEEK_API_KEY,
|
| 19 |
+
base_url="https://api.deepseek.com"
|
| 20 |
+
)
|
| 21 |
+
else: # openai
|
| 22 |
+
self.llm = ChatOpenAI(
|
| 23 |
+
model_name=settings.MODEL_NAME,
|
| 24 |
+
temperature=0.7,
|
| 25 |
+
openai_api_key=settings.OPENAI_API_KEY
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
def clean_json_response(self, response_text: str) -> str:
|
| 29 |
+
"""Nettoie la réponse du LLM pour obtenir un JSON valide."""
|
| 30 |
+
cleaned = response_text.replace('```json', '').replace('```', '').strip()
|
| 31 |
+
return cleaned.strip()
|
| 32 |
+
|
| 33 |
+
async def generate_quiz(self, theme: str, num_questions: int = 5) -> List[Question]:
|
| 34 |
+
logger.debug(f"Génération de quiz - Thème: {theme}, Nb questions: {num_questions}")
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
# Template pour la génération de questions
|
| 38 |
+
prompt = ChatPromptTemplate.from_template("""
|
| 39 |
+
Tu es un générateur de quiz intelligent.
|
| 40 |
+
Génère {num_questions} questions de quiz sur le thème: {theme}
|
| 41 |
+
|
| 42 |
+
IMPORTANT: Réponds UNIQUEMENT avec un JSON valide sans backticks ni formatage markdown.
|
| 43 |
+
Format exact attendu:
|
| 44 |
+
{{
|
| 45 |
+
"questions": [
|
| 46 |
+
{{
|
| 47 |
+
"question": "La question",
|
| 48 |
+
"options": ["Option A", "Option B", "Option C"],
|
| 49 |
+
"correct_answer": "La bonne réponse (qui doit être une des options)"
|
| 50 |
+
}}
|
| 51 |
+
]
|
| 52 |
+
}}
|
| 53 |
+
""")
|
| 54 |
+
|
| 55 |
+
# Générer les questions
|
| 56 |
+
response = await self.llm.agenerate([
|
| 57 |
+
prompt.format_messages(
|
| 58 |
+
theme=theme,
|
| 59 |
+
num_questions=num_questions
|
| 60 |
+
)
|
| 61 |
+
])
|
| 62 |
+
|
| 63 |
+
# Parser le JSON et créer les objets Question
|
| 64 |
+
response_text = response.generations[0][0].text
|
| 65 |
+
cleaned_response = self.clean_json_response(response_text)
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
response_json = json.loads(cleaned_response)
|
| 69 |
+
except json.JSONDecodeError as e:
|
| 70 |
+
logger.error(f"Réponse brute: {response_text}")
|
| 71 |
+
logger.error(f"Réponse nettoyée: {cleaned_response}")
|
| 72 |
+
raise Exception(f"Erreur de parsing JSON: {str(e)}")
|
| 73 |
+
|
| 74 |
+
questions = []
|
| 75 |
+
for q in response_json["questions"]:
|
| 76 |
+
questions.append(Question(
|
| 77 |
+
question=q["question"],
|
| 78 |
+
options=q["options"],
|
| 79 |
+
correct_answer=q["correct_answer"]
|
| 80 |
+
))
|
| 81 |
+
|
| 82 |
+
return questions
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Erreur dans generate_quiz: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
raise e
|