|
""" |
|
π Simplified CourseCrafter Agent - Core Functionality Only |
|
|
|
Focuses on web search, lesson creation, flashcards, and quizzes without complex MCP dependencies. |
|
""" |
|
|
|
import json |
|
from typing import Dict, List, Any, Optional, AsyncGenerator, Callable |
|
from datetime import datetime |
|
|
|
from .llm_client import LlmClient, Message |
|
from ..types import ( |
|
GenerationOptions, ProgressUpdate, StreamChunk |
|
) |
|
from ..utils.config import config |
|
from ..utils.helpers import smart_json_loads |
|
|
|
from ..tools.web_research import research_topic |
|
from ..tools.image_generation import ( |
|
generate_educational_image, |
|
extract_image_placeholders, |
|
replace_image_placeholders |
|
) |
|
|
|
class SimpleCourseAgent: |
|
""" |
|
Main agent for course generation: |
|
- Web search for research |
|
- Lesson generation |
|
- Flashcard creation |
|
- Quiz generation |
|
- Image generation |
|
""" |
|
|
|
def __init__(self): |
|
self.llm_client = LlmClient() |
|
self.system_prompt = self._get_system_prompt() |
|
self.default_provider = config.get_default_llm_provider() |
|
print(f"π SimpleCourseAgent initialized with default provider: {self.default_provider}") |
|
|
|
def _get_system_prompt(self) -> str: |
|
"""Get the system prompt for course generation""" |
|
return """You are Course Creator AI, an expert educational content creator and course designer. Your goal is to create high-quality educational content that is comprehensive, well-structured, engaging, and tailored to the needs of lerners. Create detailed lessons, generate flashcards, quizzes, and educational images. |
|
|
|
## Your Capabilities: |
|
- Research topics thoroughly using web search and content analysis |
|
- Generate engaging, structured lesson content with clear explanations and objectives |
|
- Generate interactive flashcards for key concepts for repetition learning |
|
- Create multiple-choice quizzes to reinforce learning |
|
- Generate educational images and visual aids |
|
- Ensure content quality and educational effectiveness |
|
|
|
## Quality Standards: |
|
- Content must be accurate, well-researched, and up-to-date |
|
- Lessons should build upon each other logically |
|
- Include practical examples and real-world applications |
|
- Maintain appropriate difficulty level for target audience |
|
- Ensure content is engaging and interactive |
|
- Provide clear learning objectives and outcomes |
|
|
|
## RESPONSE FORMAT: |
|
- Always respond with valid JSON only. No markdown, no explanations, just pure JSON. |
|
- Follow the format below for each type of content: |
|
|
|
For course planning, return: |
|
{ |
|
"title": "Course title", |
|
"description": "Brief description", |
|
"learning_objectives": ["objective1", "objective2"], |
|
"lesson_titles": ["Lesson 1 title", "Lesson 2 title"], |
|
"estimated_duration": 60 |
|
} |
|
|
|
For lessons, return: |
|
{ |
|
"title": "Lesson title", |
|
"duration": 15, |
|
"objectives": ["Learn X", "Understand Y"], |
|
"content": "Detailed lesson content in markdown", |
|
"key_takeaways": ["Key point 1", "Key point 2"], |
|
"examples": ["Example 1", "Example 2"] |
|
} |
|
|
|
For flashcards, return: |
|
[ |
|
{ |
|
"question": "What is X?", |
|
"answer": "X is...", |
|
"category": "Category name" |
|
} |
|
] |
|
|
|
For quizzes, return: |
|
{ |
|
"title": "Quiz title", |
|
"instructions": "Instructions text", |
|
"questions": [ |
|
{ |
|
"question": "Question text?", |
|
"options": ["A) Option 1", "B) Option 2", "C) Option 3", "D) Option 4"], |
|
"correct_answer": "A", |
|
"explanation": "Why A is correct" |
|
} |
|
] |
|
} |
|
|
|
Always strive to create courses that are not just informative, but are easy to understand, engaging, learning experiences.""" |
|
|
|
def update_provider_config(self, provider: str, api_key: str = None, **kwargs): |
|
"""Update provider configuration and reinitialize client""" |
|
success = self.llm_client.update_provider_config(provider, api_key, **kwargs) |
|
if success: |
|
self.default_provider = provider |
|
return success |
|
|
|
async def generate_course( |
|
self, |
|
topic: str, |
|
options: GenerationOptions, |
|
provider: Optional[str] = None, |
|
progress_callback: Optional[Callable[[ProgressUpdate], None]] = None |
|
) -> AsyncGenerator[StreamChunk, None]: |
|
"""Generate a complete course on the given topic""" |
|
|
|
|
|
if provider is None: |
|
provider = self.default_provider |
|
|
|
print(f"π Starting course generation for: {topic}") |
|
print(f"π Options: {options.lesson_count} lessons, {options.difficulty.value} difficulty") |
|
print(f"π§ Using LLM provider: {provider}") |
|
|
|
try: |
|
|
|
if progress_callback: |
|
progress_callback(ProgressUpdate( |
|
stage="research", |
|
progress=0.1, |
|
message="Researching topic..." |
|
)) |
|
|
|
print("π Step 1: Researching topic...") |
|
research_data = await self._research_topic(topic, provider) |
|
|
|
self._current_research = research_data |
|
yield StreamChunk(type="progress", content="β
Research completed") |
|
|
|
|
|
if progress_callback: |
|
progress_callback(ProgressUpdate( |
|
stage="planning", |
|
progress=0.3, |
|
message="Planning course structure..." |
|
)) |
|
|
|
print("π Step 2: Planning course structure...") |
|
course_plan = await self._plan_course(topic, options, provider) |
|
print(f"β
Course plan created: {course_plan.get('title', 'Unknown')}") |
|
yield StreamChunk(type="progress", content="β
Course structure planned") |
|
|
|
|
|
if progress_callback: |
|
progress_callback(ProgressUpdate( |
|
stage="lessons", |
|
progress=0.5, |
|
message="Creating lessons..." |
|
)) |
|
|
|
print("π Step 3: Generating lessons...") |
|
lessons = await self._generate_lessons(course_plan, options, provider) |
|
print(f"β
Generated {len(lessons)} lessons") |
|
yield StreamChunk(type="progress", content="β
Lessons created") |
|
|
|
|
|
if progress_callback and options.include_flashcards: |
|
progress_callback(ProgressUpdate( |
|
stage="flashcards", |
|
progress=0.7, |
|
message="Creating flashcards..." |
|
)) |
|
|
|
flashcards = [] |
|
if options.include_flashcards: |
|
print("π Step 4: Generating flashcards...") |
|
flashcards = await self._generate_flashcards(lessons, provider, options.difficulty.value) |
|
print(f"β
Generated {len(flashcards)} flashcards") |
|
yield StreamChunk(type="progress", content="β
Flashcards created") |
|
|
|
|
|
if progress_callback and options.include_quizzes: |
|
progress_callback(ProgressUpdate( |
|
stage="quiz", |
|
progress=0.8, |
|
message="Creating quiz..." |
|
)) |
|
|
|
quiz = None |
|
if options.include_quizzes: |
|
print("π Step 5: Generating quiz...") |
|
quiz = await self._generate_quiz(lessons, provider, options.difficulty.value) |
|
print(f"β
Generated quiz with {len(quiz.get('questions', []))} questions") |
|
yield StreamChunk(type="progress", content="β
Quiz created") |
|
|
|
|
|
images = [] |
|
if options.include_images: |
|
if progress_callback: |
|
progress_callback(ProgressUpdate( |
|
stage="images", |
|
progress=0.9, |
|
message="Generating images..." |
|
)) |
|
|
|
print("πΌοΈ Step 6: Generating images...") |
|
try: |
|
images = await self._generate_images(lessons) |
|
print(f"β
Generated {len(images)} images") |
|
yield StreamChunk(type="progress", content="β
Images generated") |
|
|
|
|
|
for i, lesson in enumerate(lessons): |
|
if i < len(images): |
|
lesson["images"] = [images[i]] |
|
|
|
print(f"π Attached images to {min(len(lessons), len(images))} lessons") |
|
|
|
except Exception as e: |
|
print(f"β οΈ Image generation failed: {e}") |
|
|
|
images = [] |
|
yield StreamChunk(type="progress", content="β οΈ Images skipped (generation failed)") |
|
else: |
|
print("πΌοΈ Image generation skipped (not requested)") |
|
yield StreamChunk(type="progress", content="βοΈ Images skipped") |
|
|
|
|
|
if progress_callback: |
|
progress_callback(ProgressUpdate( |
|
stage="completion", |
|
progress=1.0, |
|
message="Course generation complete!" |
|
)) |
|
|
|
print("π Assembling final course data...") |
|
yield StreamChunk(type="progress", content="β
Finalizing course") |
|
|
|
|
|
course_data = { |
|
"course_info": course_plan, |
|
"lessons": lessons, |
|
"flashcards": flashcards, |
|
"quiz": quiz, |
|
"images": images, |
|
"generated_at": datetime.now().isoformat() |
|
} |
|
|
|
print("π Course generation completed successfully!") |
|
print(f"π Final course data: {len(lessons)} lessons, {len(flashcards)} flashcards, {len(quiz.get('questions', []) if quiz else [])} quiz questions") |
|
|
|
yield StreamChunk(type="course_complete", content=json.dumps(course_data, indent=2)) |
|
|
|
except Exception as e: |
|
error_msg = f"Error generating course: {str(e)}" |
|
print(f"β {error_msg}") |
|
import traceback |
|
traceback.print_exc() |
|
yield StreamChunk(type="error", content=error_msg) |
|
|
|
async def _research_topic(self, topic: str, provider: str = None) -> Dict[str, Any]: |
|
"""Research the topic using web search and content extraction""" |
|
print(f"π Researching topic: {topic}") |
|
|
|
|
|
research_provider = provider or self.default_provider |
|
print(f"π§ Using LLM provider for research: {research_provider}") |
|
|
|
try: |
|
|
|
research_results = await research_topic(topic, llm_provider=research_provider) |
|
|
|
if research_results and research_results.get("success"): |
|
print(f"β
Web research successful: {research_results.get('successful_sources', 0)} sources") |
|
return research_results |
|
else: |
|
print(f"β οΈ Web research failed or returned no results") |
|
|
|
except Exception as e: |
|
print(f"β οΈ Web research failed: {e}") |
|
|
|
|
|
print("π Using fallback research data") |
|
return { |
|
"topic": topic, |
|
"key_concepts": [ |
|
f"Fundamental concepts of {topic}", |
|
f"Practical applications of {topic}", |
|
f"Tools and resources for {topic}", |
|
f"Best practices in {topic}", |
|
f"Common challenges in {topic}" |
|
], |
|
"sources": [ |
|
f"Educational resources for {topic}", |
|
f"Documentation and tutorials for {topic}", |
|
f"Community forums and discussions about {topic}", |
|
f"Official guides and specifications for {topic}" |
|
], |
|
"research_summary": f"Comprehensive research on {topic} covering fundamental concepts, practical applications, available tools and resources, best practices, and common challenges. This research would typically include web search results, documentation extraction, and content analysis from multiple authoritative sources.", |
|
"success": True, |
|
"fallback": True |
|
} |
|
|
|
async def _plan_course( |
|
self, |
|
topic: str, |
|
options: GenerationOptions, |
|
provider: str |
|
) -> Dict[str, Any]: |
|
"""Plan the overall course structure""" |
|
|
|
print(f"π Planning course for {topic} with {options.lesson_count} lessons") |
|
|
|
prompt = f"""Create a course plan for: "{topic}" |
|
|
|
Requirements: |
|
- {options.lesson_count} lessons |
|
- {options.difficulty.value} difficulty level |
|
- {options.max_lesson_duration} minutes per lesson |
|
|
|
Return ONLY a JSON object with: |
|
- title: Course title |
|
- description: Brief description |
|
- learning_objectives: Array of 3-5 objectives |
|
- lesson_titles: Array of lesson titles |
|
- estimated_duration: Total course duration |
|
|
|
Focus on practical, engaging content. Return only valid JSON, no other text. Do not wrap the JSON in markdown code blocks or backticks.""" |
|
|
|
messages = [ |
|
Message(role="system", content=self.system_prompt), |
|
Message(role="user", content=prompt) |
|
] |
|
|
|
try: |
|
response_text = await self._get_llm_response(provider, messages) |
|
print(f"π LLM response for course plan: {response_text[:200]}...") |
|
|
|
|
|
course_plan = smart_json_loads(response_text) |
|
if course_plan is not None: |
|
print(f"β
Successfully parsed course plan JSON") |
|
return course_plan |
|
else: |
|
raise ValueError("Failed to extract valid JSON from response") |
|
|
|
except Exception as e: |
|
print(f"β JSON parsing failed for course plan: {e}") |
|
print(f"Raw response: {response_text}") |
|
|
|
return { |
|
"title": f"Course: {topic}", |
|
"description": f"A comprehensive introduction to {topic}", |
|
"learning_objectives": [ |
|
f"Understand the fundamentals of {topic}", |
|
f"Apply key concepts of {topic}", |
|
f"Analyze real-world applications of {topic}" |
|
], |
|
"lesson_titles": [f"Lesson {i+1}: {topic} Fundamentals" for i in range(options.lesson_count)], |
|
"estimated_duration": options.lesson_count * options.max_lesson_duration |
|
} |
|
except Exception as e: |
|
print(f"β Error in course planning: {e}") |
|
|
|
return { |
|
"title": f"Course: {topic}", |
|
"description": f"A comprehensive introduction to {topic}", |
|
"learning_objectives": [ |
|
f"Understand the fundamentals of {topic}", |
|
f"Apply key concepts of {topic}", |
|
f"Analyze real-world applications of {topic}" |
|
], |
|
"lesson_titles": [f"Lesson {i+1}: {topic} Basics" for i in range(options.lesson_count)], |
|
"estimated_duration": options.lesson_count * options.max_lesson_duration |
|
} |
|
|
|
async def _generate_lessons( |
|
self, |
|
course_plan: Dict[str, Any], |
|
options: GenerationOptions, |
|
provider: str |
|
) -> List[Dict[str, Any]]: |
|
"""Generate detailed lesson content""" |
|
|
|
lessons = [] |
|
lesson_titles = course_plan.get("lesson_titles", []) |
|
print(f"π Generating {len(lesson_titles)} lessons") |
|
|
|
for i, title in enumerate(lesson_titles): |
|
print(f"π Generating lesson {i+1}: {title}") |
|
|
|
|
|
research_context = "" |
|
if hasattr(self, '_current_research') and self._current_research: |
|
research_context = f""" |
|
Research Context: |
|
{self._current_research.get('research_summary', '')} |
|
|
|
Key Concepts: {', '.join(self._current_research.get('key_concepts', [])[:3])} |
|
""" |
|
|
|
|
|
difficulty_guidelines = { |
|
"beginner": """ |
|
- Use simple, clear language and avoid technical jargon |
|
- Explain every concept from the ground up with no assumed prior knowledge |
|
- Include step-by-step instructions with detailed explanations for each step |
|
- Use basic, relatable examples that anyone can understand |
|
- Focus on fundamental concepts and practical applications |
|
- Include plenty of context and background information |
|
- Break down complex ideas into smaller, digestible parts""", |
|
"intermediate": """ |
|
- Use some technical terminology but explain it when first introduced |
|
- Assume basic familiarity with the subject area |
|
- Include moderately complex examples that build on fundamental knowledge |
|
- Focus on practical applications and real-world scenarios |
|
- Introduce some advanced concepts but explain them thoroughly |
|
- Include best practices and common patterns |
|
- Balance theory with hands-on practice""", |
|
"advanced": """ |
|
- Use technical language and industry-standard terminology |
|
- Assume solid foundational knowledge in the subject area |
|
- Include complex, real-world examples and edge cases |
|
- Focus on advanced techniques, optimization, and expert-level practices |
|
- Discuss trade-offs, limitations, and alternative approaches |
|
- Include cutting-edge developments and research |
|
- Emphasize problem-solving and critical thinking""" |
|
} |
|
|
|
prompt = f"""Create comprehensive, detailed educational content for: "{title}" |
|
|
|
This is lesson {i+1} of {len(lesson_titles)} in a course about "{course_plan.get('title', '')}" |
|
|
|
{research_context} |
|
|
|
Requirements: |
|
- Duration: {options.max_lesson_duration} minutes |
|
- Difficulty Level: {options.difficulty.value.upper()} |
|
|
|
DIFFICULTY-SPECIFIC GUIDELINES for {options.difficulty.value.upper()} level: |
|
{difficulty_guidelines.get(options.difficulty.value, difficulty_guidelines["intermediate"])} |
|
|
|
Content Requirements: |
|
- Create EXTENSIVE, thorough content that truly teaches the topic (aim for 2000+ words) |
|
- Include multiple practical examples with code/step-by-step instructions |
|
- Provide detailed explanations that help students understand complex concepts |
|
- Include real-world applications and use cases |
|
- Add troubleshooting tips and common pitfalls |
|
- Make content comprehensive enough to actually learn from |
|
- Include image placeholders where visual aids would be helpful |
|
|
|
IMPORTANT: When you want to include an educational image, use this format: |
|
{{{{IMAGE_PLACEHOLDER:{title}:Description of the image needed}}}} |
|
|
|
LIMIT: Use a MAXIMUM of 3 image placeholders per lesson. Choose the most important visual aids. |
|
|
|
For example: |
|
{{{{IMAGE_PLACEHOLDER:{title}:Diagram showing the main components}}}} |
|
{{{{IMAGE_PLACEHOLDER:{title}:Screenshot of the user interface}}}} |
|
{{{{IMAGE_PLACEHOLDER:{title}:Flowchart of the process}}}} |
|
|
|
The content should be substantial and educational. Include sections like: |
|
- Introduction with context and importance |
|
- Core concepts with detailed explanations |
|
- Multiple practical examples with code/instructions |
|
- Step-by-step tutorials |
|
- Best practices and tips |
|
- Common mistakes to avoid |
|
- Real-world applications |
|
- Further resources and next steps |
|
|
|
Return ONLY a JSON object with: |
|
- title: Lesson title |
|
- duration: Estimated duration |
|
- objectives: Learning objectives for this lesson (3-5 specific objectives) |
|
- content: EXTENSIVE lesson content in markdown format (2000+ words, with image placeholders) |
|
- key_takeaways: Array of 5-7 key points |
|
- examples: Array of 3-5 detailed practical examples with explanations |
|
|
|
Return only valid JSON, no other text. Do not wrap the JSON in markdown code blocks or backticks.""" |
|
|
|
messages = [ |
|
Message(role="system", content=self.system_prompt), |
|
Message(role="user", content=prompt) |
|
] |
|
|
|
try: |
|
response_text = await self._get_llm_response(provider, messages) |
|
print(f"π LLM response for lesson {i+1}: {response_text[:100]}...") |
|
|
|
lesson_data = smart_json_loads(response_text) |
|
if lesson_data is not None: |
|
lessons.append(lesson_data) |
|
print(f"β
Successfully generated lesson {i+1}") |
|
else: |
|
raise ValueError("Failed to extract valid JSON from response") |
|
|
|
except Exception as e: |
|
print(f"β JSON parsing failed for lesson {i+1}: {e}") |
|
|
|
lessons.append({ |
|
"title": title, |
|
"duration": options.max_lesson_duration, |
|
"objectives": [f"Learn about {title}"], |
|
"content": f"# {title}\n\nThis lesson covers the fundamentals of {title}.\n\n## Key Concepts\n\n- Important concept 1\n- Important concept 2\n- Important concept 3\n\n## Examples\n\nHere are some practical examples related to {title}...", |
|
"key_takeaways": [f"Key concept from {title}", f"Important principle of {title}"], |
|
"examples": [f"Example 1 related to {title}", f"Example 2 related to {title}"] |
|
}) |
|
except Exception as e: |
|
print(f"β Error generating lesson {i+1}: {e}") |
|
|
|
lessons.append({ |
|
"title": title, |
|
"duration": options.max_lesson_duration, |
|
"objectives": [f"Learn about {title}"], |
|
"content": f"# {title}\n\nDetailed content about {title}...", |
|
"key_takeaways": [f"Key concept from {title}"], |
|
"examples": [f"Example related to {title}"] |
|
}) |
|
|
|
return lessons |
|
|
|
async def _generate_flashcards( |
|
self, |
|
lessons: List[Dict[str, Any]], |
|
provider: str, |
|
difficulty: str = "intermediate" |
|
) -> List[Dict[str, Any]]: |
|
"""Generate flashcards from lesson content with difficulty-appropriate complexity""" |
|
|
|
print(f"π Generating {difficulty} level flashcards from lesson content") |
|
|
|
|
|
all_content = "\n\n".join([ |
|
lesson.get("content", "") + "\n" + |
|
"\n".join(lesson.get("key_takeaways", [])) |
|
for lesson in lessons |
|
]) |
|
|
|
|
|
flashcard_guidelines = { |
|
"beginner": """ |
|
- Focus on basic definitions and simple facts |
|
- Use clear, simple language in both questions and answers |
|
- Test fundamental concepts and terminology |
|
- Include basic examples and straightforward explanations |
|
- Avoid complex relationships or multi-step reasoning |
|
- Keep answers concise and direct""", |
|
"intermediate": """ |
|
- Include concepts, relationships, and applications |
|
- Test understanding of how concepts connect |
|
- Use moderate complexity in questions and explanations |
|
- Include practical examples and use cases |
|
- Test both knowledge and basic application |
|
- Balance definitions with conceptual understanding""", |
|
"advanced": """ |
|
- Focus on complex principles, applications, and analysis |
|
- Test deep understanding and critical thinking |
|
- Include challenging scenarios and edge cases |
|
- Test ability to synthesize and evaluate information |
|
- Include questions about trade-offs and best practices |
|
- Emphasize expert-level insights and nuanced understanding""" |
|
} |
|
|
|
prompt = f"""Create flashcards based on this lesson content: |
|
|
|
{all_content[:2000]}... |
|
|
|
DIFFICULTY LEVEL: {difficulty.upper()} |
|
|
|
FLASHCARD GUIDELINES for {difficulty.upper()} level: |
|
{flashcard_guidelines.get(difficulty, flashcard_guidelines["intermediate"])} |
|
|
|
Generate 10-15 flashcards covering the most important concepts at the {difficulty} level. |
|
|
|
Return ONLY a JSON array where each flashcard has: |
|
- question: The question/prompt |
|
- answer: The answer/explanation |
|
- category: Which lesson/topic this relates to |
|
|
|
Ensure flashcards match the {difficulty} difficulty level. Return only valid JSON, no other text. Do not wrap the JSON in markdown code blocks or backticks.""" |
|
|
|
messages = [ |
|
Message(role="system", content=self.system_prompt), |
|
Message(role="user", content=prompt) |
|
] |
|
|
|
try: |
|
response_text = await self._get_llm_response(provider, messages) |
|
print(f"π LLM response for flashcards: {response_text[:100]}...") |
|
|
|
flashcards = smart_json_loads(response_text) |
|
if flashcards is not None: |
|
print(f"β
Successfully generated {len(flashcards)} flashcards") |
|
return flashcards |
|
else: |
|
raise ValueError("Failed to extract valid JSON from response") |
|
|
|
except Exception as e: |
|
print(f"β JSON parsing failed for flashcards: {e}") |
|
|
|
return [ |
|
{ |
|
"question": f"What is the main concept in {lesson.get('title', 'this lesson')}?", |
|
"answer": f"The main concept is related to {lesson.get('title', 'the lesson topic')}", |
|
"category": lesson.get('title', 'General') |
|
} |
|
for lesson in lessons[:5] |
|
] |
|
except Exception as e: |
|
print(f"β Error generating flashcards: {e}") |
|
return [] |
|
|
|
async def _generate_quiz( |
|
self, |
|
lessons: List[Dict[str, Any]], |
|
provider: str, |
|
difficulty: str = "intermediate" |
|
) -> Dict[str, Any]: |
|
"""Generate a multiple-choice quiz with difficulty-appropriate questions""" |
|
|
|
print(f"π Generating {difficulty} level quiz from lesson content") |
|
|
|
|
|
all_content = "\n\n".join([ |
|
lesson.get("content", "") + "\n" + |
|
"\n".join(lesson.get("key_takeaways", [])) |
|
for lesson in lessons |
|
]) |
|
|
|
|
|
quiz_guidelines = { |
|
"beginner": """ |
|
- Focus on basic recall and recognition questions |
|
- Test fundamental concepts and definitions |
|
- Use simple, clear language in questions |
|
- Include straightforward examples |
|
- Avoid trick questions or complex scenarios |
|
- Test one concept per question""", |
|
"intermediate": """ |
|
- Include application and analysis questions |
|
- Test understanding of relationships between concepts |
|
- Use moderate complexity in scenarios |
|
- Include some problem-solving questions |
|
- Test ability to apply knowledge to new situations |
|
- Mix recall with application questions""", |
|
"advanced": """ |
|
- Focus on analysis, synthesis, and evaluation questions |
|
- Test complex problem-solving abilities |
|
- Include multi-step reasoning questions |
|
- Use challenging real-world scenarios |
|
- Test ability to compare and contrast approaches |
|
- Include questions about trade-offs and limitations""" |
|
} |
|
|
|
prompt = f"""Create a 10-question multiple-choice quiz based on this content: |
|
|
|
{all_content[:2000]}... |
|
|
|
DIFFICULTY LEVEL: {difficulty.upper()} |
|
|
|
QUIZ GUIDELINES for {difficulty.upper()} level: |
|
{quiz_guidelines.get(difficulty, quiz_guidelines["intermediate"])} |
|
|
|
Return ONLY a JSON object with: |
|
- title: Quiz title |
|
- instructions: Brief instructions |
|
- questions: Array of question objects |
|
|
|
Each question should have: |
|
- question: The question text |
|
- options: Array of 4 multiple choice options (A, B, C, D) |
|
- correct_answer: The letter of the correct answer |
|
- explanation: Why this answer is correct |
|
|
|
Ensure questions match the {difficulty} difficulty level. Return only valid JSON, no other text. Do not wrap the JSON in markdown code blocks or backticks.""" |
|
|
|
messages = [ |
|
Message(role="system", content=self.system_prompt), |
|
Message(role="user", content=prompt) |
|
] |
|
|
|
try: |
|
response_text = await self._get_llm_response(provider, messages) |
|
print(f"π LLM response for quiz: {response_text[:100]}...") |
|
|
|
quiz = smart_json_loads(response_text) |
|
if quiz is not None: |
|
print(f"β
Successfully generated quiz with {len(quiz.get('questions', []))} questions") |
|
return quiz |
|
else: |
|
raise ValueError("Failed to extract valid JSON from response") |
|
|
|
except Exception as e: |
|
print(f"β JSON parsing failed for quiz: {e}") |
|
|
|
return { |
|
"title": "Course Quiz", |
|
"instructions": "Choose the best answer for each question.", |
|
"questions": [ |
|
{ |
|
"question": f"What is a key concept from {lesson.get('title', 'this lesson')}?", |
|
"options": ["A) Option A", "B) Option B", "C) Option C", "D) Option D"], |
|
"correct_answer": "A", |
|
"explanation": "This is the correct answer based on the lesson content." |
|
} |
|
for lesson in lessons[:3] |
|
] |
|
} |
|
except Exception as e: |
|
print(f"β Error generating quiz: {e}") |
|
return { |
|
"title": "Course Quiz", |
|
"instructions": "Choose the best answer for each question.", |
|
"questions": [] |
|
} |
|
|
|
async def _generate_images(self, lessons: List[Dict[str, Any]]) -> List[Dict[str, Any]]: |
|
"""Generate educational images for lessons using Pollinations API""" |
|
|
|
print("πΌοΈ Generating actual images for lessons using Pollinations API") |
|
|
|
images = [] |
|
|
|
|
|
for lesson in lessons: |
|
lesson_title = lesson.get("title", "") |
|
content = lesson.get("content", "") |
|
|
|
print(f"π Processing images for lesson: {lesson_title}") |
|
|
|
|
|
placeholders = extract_image_placeholders(content) |
|
|
|
if not placeholders: |
|
print(f"π No image placeholders found for {lesson_title}, generating 1 default image") |
|
|
|
topic = lesson_title.split(":")[0] if ":" in lesson_title else lesson_title |
|
|
|
try: |
|
image_data = await generate_educational_image(lesson_title, topic, "educational") |
|
if image_data: |
|
images.append(image_data) |
|
print(f"β
Generated default image for: {lesson_title}") |
|
else: |
|
print(f"β οΈ Failed to generate default image for: {lesson_title}") |
|
except Exception as e: |
|
print(f"β Error generating default image for {lesson_title}: {e}") |
|
else: |
|
|
|
print(f"π¨ Found {len(placeholders)} placeholders for {lesson_title}, generating images for ALL of them") |
|
|
|
for i, placeholder in enumerate(placeholders): |
|
placeholder_lesson_title = placeholder["lesson_title"] |
|
description = placeholder["description"] |
|
|
|
try: |
|
|
|
topic = description |
|
image_data = await generate_educational_image(placeholder_lesson_title, topic, "educational") |
|
|
|
if image_data: |
|
|
|
image_data["placeholder_description"] = description |
|
image_data["placeholder_full"] = placeholder["placeholder"] |
|
images.append(image_data) |
|
print(f"β
Generated image {i+1}/{len(placeholders)} for {lesson_title}: {description[:50]}...") |
|
else: |
|
print(f"β οΈ Failed to generate image {i+1} for {lesson_title}: {description[:50]}...") |
|
|
|
except Exception as e: |
|
print(f"β Error generating image {i+1} for {lesson_title}: {e}") |
|
|
|
|
|
for lesson in lessons: |
|
if images: |
|
lesson["content"] = replace_image_placeholders(lesson["content"], images) |
|
|
|
print(f"β
Generated {len(images)} total images and updated lesson content") |
|
return images |
|
|
|
async def _get_llm_response(self, provider: str, messages: List[Message]) -> str: |
|
"""Get a complete response from the LLM by collecting all streaming chunks""" |
|
|
|
print(f"π§ Getting LLM response from {provider}") |
|
|
|
response_text = "" |
|
|
|
try: |
|
async for chunk in self.llm_client.generate_stream( |
|
provider=provider, |
|
messages=messages |
|
): |
|
if chunk.type == "text": |
|
response_text += chunk.content |
|
elif chunk.type == "error": |
|
raise Exception(f"LLM error: {chunk.content}") |
|
|
|
print(f"β
Got LLM response ({len(response_text)} characters)") |
|
return response_text.strip() |
|
|
|
except Exception as e: |
|
print(f"β Error getting LLM response: {e}") |
|
raise |
|
|
|
async def refine_course( |
|
self, |
|
course_data: Dict[str, Any], |
|
user_request: str, |
|
provider: Optional[str] = None |
|
) -> Dict[str, Any]: |
|
"""Refine or add to existing course based on user feedback""" |
|
|
|
|
|
if provider is None: |
|
provider = self.default_provider |
|
|
|
prompt = f"""The user wants to modify this course: |
|
|
|
Current course: {json.dumps(course_data, indent=2)[:1000]}... |
|
|
|
User request: "{user_request}" |
|
|
|
Please modify the course accordingly. If they want more information about a specific topic, research it and add detailed content. Return the updated course data in the same JSON format.""" |
|
|
|
messages = [ |
|
Message(role="system", content=self.system_prompt), |
|
Message(role="user", content=prompt) |
|
] |
|
|
|
try: |
|
response_text = await self._get_llm_response(provider, messages) |
|
refined_course = smart_json_loads(response_text) |
|
if refined_course is not None: |
|
return refined_course |
|
else: |
|
return course_data |
|
except: |
|
return course_data |
|
|
|
def get_available_providers(self) -> List[str]: |
|
"""Get list of available LLM providers""" |
|
return self.llm_client.get_available_providers() |