- Backend: FastAPI + Python-SocketIO + SQLAlchemy - Models for categories, questions, game sessions, events - AI services for answer validation and question generation (Claude) - Room management with Redis - Game logic with stealing mechanics - Admin API for question management - Frontend: React + Vite + TypeScript + Tailwind - 5 visual themes (DRRR, Retro, Minimal, RGB, Anime 90s) - Real-time game with Socket.IO - Achievement system - Replay functionality - Sound effects per theme - Docker Compose for deployment - Design documentation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
import json
|
|
from anthropic import Anthropic
|
|
from app.config import get_settings
|
|
|
|
settings = get_settings()
|
|
|
|
|
|
class AIValidator:
|
|
def __init__(self):
|
|
self.client = Anthropic(api_key=settings.anthropic_api_key)
|
|
|
|
async def validate_answer(
|
|
self,
|
|
question: str,
|
|
correct_answer: str,
|
|
alt_answers: list[str],
|
|
player_answer: str
|
|
) -> dict:
|
|
"""
|
|
Validate if the player's answer is correct using Claude AI.
|
|
|
|
Returns:
|
|
dict: {"valid": bool, "reason": str}
|
|
"""
|
|
prompt = f"""Eres un validador de trivia. Determina si la respuesta del jugador
|
|
es correcta comparándola con la respuesta oficial.
|
|
|
|
Pregunta: {question}
|
|
Respuesta correcta: {correct_answer}
|
|
Respuestas alternativas válidas: {', '.join(alt_answers) if alt_answers else 'Ninguna'}
|
|
Respuesta del jugador: {player_answer}
|
|
|
|
Considera válido si:
|
|
- Es sinónimo o variación de la respuesta correcta
|
|
- Tiene errores menores de ortografía
|
|
- Usa abreviaciones comunes (ej: "BOTW" = "Breath of the Wild")
|
|
- Es conceptualmente equivalente
|
|
|
|
Responde SOLO con JSON: {{"valid": true/false, "reason": "breve explicación"}}"""
|
|
|
|
try:
|
|
message = self.client.messages.create(
|
|
model="claude-3-haiku-20240307",
|
|
max_tokens=150,
|
|
messages=[
|
|
{"role": "user", "content": prompt}
|
|
]
|
|
)
|
|
|
|
response_text = message.content[0].text.strip()
|
|
|
|
# Parse JSON response
|
|
result = json.loads(response_text)
|
|
return result
|
|
|
|
except json.JSONDecodeError:
|
|
# If JSON parsing fails, try to extract the result
|
|
response_lower = response_text.lower()
|
|
if "true" in response_lower:
|
|
return {"valid": True, "reason": "Respuesta validada por IA"}
|
|
return {"valid": False, "reason": "No se pudo validar la respuesta"}
|
|
|
|
except Exception as e:
|
|
print(f"Error validating answer: {e}")
|
|
# Fallback to exact match
|
|
player_lower = player_answer.lower().strip()
|
|
correct_lower = correct_answer.lower().strip()
|
|
|
|
if player_lower == correct_lower:
|
|
return {"valid": True, "reason": "Coincidencia exacta"}
|
|
|
|
for alt in alt_answers:
|
|
if player_lower == alt.lower().strip():
|
|
return {"valid": True, "reason": "Coincide con respuesta alternativa"}
|
|
|
|
return {"valid": False, "reason": "Respuesta incorrecta"}
|
|
|
|
|
|
# Singleton instance
|
|
ai_validator = AIValidator()
|