File size: 8,063 Bytes
325b94c | 1 2 3 4 5 6 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | from crewai import Agent, Task, Crew, Process, LLM
import os
import json
from modules import llm
from schemas import CurriculumOutline, DeepSemanticTopicsOutput
# ---------------------------
# === Agent 1: Deep Semantic Topics Generator (Full Coverage + Metadata Adaptive) ===
# ---------------------------
def research_agent():
model = llm()
return Agent(
role="Deep Semantic Topics Generator (Educational Research Agent)",
goal="\n".join(
[
"Generate as many interconnected and realistic Arabic topics as possible for the curriculum title: {topic}.",
"Do NOT divide topics into units — just produce a comprehensive standalone list of high-quality topics.",
"Your mission is to achieve *maximum conceptual and practical coverage* of the topic: cover every visible and implicit meaning embedded in the title: {topic}.",
"Interpret the topic deeply and explore all related sub-concepts, tools, methods, applications, personal skills, frameworks, real-world examples, and future trends.",
"No aspect of the topic should remain uncovered — ensure total thematic coverage from theory to practice.",
# === Adaptation & Metadata awareness ===
"Always adapt to metadata: domain ({domain}), content_type ({content_type}), audience ({audience}), material_type ({material_type}).",
"Adjust tone, complexity, and depth according to the audience and domain type.",
"Apply DNA methodology explicitly in topic design:",
"- material_type:",
"-- Conceptual: theories, principles, abstract knowledge.",
"-- Procedural: step-by-step instructions or methods.",
"-- Structural: frameworks, models, or system views.",
"-- Personal: learner reflection, skills, or self-development aspects.",
"-- Realistic: real-world examples, applications, or case studies.",
"- content_type:",
"-- Educational → for structured learning (e.g., lessons, modules).",
"-- Awareness → for informative or awareness-raising purposes (e.g., campaigns, workshops).",
"-- Training → for hands-on skill development (e.g., exercises, applied learning).",
# === Learning progression ===
"Ensure logical educational progression throughout the topics:",
" 1️⃣ Start with conceptual understanding (definitions, foundations, importance).",
" 2️⃣ Then practical tools and applications (methods, workflows, tools).",
" 3️⃣ Then analysis and real-world case studies (evaluation, lessons learned).",
" 4️⃣ End with strategies, integration, and innovation (advanced synthesis, policies, or future directions).",
# === Title & Content validation ===
"Each topic must be realistic and searchable in Arabic or bilingual educational sources.",
"Before finalizing each topic, mentally confirm that real Arabic educational content exists online for it.",
"Reject or replace any vague, repetitive, or non-educational topics.",
"Avoid vague or redundant points — every topic must be unique and non-overlapping.",
# === Arabic educational title standards ===
"Follow Arabic educational title standards strictly:",
" - Clear, concise, and direct phrasing (no metaphors or poetic language).",
" - Titles should be between 4–7 words long.",
" - Represent a clear learning focus, suitable for the target audience ({audience}).",
" - Example good: مهارات التفكير النقدي واتخاذ القرار",
" - Example bad: رحلة العقل نحو التفكير النقدي وصناعة القرار",
# === Output schema ===
"For each topic, provide the following JSON fields exactly:",
" - title: Arabic, concise and direct",
" - description: 1–2 lines Arabic summarizing the topic",
" - dna: one of [Conceptual, Procedural, Structural, Personal, Realistic] and [Educational, Awareness, Training]",
" - suggested_unit: integer (1–4) showing depth progression (1=basic → 4=advanced)",
" - search_terms: 3–6 Arabic keywords relevant to the topic",
" - source: optional Arabic or bilingual educational source name/title (if known)",
# === Output constraints ===
"Output must be a valid JSON list (no commentary, no explanations).",
"List must be ordered by suggested_unit (1 → 4) to reflect gradual learning progression.",
]
),
backstory="\n".join(
[
"You are a senior educational researcher and curriculum architect with deep expertise in Arabic educational design and semantic topic analysis.",
"You deeply interpret the input topic — uncovering both its explicit and implicit meanings to ensure total thematic coverage.",
"You create coherent, realistic, and pedagogically structured topic sets that form a logical educational progression.",
"Each topic you design is grounded in actual Arabic or bilingual educational resources, ensuring that every subject can be realistically taught or studied.",
"You think step by step before finalizing outputs — validating realism, logical sequence, and online content availability.",
"Your work directly feeds the downstream Outline Agent, so clarity, precision, and educational completeness are essential in every topic you produce.",
]
),
allow_delegation=False,
llm=model,
reasoning=True,
verbose=True,
max_reasoning_attempts=4,
max_iter=100,
)
# ---------------------------
# === Task 1: Generate Deep & Comprehensive Topics ===
# ---------------------------
def research_task(research_agent):
return Task(
description="\n".join(
[
"Generate a comprehensive, realistic, and logically structured list of Arabic topics for {topic}.",
"Ensure full semantic coverage — address both the explicit and implicit meanings of the topic title to cover all relevant dimensions and perspectives.",
"Include topics that collectively span all DNA dimensions and metadata parameters: content_type ({content_type}) and material_type ({material_type}).",
"Each topic must be unique, clear, and pedagogically meaningful — avoiding repetition, overlap, or vague expressions.",
"All topics must be realistic and supported by potential Arabic or bilingual educational resources available online.",
"Ensure educational progression: begin with conceptual foundations and definitions, then move through methods and tools, practical applications, case studies, and finally strategic or innovative insights.",
"Maintain alignment with Arabic educational title standards: concise, direct, single-part titles with clear learning focus.",
"Output must be a valid JSON list following the schema below — no explanations, introductions, or extra commentary.",
]
),
expected_output=(
"A valid JSON list ordered by suggested_unit ascending:\n"
"[\n"
" {\n"
' "title": "string (Arabic)",\n'
' "description": "string (1–2 lines in Arabic)",\n'
' "dna": "Conceptual|Procedural|Structural|Personal|Realistic",\n'
' "suggested_unit": 1|2|3|4,\n'
' "search_terms": ["string (Arabic keyword)", ...],\n'
' "source": "string (optional)"\n'
" }, ...\n"
"]\n"
"The output must be valid JSON, without markdown formatting or code blocks."
),
params={"min_topics": 10, "max_topics": 30}, # expand to ensure deeper coverage
output_json=DeepSemanticTopicsOutput,
# output_file=os.path.join(output_dir, "deep_semantic_topics.json"),
agent=research_agent,
)
|