from upsonic import Agent, Task
import json
import csv
# Create sample files for demonstration
# 1. CSV file with financial data
with open("financial_data.csv", "w", newline='') as f:
writer = csv.writer(f)
writer.writerow(["Category", "Q1", "Q2", "Q3", "Q4"])
writer.writerow(["Revenue", "100k", "120k", "135k", "150k"])
writer.writerow(["Expenses", "60k", "65k", "70k", "72k"])
# 2. JSON file with raw data
with open("raw_data.json", "w") as f:
json.dump({
"markets": ["US", "EU", "Asia"],
"segments": ["Enterprise", "SMB", "Consumer"],
"growth_rate": {"US": "15%", "EU": "12%", "Asia": "25%"}
}, f, indent=2)
# 3. Python code file for analysis
with open("analysis_helpers.py", "w") as f:
f.write("""
def calculate_growth(current, previous):
return ((current - previous) / previous) * 100
def format_currency(amount):
return f"${amount:,.2f}"
""")
# 4. Markdown report
with open("market_report.md", "w") as f:
f.write("""# Market Analysis Report
## Key Findings
- Strong performance in Asian markets
- Enterprise segment showing consistent growth
- Q4 revenue targets exceeded by 10%
## Recommendations
1. Increase investment in Asia-Pacific region
2. Focus on enterprise customer retention
""")
# Create agent for comprehensive analysis
comprehensive_agent = Agent(
model="openai/gpt-4o-mini",
name="Comprehensive Analyzer",
role="Multi-format data analysis"
)
# Initial research task - analyze market report first
previous_research_task = Task(
description="Review the market report and identify the top 3 key growth areas with specific metrics",
context=["market_report.md"] # Give it the market report to analyze
)
comprehensive_agent.print_do(previous_research_task)
# Task with multiple file types - now uses the previous analysis
comprehensive_task = Task(
description="Create a comprehensive analysis report combining all data sources and validate the growth areas identified in previous analysis",
context=[
"financial_data.csv", # CSV data
"market_report.md", # Markdown document
"raw_data.json", # JSON data
previous_research_task, # Previous task result
"Additional context: Focus on emerging markets and growth opportunities" # Text context
]
)
result = comprehensive_agent.print_do(comprehensive_task)
print(result)