Spaces:
Sleeping
Sleeping
Update agents.py
Browse files
agents.py
CHANGED
|
@@ -1,176 +1,135 @@
|
|
| 1 |
# agents.py
|
| 2 |
from crewai import Agent, Crew, Process, Task
|
| 3 |
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
|
| 4 |
-
from langchain.tools import Tool
|
| 5 |
|
| 6 |
-
# agents.py
|
| 7 |
-
class MarketResearchTools:
|
| 8 |
-
"""Advanced research tools using Serper capabilities"""
|
| 9 |
-
|
| 10 |
-
def __init__(self):
|
| 11 |
-
self.serper = SerperDevTool()
|
| 12 |
-
self.scraper = ScrapeWebsiteTool()
|
| 13 |
-
|
| 14 |
-
# Specialized search tools using Serper's default search
|
| 15 |
-
self.news_search = Tool(
|
| 16 |
-
name="News Research",
|
| 17 |
-
func=self.serper.run, # Using the standard run method
|
| 18 |
-
description="Search for recent news articles and market updates"
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
self.market_search = Tool(
|
| 22 |
-
name="Market Research",
|
| 23 |
-
func=self.serper.run, # Using the standard run method
|
| 24 |
-
description="Search for market reports and industry analysis"
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
self.competitor_search = Tool(
|
| 28 |
-
name="Competitor Analysis",
|
| 29 |
-
func=self.serper.run, # Using the standard run method
|
| 30 |
-
description="Research companies and their market positions"
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
def search_with_type(self, query: str, search_type: str) -> str:
|
| 34 |
-
"""Custom search method with type specification"""
|
| 35 |
-
base_query = query
|
| 36 |
-
if search_type == "news":
|
| 37 |
-
base_query = f"latest news about {query} market trends"
|
| 38 |
-
elif search_type == "market":
|
| 39 |
-
base_query = f"market research reports analysis {query} industry"
|
| 40 |
-
elif search_type == "competitor":
|
| 41 |
-
base_query = f"company analysis competitors {query} market share"
|
| 42 |
-
|
| 43 |
-
return self.serper.run(base_query)
|
| 44 |
-
|
| 45 |
def create_research_crew(topic: str):
|
| 46 |
"""Create an advanced research crew with specialized tools"""
|
| 47 |
try:
|
| 48 |
-
tools
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Advanced Research Analyst
|
| 51 |
researcher = Agent(
|
| 52 |
role='Senior Market Research Analyst',
|
| 53 |
goal=f'Conduct exhaustive market research about {topic} with detailed data and industry insights',
|
| 54 |
backstory="""You are a veteran market research analyst with 20+ years of experience.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
tools=[
|
| 60 |
-
tools.market_search, # For general market research
|
| 61 |
-
tools.scraper, # For website scraping
|
| 62 |
-
],
|
| 63 |
verbose=True
|
| 64 |
)
|
| 65 |
|
| 66 |
-
# Industry Expert Analyst
|
| 67 |
analyst = Agent(
|
| 68 |
role='Industry Expert & Strategy Analyst',
|
| 69 |
goal='Transform research into strategic insights and actionable recommendations',
|
| 70 |
backstory="""You are an industry expert with deep domain knowledge and strategic consulting
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
tools=[
|
| 75 |
verbose=True
|
| 76 |
)
|
| 77 |
|
| 78 |
-
# Professional Report Writer
|
| 79 |
writer = Agent(
|
| 80 |
role='Executive Report Writer',
|
| 81 |
goal='Create compelling, comprehensive market analysis reports',
|
| 82 |
backstory="""You are an experienced business writer who specializes in creating
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
verbose=True
|
| 87 |
)
|
| 88 |
|
| 89 |
# Create enhanced tasks
|
| 90 |
research_task = Task(
|
| 91 |
description=f"""
|
| 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 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
agent=researcher,
|
| 136 |
expected_output="Comprehensive research data with verified sources and confidence levels"
|
| 137 |
)
|
| 138 |
|
| 139 |
analysis_task = Task(
|
| 140 |
description="""
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
agent=analyst,
|
| 175 |
expected_output="Strategic analysis with actionable insights",
|
| 176 |
context=[research_task]
|
|
@@ -178,68 +137,67 @@ def create_research_crew(topic: str):
|
|
| 178 |
|
| 179 |
report_task = Task(
|
| 180 |
description="""
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
}
|
| 227 |
}
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
| 237 |
agent=writer,
|
| 238 |
expected_output="Professional report with executive summary and detailed analysis",
|
| 239 |
context=[research_task, analysis_task]
|
| 240 |
)
|
| 241 |
|
| 242 |
-
# Create and return the crew
|
| 243 |
return Crew(
|
| 244 |
agents=[researcher, analyst, writer],
|
| 245 |
tasks=[research_task, analysis_task, report_task],
|
|
|
|
| 1 |
# agents.py
|
| 2 |
from crewai import Agent, Crew, Process, Task
|
| 3 |
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def create_research_crew(topic: str):
|
| 6 |
"""Create an advanced research crew with specialized tools"""
|
| 7 |
try:
|
| 8 |
+
# Initialize tools directly - no wrapper needed
|
| 9 |
+
search_tool = SerperDevTool()
|
| 10 |
+
scrape_tool = ScrapeWebsiteTool()
|
| 11 |
|
| 12 |
+
# Advanced Research Analyst
|
| 13 |
researcher = Agent(
|
| 14 |
role='Senior Market Research Analyst',
|
| 15 |
goal=f'Conduct exhaustive market research about {topic} with detailed data and industry insights',
|
| 16 |
backstory="""You are a veteran market research analyst with 20+ years of experience.
|
| 17 |
+
You excel at uncovering hard-to-find data points, analyzing industry dynamics,
|
| 18 |
+
and identifying emerging trends. You have a strong network of industry contacts
|
| 19 |
+
and access to premium research databases. You always validate data through
|
| 20 |
+
multiple sources and provide confidence levels for your findings.""",
|
| 21 |
+
tools=[search_tool, scrape_tool], # Use tools directly
|
|
|
|
|
|
|
|
|
|
| 22 |
verbose=True
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Industry Expert Analyst
|
| 26 |
analyst = Agent(
|
| 27 |
role='Industry Expert & Strategy Analyst',
|
| 28 |
goal='Transform research into strategic insights and actionable recommendations',
|
| 29 |
backstory="""You are an industry expert with deep domain knowledge and strategic consulting
|
| 30 |
+
experience. You specialize in connecting market data to business implications,
|
| 31 |
+
forecasting industry changes, and developing strategic recommendations.
|
| 32 |
+
Your analysis is always backed by concrete examples and case studies.""",
|
| 33 |
+
tools=[search_tool], # Only needs search tool
|
| 34 |
verbose=True
|
| 35 |
)
|
| 36 |
|
| 37 |
+
# Professional Report Writer
|
| 38 |
writer = Agent(
|
| 39 |
role='Executive Report Writer',
|
| 40 |
goal='Create compelling, comprehensive market analysis reports',
|
| 41 |
backstory="""You are an experienced business writer who specializes in creating
|
| 42 |
+
executive-level market research reports. You excel at distilling complex
|
| 43 |
+
information into clear narratives while maintaining analytical rigor.
|
| 44 |
+
You always include relevant examples, case studies, and data visualizations.""",
|
| 45 |
verbose=True
|
| 46 |
)
|
| 47 |
|
| 48 |
# Create enhanced tasks
|
| 49 |
research_task = Task(
|
| 50 |
description=f"""
|
| 51 |
+
Conduct comprehensive market research on {topic} with the following focus areas:
|
| 52 |
+
|
| 53 |
+
1. Market Overview:
|
| 54 |
+
- Current market size with specific values
|
| 55 |
+
- Historical growth patterns (5-year minimum)
|
| 56 |
+
- Future projections with CAGR
|
| 57 |
+
- Market segmentation analysis
|
| 58 |
+
- Regional market distribution
|
| 59 |
+
- Value chain analysis
|
| 60 |
+
- Pricing trends and dynamics
|
| 61 |
+
|
| 62 |
+
2. Competitive Landscape:
|
| 63 |
+
- Detailed analysis of top 5-7 players
|
| 64 |
+
- Market share breakdown
|
| 65 |
+
- Competitive strategies
|
| 66 |
+
- Recent developments and initiatives
|
| 67 |
+
- SWOT analysis of major players
|
| 68 |
+
- Barriers to entry analysis
|
| 69 |
+
- Industry concentration metrics
|
| 70 |
+
|
| 71 |
+
3. Technology & Innovation:
|
| 72 |
+
- Current technology trends
|
| 73 |
+
- Innovation patterns
|
| 74 |
+
- Patent analysis
|
| 75 |
+
- R&D investments
|
| 76 |
+
- Emerging technologies
|
| 77 |
+
- Digital transformation trends
|
| 78 |
+
|
| 79 |
+
4. Regulatory & Environmental Factors:
|
| 80 |
+
- Current regulations
|
| 81 |
+
- Upcoming policy changes
|
| 82 |
+
- Environmental considerations
|
| 83 |
+
- Compliance requirements
|
| 84 |
+
- Industry standards
|
| 85 |
+
- Certification needs
|
| 86 |
+
|
| 87 |
+
Requirements:
|
| 88 |
+
- Use multiple sources for each data point
|
| 89 |
+
- Focus on recent data (last 12 months)
|
| 90 |
+
- Include source citations
|
| 91 |
+
- Note confidence levels for projections
|
| 92 |
+
- Identify any data gaps or inconsistencies
|
| 93 |
+
""",
|
| 94 |
agent=researcher,
|
| 95 |
expected_output="Comprehensive research data with verified sources and confidence levels"
|
| 96 |
)
|
| 97 |
|
| 98 |
analysis_task = Task(
|
| 99 |
description="""
|
| 100 |
+
Analyze the research findings and create strategic insights:
|
| 101 |
+
|
| 102 |
+
1. Strategic Analysis:
|
| 103 |
+
- Porter's Five Forces analysis
|
| 104 |
+
- PESTLE analysis
|
| 105 |
+
- Value chain opportunities
|
| 106 |
+
- Growth vectors
|
| 107 |
+
- Market maturity assessment
|
| 108 |
+
- Investment potential
|
| 109 |
+
|
| 110 |
+
2. Trend Analysis:
|
| 111 |
+
- Major market trends
|
| 112 |
+
- Technology impact assessment
|
| 113 |
+
- Consumer behavior shifts
|
| 114 |
+
- Emerging business models
|
| 115 |
+
- Future scenarios
|
| 116 |
+
- Risk assessment
|
| 117 |
+
|
| 118 |
+
3. Strategic Implications:
|
| 119 |
+
- Market entry strategies
|
| 120 |
+
- Growth opportunities
|
| 121 |
+
- Potential threats
|
| 122 |
+
- Success factors
|
| 123 |
+
- Risk mitigation strategies
|
| 124 |
+
- Partnership opportunities
|
| 125 |
+
|
| 126 |
+
Requirements:
|
| 127 |
+
- Provide concrete examples
|
| 128 |
+
- Include case studies
|
| 129 |
+
- Support all conclusions with data
|
| 130 |
+
- Identify key success factors
|
| 131 |
+
- Assess market attractiveness
|
| 132 |
+
""",
|
| 133 |
agent=analyst,
|
| 134 |
expected_output="Strategic analysis with actionable insights",
|
| 135 |
context=[research_task]
|
|
|
|
| 137 |
|
| 138 |
report_task = Task(
|
| 139 |
description="""
|
| 140 |
+
Create a comprehensive market research report:
|
| 141 |
+
|
| 142 |
+
1. Executive Summary (2-3 pages):
|
| 143 |
+
- Key findings
|
| 144 |
+
- Market highlights
|
| 145 |
+
- Strategic implications
|
| 146 |
+
- Recommendations
|
| 147 |
+
- Investment thesis
|
| 148 |
+
|
| 149 |
+
2. Comprehensive Analysis (15-20 pages):
|
| 150 |
+
- Detailed market analysis
|
| 151 |
+
- Competitive landscape
|
| 152 |
+
- Strategic insights
|
| 153 |
+
- Future outlook
|
| 154 |
+
- Risk analysis
|
| 155 |
+
|
| 156 |
+
3. Supporting Materials:
|
| 157 |
+
- Case studies
|
| 158 |
+
- Company profiles
|
| 159 |
+
- Data tables
|
| 160 |
+
- Source citations
|
| 161 |
+
- Methodology notes
|
| 162 |
+
|
| 163 |
+
Format as JSON:
|
| 164 |
+
{
|
| 165 |
+
"exec_summary": {
|
| 166 |
+
"summary": "comprehensive executive overview",
|
| 167 |
+
"market_highlights": "key market metrics and findings",
|
| 168 |
+
"strategic_implications": "main strategic insights",
|
| 169 |
+
"recommendations": "key recommendations"
|
| 170 |
+
},
|
| 171 |
+
"market_analysis": {
|
| 172 |
+
"overview": "detailed market overview section",
|
| 173 |
+
"dynamics": "comprehensive industry dynamics",
|
| 174 |
+
"competitive_landscape": "detailed competitive analysis",
|
| 175 |
+
"strategic_analysis": "in-depth strategic insights"
|
| 176 |
+
},
|
| 177 |
+
"future_outlook": "detailed future projections and implications",
|
| 178 |
+
"appendices": "supporting data and sources",
|
| 179 |
+
"sources": ["source1", "source2", "..."],
|
| 180 |
+
"metrics": {
|
| 181 |
+
"market_size": "current and projected size",
|
| 182 |
+
"growth_rate": "historical and projected growth",
|
| 183 |
+
"market_shares": "key player market shares",
|
| 184 |
+
"key_metrics": "other important metrics"
|
|
|
|
| 185 |
}
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
Requirements:
|
| 189 |
+
- Clear, professional writing
|
| 190 |
+
- Logical flow of information
|
| 191 |
+
- Evidence-based conclusions
|
| 192 |
+
- Actionable recommendations
|
| 193 |
+
- 5-10 credible sources
|
| 194 |
+
- Recent data points
|
| 195 |
+
""",
|
| 196 |
agent=writer,
|
| 197 |
expected_output="Professional report with executive summary and detailed analysis",
|
| 198 |
context=[research_task, analysis_task]
|
| 199 |
)
|
| 200 |
|
|
|
|
| 201 |
return Crew(
|
| 202 |
agents=[researcher, analyst, writer],
|
| 203 |
tasks=[research_task, analysis_task, report_task],
|