Spaces:
Sleeping
Sleeping
Create agents.py
Browse files
agents.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# agents.py
|
| 2 |
+
from crewai import Agent, Crew, Process, Task
|
| 3 |
+
from crewai_tools import SerperDevTool
|
| 4 |
+
|
| 5 |
+
def create_research_crew(topic: str):
|
| 6 |
+
"""Create the research crew with SerperDev tool integration"""
|
| 7 |
+
try:
|
| 8 |
+
# Initialize SerperDev tool
|
| 9 |
+
search_tool = SerperDevTool()
|
| 10 |
+
|
| 11 |
+
# Create agents
|
| 12 |
+
researcher = Agent(
|
| 13 |
+
role='Research Analyst',
|
| 14 |
+
goal=f'Conduct exhaustive market research about {topic} with comprehensive analysis',
|
| 15 |
+
backstory="""Senior market research analyst with 15+ years of experience in detailed industry analysis.""",
|
| 16 |
+
tools=[search_tool],
|
| 17 |
+
verbose=True
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
analyst = Agent(
|
| 21 |
+
role='Data Analyst',
|
| 22 |
+
goal='Transform research data into actionable insights with visualizations',
|
| 23 |
+
backstory="""Expert data analyst specializing in predictive analytics and trend analysis.""",
|
| 24 |
+
tools=[search_tool],
|
| 25 |
+
verbose=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
writer = Agent(
|
| 29 |
+
role='Report Writer',
|
| 30 |
+
goal='Create comprehensive market research reports with compelling narratives',
|
| 31 |
+
backstory="""Professional writer specializing in creating clear, engaging market research reports.""",
|
| 32 |
+
verbose=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Create tasks
|
| 36 |
+
research_task = create_research_task(topic, researcher)
|
| 37 |
+
analysis_task = create_analysis_task(analyst, research_task)
|
| 38 |
+
report_task = create_report_task(writer, [research_task, analysis_task])
|
| 39 |
+
|
| 40 |
+
return Crew(
|
| 41 |
+
agents=[researcher, analyst, writer],
|
| 42 |
+
tasks=[research_task, analysis_task, report_task],
|
| 43 |
+
verbose=True,
|
| 44 |
+
process=Process.sequential
|
| 45 |
+
)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
raise Exception(f"Error initializing research crew: {str(e)}")
|
| 48 |
+
|
| 49 |
+
def create_research_task(topic: str, agent):
|
| 50 |
+
"""Create the research task"""
|
| 51 |
+
return Task(
|
| 52 |
+
description=f"""
|
| 53 |
+
Conduct exhaustive market research on {topic} covering:
|
| 54 |
+
1. Market Overview (size, growth, segmentation)
|
| 55 |
+
2. Competitive Analysis (players, shares, positioning)
|
| 56 |
+
3. Market Dynamics (drivers, challenges, trends)
|
| 57 |
+
4. Economic Factors (macro influences, indicators)
|
| 58 |
+
|
| 59 |
+
Provide specific numerical data and sources.
|
| 60 |
+
""",
|
| 61 |
+
agent=agent,
|
| 62 |
+
expected_output="Comprehensive research data with verified sources"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
def create_analysis_task(agent, research_task):
|
| 66 |
+
"""Create the analysis task"""
|
| 67 |
+
return Task(
|
| 68 |
+
description="""
|
| 69 |
+
Analyze the research findings providing:
|
| 70 |
+
1. Strategic Market Analysis
|
| 71 |
+
2. Advanced Data Analysis
|
| 72 |
+
3. Competitive Intelligence
|
| 73 |
+
4. Trend Analysis
|
| 74 |
+
|
| 75 |
+
Include specific metrics and visualization-ready data.
|
| 76 |
+
""",
|
| 77 |
+
agent=agent,
|
| 78 |
+
expected_output="Detailed analysis with visualization-ready metrics",
|
| 79 |
+
context=[research_task]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
def create_report_task(agent, context_tasks):
|
| 83 |
+
"""Create the report task"""
|
| 84 |
+
return Task(
|
| 85 |
+
description="""
|
| 86 |
+
Create a professional report with:
|
| 87 |
+
1. Executive Summary (2-3 pages)
|
| 88 |
+
2. Detailed Analysis (10+ pages)
|
| 89 |
+
3. Sources and Citations
|
| 90 |
+
|
| 91 |
+
Format as JSON with exec_summary, detailed_report, and metrics sections.
|
| 92 |
+
""",
|
| 93 |
+
agent=agent,
|
| 94 |
+
expected_output="JSON formatted report with executive summary and details",
|
| 95 |
+
context=context_tasks
|
| 96 |
+
)
|