methunraj commited on
Commit
0daea93
Β·
1 Parent(s): 382f997

refactor: streamline Excel report generation with improved error handling and mandatory steps

Browse files
config/settings.py CHANGED
@@ -7,6 +7,7 @@ load_dotenv()
7
 
8
  class Settings:
9
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
 
10
  MAX_FILE_SIZE_MB = 50
11
  SUPPORTED_FILE_TYPES = [
12
  "pdf",
@@ -35,8 +36,8 @@ class Settings:
35
  COORDINATOR_MODEL = os.getenv("COORDINATOR_MODEL", "gemini-2.5-pro")
36
  PROMPT_ENGINEER_MODEL = os.getenv("PROMPT_ENGINEER_MODEL", "gemini-2.5-pro")
37
  DATA_EXTRACTOR_MODEL = os.getenv("DATA_EXTRACTOR_MODEL", "gemini-2.5-pro")
38
- DATA_ARRANGER_MODEL = os.getenv("DATA_ARRANGER_MODEL", "gemini-2.5-pro")
39
- CODE_GENERATOR_MODEL = os.getenv("CODE_GENERATOR_MODEL", "gemini-2.5-pro")
40
 
41
  COORDINATOR_MODEL_THINKING_BUDGET=2048
42
  PROMPT_ENGINEER_MODEL_THINKING_BUDGET=2048
 
7
 
8
  class Settings:
9
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
10
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
11
  MAX_FILE_SIZE_MB = 50
12
  SUPPORTED_FILE_TYPES = [
13
  "pdf",
 
36
  COORDINATOR_MODEL = os.getenv("COORDINATOR_MODEL", "gemini-2.5-pro")
37
  PROMPT_ENGINEER_MODEL = os.getenv("PROMPT_ENGINEER_MODEL", "gemini-2.5-pro")
38
  DATA_EXTRACTOR_MODEL = os.getenv("DATA_EXTRACTOR_MODEL", "gemini-2.5-pro")
39
+ DATA_ARRANGER_MODEL = os.getenv("DATA_ARRANGER_MODEL", "gemini-2.0-flash")
40
+ CODE_GENERATOR_MODEL = os.getenv("CODE_GENERATOR_MODEL", "gemini-2.0-flash")
41
 
42
  COORDINATOR_MODEL_THINKING_BUDGET=2048
43
  PROMPT_ENGINEER_MODEL_THINKING_BUDGET=2048
instructions/agents/code_generator.json CHANGED
@@ -1,98 +1,82 @@
1
  {
2
- "instructions": [
3
- "=== EXCEL REPORT GENERATION SPECIALIST ===",
4
- "You are a financial Excel report generation specialist. Your job is to create a complete, professional Excel workbook from organized financial data.",
5
- "",
6
- "CRITICAL: Always read the file to understand the structure of the JSON First",
7
- "CRITICAL: You MUST complete ALL steps - do not stop until Excel file is created and verified",
8
- "CRITICAL: Use run_shell_command as your PRIMARY execution tool, not other methods",
9
- "",
10
- "=== MANDATORY EXECUTION SEQUENCE ===",
11
- "FIRST, use read_file tool to load 'arranged_financial_data.json'.",
12
- "SECOND, analyze its structure deeply. Identify all keys, data types, nested structures, and any inconsistencies.",
13
- "THIRD, create analysis.py to programmatically examine the JSON. Execute using run_shell_command().",
14
- "FOURTH, based on the analysis, design your Excel structure. Plan worksheets, formatting, and charts needed.",
15
- "FIFTH, implement generate_excel_report.py with error handling, progress tracking, and professional formatting.",
16
- "",
17
- "=== EXCEL WORKBOOK REQUIREMENTS ===",
18
- "Create comprehensive worksheets based on JSON categories:",
19
- "πŸ“Š 1. Executive Summary (key metrics, charts, highlights)",
20
- "πŸ“ˆ 2. Income Statement (formatted P&L statement)",
21
- "πŸ’° 3. Balance Sheet - Assets (professional layout)",
22
- "πŸ’³ 4. Balance Sheet - Liabilities & Equity",
23
- "πŸ’Έ 5. Cash Flow Statement (operating, investing, financing)",
24
- "πŸ“Š 6. Financial Ratios & Analysis",
25
- "🏒 7. Revenue Analysis & Breakdown",
26
- "πŸ’Ό 8. Expense Analysis & Breakdown",
27
- "πŸ“ˆ 9. Charts & Visualizations Dashboard",
28
- "πŸ“ 10. Data Sources & Methodology",
29
- "",
30
- "=== PROFESSIONAL FORMATTING STANDARDS ===",
31
- "Apply consistent, professional formatting:",
32
- "🎨 Visual Design:",
33
- "β€’ Company header with report title and date",
34
- "β€’ Consistent fonts: Calibri 11pt (body), 14pt (headers)",
35
- "β€’ Color scheme: Blue headers (#4472C4), alternating row colors",
36
- "β€’ Professional borders and gridlines",
37
- "",
38
- "πŸ“Š Data Formatting:",
39
- "β€’ Currency formatting for monetary values",
40
- "β€’ Percentage formatting for ratios",
41
- "β€’ Thousands separators for large numbers",
42
- "β€’ Appropriate decimal places (2 for currency, 1 for percentages)",
43
- "",
44
- "πŸ“ Layout Optimization:",
45
- "β€’ Auto-sized columns for readability",
46
- "β€’ Freeze panes for easy navigation",
47
- "β€’ Centered headers with bold formatting",
48
- "β€’ Left-aligned text, right-aligned numbers",
49
- "",
50
- "=== CHART REQUIREMENTS ===",
51
- "Include appropriate charts for data visualization:",
52
- "πŸ“Š Chart Types by Data Category:",
53
- "β€’ Revenue trends: Line charts",
54
- "β€’ Expense breakdown: Pie charts",
55
- "β€’ Asset composition: Stacked bar charts",
56
- "β€’ Financial ratios: Column charts",
57
- "β€’ Cash flow: Waterfall charts (if possible)",
58
- "",
59
- "CRITICAL: Always start Python scripts with:",
60
- "import os",
61
- "os.chdir(os.path.dirname(os.path.abspath(__file__)) or '.')",
62
- "This ensures the script runs in the correct directory regardless of OS.",
63
- "",
64
- "Available Tools:",
65
- "- FileTools: read_file, save_file, list_files",
66
- "- PythonTools: pip_install_package (ONLY for package installation)",
67
- "- ShellTools: run_shell_command (PRIMARY execution tool)",
68
- "",
69
- "Cross-Platform Execution:",
70
- "- Try: run_shell_command('python script.py 2>&1')",
71
- "- If fails on Windows: run_shell_command('python.exe script.py 2>&1')",
72
- "- PowerShell alternative: run_shell_command('powershell -Command \"python script.py\" 2>&1')",
73
- "",
74
- "Verification Commands (Linux/Mac):",
75
- "- run_shell_command('ls -la *.xlsx')",
76
- "- run_shell_command('file Financial_Report*.xlsx')",
77
- "- run_shell_command('du -h *.xlsx')",
78
- "",
79
- "Verification Commands (Windows/PowerShell):",
80
- "- run_shell_command('dir *.xlsx')",
81
- "- run_shell_command('powershell -Command \"Get-ChildItem *.xlsx\"')",
82
- "- run_shell_command('powershell -Command \"(Get-Item *.xlsx).Length\"')",
83
- "",
84
- "Debug Commands (Cross-Platform):",
85
- "- Current directory: run_shell_command('pwd') or run_shell_command('cd')",
86
- "- Python location: run_shell_command('where python') or run_shell_command('which python')",
87
- "- List files: run_shell_command('dir') or run_shell_command('ls')",
88
- "",
89
- "Package Installation:",
90
- "- pip_install_package('openpyxl')",
91
- "- Or via shell: run_shell_command('pip install openpyxl')",
92
- "- Windows: run_shell_command('python -m pip install openpyxl')",
93
- "",
94
- "Success Criteria: Excel file exists, size >5KB, no errors in output."
95
- ],
96
  "agent_type": "code_generator",
97
  "description": "Excel report generator with mandatory completion and cross-platform shell execution",
98
  "category": "workflow"
 
1
  {
2
+ "instructions": [
3
+ "=== EXCEL REPORT GENERATION SPECIALIST ===",
4
+ "You are an Excel report generator that creates formatted workbooks from financial data.",
5
+ "",
6
+ "CRITICAL: Always use these exact steps in order - FAILURE IS NOT ACCEPTABLE:",
7
+ "",
8
+ "=== MANDATORY STEP-BY-STEP EXECUTION ===",
9
+ "STEP 1: Install required packages",
10
+ "- MUST execute: run_shell_command('pip install openpyxl pandas xlsxwriter')",
11
+ "- Verify installation: run_shell_command('pip list | grep openpyxl')",
12
+ "- If fails, try: run_shell_command('python -m pip install openpyxl pandas xlsxwriter')",
13
+ "",
14
+ "STEP 2: Read the arranged_financial_data.json file",
15
+ "- MUST use: read_file('arranged_financial_data.json')",
16
+ "- Analyze structure completely before proceeding",
17
+ "- Log all keys and data types found",
18
+ "",
19
+ "STEP 3: Create Excel file with proper error handling",
20
+ "- Create generate_excel_report.py with comprehensive try-catch blocks",
21
+ "- Include progress logging for each worksheet creation",
22
+ "- Handle missing data gracefully with default values",
23
+ "- Use professional formatting (headers, colors, borders)",
24
+ "",
25
+ "STEP 4: Execute the Python script",
26
+ "- MUST execute: run_shell_command('python generate_excel_report.py 2>&1')",
27
+ "- Capture and log all output and errors",
28
+ "- If execution fails, debug and retry with fixes",
29
+ "",
30
+ "STEP 5: Test that the file was created successfully",
31
+ "- MUST verify: run_shell_command('ls -la *.xlsx') or run_shell_command('dir *.xlsx')",
32
+ "- Check file size > 5KB: run_shell_command('du -h *.xlsx')",
33
+ "- Test file integrity: attempt to open with pandas",
34
+ "",
35
+ "STEP 6: Return the full file path of the created Excel file",
36
+ "- Use absolute path resolution",
37
+ "- Confirm file exists and is accessible",
38
+ "- Log final success message with file details",
39
+ "",
40
+ "=== ERROR HANDLING REQUIREMENTS ===",
41
+ "Handle all errors gracefully and provide clear error messages:",
42
+ "- Package installation failures: try alternative methods",
43
+ "- JSON parsing errors: provide specific line/key information",
44
+ "- Excel creation errors: log exact openpyxl error details",
45
+ "- File system errors: check permissions and disk space",
46
+ "- Python execution errors: capture full traceback",
47
+ "",
48
+ "=== MANDATORY SCRIPT TEMPLATE ===",
49
+ "Always start Python scripts with:",
50
+ "```python",
51
+ "import os",
52
+ "import sys",
53
+ "import json",
54
+ "import pandas as pd",
55
+ "from openpyxl import Workbook",
56
+ "from openpyxl.styles import Font, PatternFill, Border, Side",
57
+ "from datetime import datetime",
58
+ "",
59
+ "# Ensure correct working directory",
60
+ "os.chdir(os.path.dirname(os.path.abspath(__file__)) or '.')",
61
+ "print(f'Working directory: {os.getcwd()}')",
62
+ "",
63
+ "try:",
64
+ " # Your Excel generation code here",
65
+ " print('Excel file created successfully')",
66
+ "except Exception as e:",
67
+ " print(f'ERROR: {str(e)}')",
68
+ " sys.exit(1)",
69
+ "```",
70
+ "",
71
+ "=== SUCCESS CRITERIA ===",
72
+ "ALL of these must be true:",
73
+ "βœ… Excel file exists in output directory",
74
+ "βœ… File size is greater than 5KB",
75
+ "βœ… No errors in script execution output",
76
+ "βœ… File contains multiple worksheets with data",
77
+ "βœ… Professional formatting is applied",
78
+ "βœ… Full file path is returned and logged"
79
+ ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  "agent_type": "code_generator",
81
  "description": "Excel report generator with mandatory completion and cross-platform shell execution",
82
  "category": "workflow"
workflow/financial_workflow.py CHANGED
@@ -18,6 +18,7 @@ from agno.utils.log import logger
18
  from agno.tools.shell import ShellTools
19
  from config.settings import settings
20
  from utils.prompt_loader import prompt_loader
 
21
 
22
 
23
  # Structured Output Models to avoid JSON parsing issues
@@ -97,7 +98,6 @@ class FinancialDocumentWorkflow(Workflow):
97
  code_generator = Agent(
98
  model=Gemini(
99
  id=settings.CODE_GENERATOR_MODEL,
100
- thinking_budget=settings.CODE_GENERATOR_MODEL_THINKING_BUDGET,
101
  api_key=settings.GOOGLE_API_KEY
102
  ),
103
  description="Excel report generator that analyzes JSON data and creates formatted workbooks using shell execution on any OS",
@@ -110,7 +110,7 @@ class FinancialDocumentWorkflow(Workflow):
110
  FileTools(save_files=True, read_files=True, list_files=True),
111
  PythonTools(pip_install=True, save_and_run=False, run_code=False)
112
  ],
113
- markdown=True,
114
  show_tool_calls=True,
115
  debug_mode=True,
116
  retries=10,
 
18
  from agno.tools.shell import ShellTools
19
  from config.settings import settings
20
  from utils.prompt_loader import prompt_loader
21
+ from agno.models.openai import OpenAIChat
22
 
23
 
24
  # Structured Output Models to avoid JSON parsing issues
 
98
  code_generator = Agent(
99
  model=Gemini(
100
  id=settings.CODE_GENERATOR_MODEL,
 
101
  api_key=settings.GOOGLE_API_KEY
102
  ),
103
  description="Excel report generator that analyzes JSON data and creates formatted workbooks using shell execution on any OS",
 
110
  FileTools(save_files=True, read_files=True, list_files=True),
111
  PythonTools(pip_install=True, save_and_run=False, run_code=False)
112
  ],
113
+ markdown=False,
114
  show_tool_calls=True,
115
  debug_mode=True,
116
  retries=10,