Files
vuongps38770 7c41ddaa82 init
2026-01-13 09:33:10 +07:00

170 lines
6.0 KiB
Python

"""
games/match.py - Match Game - Match words/phrases with images
type_id = 3
Input: Danh sách từ hoặc cụm từ
Output: Mỗi item gồm từ/cụm từ và mô tả hình ảnh tương ứng
"""
from typing import List, Literal
from pydantic import BaseModel, Field
from langchain_core.output_parsers import PydanticOutputParser
# ============== SCHEMA ==============
class MatchItem(BaseModel):
"""Schema cho 1 item của Match game"""
word: str = Field(
description="The word or phrase to be matched (EXACT copy from source, cleaned of numbering)"
)
original_quote: str = Field(
description="EXACT quote from source text before any cleaning"
)
image_description: str = Field(
description="Detailed visual description for image generation in ENGLISH. Must be specific and visual."
)
image_keywords: List[str] = Field(
default=[], description="2-3 English keywords for image search"
)
image_is_complex: bool = Field(
default=False,
description="True if image needs precise quantities, humans, or multiple detailed objects",
)
class MatchMetadata(BaseModel):
"""Metadata đánh giá nội dung"""
title: str = Field(description="Title from source or short descriptive title")
description: str = Field(description="One sentence summary of the content")
grade: int = Field(
description="Estimated grade level 1-5 (1=easy/young, 5=advanced)"
)
type: Literal["match"] = Field(default="match", description="Game type")
difficulty: int = Field(description="Difficulty 1-5 for that grade")
class MatchOutput(BaseModel):
"""Output wrapper for match items"""
items: List[MatchItem] = Field(
description="List of match items generated from source text"
)
metadata: MatchMetadata = Field(description="Metadata about the content")
# Output parser
output_parser = PydanticOutputParser(pydantic_object=MatchOutput)
# ============== CONFIG ==============
GAME_CONFIG = {
# === REQUIRED ===
"game_type": "match",
"type_id": 3,
"display_name": "Match with Image",
"description": "Match words or phrases with their corresponding images",
"schema": MatchItem,
"output_schema": MatchOutput,
"output_parser": output_parser,
# === OPTIONAL ===
"active": True,
"max_items": 10,
# Input validation rules
"input_format_rules": [
"Text MUST be a list of words or phrases separated by commas, semicolons, or newlines",
"NOT suitable for long sentences or paragraphs",
"Each item should be a concrete noun/concept that can be visualized",
],
# Analyzer rules - khi nào nên chọn game này
"analyzer_rules": [
"Text is a list of words or short phrases",
"Words represent concrete objects/concepts that can be visualized",
"Examples: 'apple, banana, orange' or 'cat; dog; bird'",
"NOT suitable for abstract concepts or long sentences",
],
# Generation rules - cách tạo nội dung
"generation_rules": [
"KEEP ORIGINAL LANGUAGE for 'word' field - Do NOT translate",
"original_quote = EXACT copy from source before cleaning",
"Clean numbering like '1.', 'a)', '' from word field",
"Each word/phrase should represent a visualizable concept",
# Image rules
"image_description: MUST be DETAILED visual description in ENGLISH",
"image_description: Describe colors, shapes, actions, context",
"image_keywords: 2-3 English keywords for search",
"image_is_complex: TRUE for humans, precise counts, complex scenes",
"NEVER leave image_description empty!",
# Quality rules
"Each image should be visually DISTINCT from others",
"Avoid generic descriptions - be specific",
],
"examples": [], # Defined below
}
# ============== EXAMPLES ==============
EXAMPLES = [
{
"input": "apple; banana;",
"output": {
"items": [
{
"word": "apple",
"original_quote": "apple",
"image_description": "A shiny red apple with a green leaf on top",
"image_keywords": ["apple", "fruit", "red"],
"image_is_complex": False,
},
{
"word": "banana",
"original_quote": "banana",
"image_description": "A curved yellow banana",
"image_keywords": ["banana", "fruit", "yellow"],
"image_is_complex": False,
},
],
"metadata": {
"title": "Fruits",
"description": "Common fruits vocabulary",
"grade": 1,
"type": "match",
"difficulty": 1,
},
},
"why_suitable": "Simple words representing concrete objects that can be visualized",
},
{
"input": "1. elephant\n2. giraffe\n",
"output": {
"items": [
{
"word": "elephant",
"original_quote": "1. elephant",
"image_description": "A large grey elephant with big ears and long trunk",
"image_keywords": ["elephant", "animal", "africa"],
"image_is_complex": False,
},
{
"word": "giraffe",
"original_quote": "2. giraffe",
"image_description": "A tall giraffe with brown spots and long neck",
"image_keywords": ["giraffe", "tall", "spots"],
"image_is_complex": False,
},
],
"metadata": {
"title": "African Animals",
"description": "Safari animals vocabulary",
"grade": 2,
"type": "match",
"difficulty": 1,
},
},
"why_suitable": "Numbered list of animals - numbering will be cleaned",
},
]
GAME_CONFIG["examples"] = EXAMPLES