Spaces:
Runtime error
Runtime error
Create crew_course_agents.py
Browse files- utils/crew_course_agents.py +140 -0
utils/crew_course_agents.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/crew_course_agents.py
|
| 2 |
+
|
| 3 |
+
from crewai import Agent, Task, Crew, Process
|
| 4 |
+
from langchain.llms import Anthropic
|
| 5 |
+
from langchain.tools import DuckDuckGoSearchRun
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import json
|
| 8 |
+
import streamlit as st
|
| 9 |
+
|
| 10 |
+
class CourseCrewOrchestrator:
|
| 11 |
+
def __init__(self, anthropic_api_key):
|
| 12 |
+
self.llm = Anthropic(anthropic_api_key=anthropic_api_key)
|
| 13 |
+
self.search_tool = DuckDuckGoSearchRun()
|
| 14 |
+
self.data_dir = Path("data/courses")
|
| 15 |
+
self.data_dir.mkdir(parents=True, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
def create_agents(self):
|
| 18 |
+
"""Create specialized agents for course creation"""
|
| 19 |
+
curriculum_designer = Agent(
|
| 20 |
+
role='Curriculum Designer',
|
| 21 |
+
goal='Design comprehensive and structured trading course outlines',
|
| 22 |
+
backstory='Expert in educational design with years of experience in financial education',
|
| 23 |
+
tools=[self.search_tool],
|
| 24 |
+
llm=self.llm,
|
| 25 |
+
verbose=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
content_creator = Agent(
|
| 29 |
+
role='Content Creator',
|
| 30 |
+
goal='Create engaging and informative trading content',
|
| 31 |
+
backstory='Experienced financial writer and educator with expertise in explaining complex concepts',
|
| 32 |
+
tools=[self.search_tool],
|
| 33 |
+
llm=self.llm,
|
| 34 |
+
verbose=True
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
technical_expert = Agent(
|
| 38 |
+
role='Trading Technical Expert',
|
| 39 |
+
goal='Provide deep technical insights and practical trading knowledge',
|
| 40 |
+
backstory='Professional trader with 15+ years of market experience',
|
| 41 |
+
tools=[self.search_tool],
|
| 42 |
+
llm=self.llm,
|
| 43 |
+
verbose=True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
exercise_creator = Agent(
|
| 47 |
+
role='Exercise Designer',
|
| 48 |
+
goal='Create practical exercises and assessments',
|
| 49 |
+
backstory='Expert in creating hands-on learning experiences in trading',
|
| 50 |
+
tools=[self.search_tool],
|
| 51 |
+
llm=self.llm,
|
| 52 |
+
verbose=True
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
'curriculum': curriculum_designer,
|
| 57 |
+
'content': content_creator,
|
| 58 |
+
'technical': technical_expert,
|
| 59 |
+
'exercise': exercise_creator
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
def create_course_tasks(self, agents, topic):
|
| 63 |
+
"""Create sequential tasks for course creation"""
|
| 64 |
+
tasks = [
|
| 65 |
+
Task(
|
| 66 |
+
description=f"Design a comprehensive curriculum outline for {topic}. Include learning objectives, prerequisites, and module structure.",
|
| 67 |
+
agent=agents['curriculum']
|
| 68 |
+
),
|
| 69 |
+
Task(
|
| 70 |
+
description=f"Create detailed content for each module in the {topic} curriculum. Focus on clear explanations and examples.",
|
| 71 |
+
agent=agents['content']
|
| 72 |
+
),
|
| 73 |
+
Task(
|
| 74 |
+
description=f"Add technical insights and practical trading strategies for {topic}. Include real market examples.",
|
| 75 |
+
agent=agents['technical']
|
| 76 |
+
),
|
| 77 |
+
Task(
|
| 78 |
+
description=f"Design exercises, quizzes, and practical assignments for {topic}. Include answer keys and explanations.",
|
| 79 |
+
agent=agents['exercise']
|
| 80 |
+
)
|
| 81 |
+
]
|
| 82 |
+
return tasks
|
| 83 |
+
|
| 84 |
+
async def generate_course(self, course_id, topic):
|
| 85 |
+
"""Generate course content using CrewAI"""
|
| 86 |
+
try:
|
| 87 |
+
# Create status file
|
| 88 |
+
self._update_status(course_id, 'starting', 0)
|
| 89 |
+
|
| 90 |
+
# Initialize agents and tasks
|
| 91 |
+
agents = self.create_agents()
|
| 92 |
+
tasks = self.create_course_tasks(agents, topic)
|
| 93 |
+
|
| 94 |
+
# Create and run the crew
|
| 95 |
+
course_crew = Crew(
|
| 96 |
+
agents=list(agents.values()),
|
| 97 |
+
tasks=tasks,
|
| 98 |
+
process=Process.sequential
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# Execute tasks and save results
|
| 102 |
+
result = await course_crew.run()
|
| 103 |
+
course_content = self._parse_crew_result(result)
|
| 104 |
+
|
| 105 |
+
# Save course content
|
| 106 |
+
self._save_course_content(course_id, course_content)
|
| 107 |
+
self._update_status(course_id, 'complete', 100)
|
| 108 |
+
|
| 109 |
+
return course_content
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
self._update_status(course_id, 'error', 0, str(e))
|
| 113 |
+
raise e
|
| 114 |
+
|
| 115 |
+
def _update_status(self, course_id, status, progress, error=None):
|
| 116 |
+
"""Update course generation status"""
|
| 117 |
+
status_data = {
|
| 118 |
+
'status': status,
|
| 119 |
+
'progress': progress,
|
| 120 |
+
'error': error
|
| 121 |
+
}
|
| 122 |
+
status_file = self.data_dir / f"{course_id}_status.json"
|
| 123 |
+
with open(status_file, 'w') as f:
|
| 124 |
+
json.dump(status_data, f)
|
| 125 |
+
|
| 126 |
+
def _save_course_content(self, course_id, content):
|
| 127 |
+
"""Save generated course content"""
|
| 128 |
+
content_file = self.data_dir / f"{course_id}_content.json"
|
| 129 |
+
with open(content_file, 'w') as f:
|
| 130 |
+
json.dump(content, f)
|
| 131 |
+
|
| 132 |
+
def _parse_crew_result(self, result):
|
| 133 |
+
"""Parse and structure the crew's output"""
|
| 134 |
+
# Implement parsing logic based on the crew's output format
|
| 135 |
+
return {
|
| 136 |
+
'curriculum': result.get('curriculum', {}),
|
| 137 |
+
'content': result.get('content', {}),
|
| 138 |
+
'technical': result.get('technical', {}),
|
| 139 |
+
'exercises': result.get('exercises', {})
|
| 140 |
+
}
|