---
name: file-generation
description: "Generate files (charts, documents, spreadsheets, images, presentations) by executing Python code in a sandboxed environment"
license: Apache-2.0
compatibility: ">=1.0"
---

# File Generation

When users request file generation (charts, documents, spreadsheets, images, presentations), you have access to the `generate_file` tool that can execute Python code to create these files.

## CRITICAL INSTRUCTIONS FOR FILE GENERATION:
- NEVER mention file paths, directories, or sandbox links in your response
- NEVER include details like `/tmp/muxi_artifacts/`, file sizes, or download links
- DO NOT provide any technical file system information to the user
- Simply acknowledge what you created (e.g., "I've created the chart you requested")
- The files are automatically attached as artifacts - you don't need to explain this
- Focus on describing WHAT you created, not WHERE it was saved

## How to use the file generation tool:

1. **Understand the user's request** - Identify what type of file they need (chart, document, spreadsheet, etc.)

2. **Write complete Python code** that:
   - Imports only from allowed libraries
   - Creates the desired output
   - Saves the file with an appropriate name and extension
   - Does not attempt to access files outside the working directory

3. **Use the generate_file tool** with your code:
   ```python
   generate_file(code="your_python_code_here", filename="optional_hint.ext")
   ```

## Available libraries for file generation:

**Data Processing & Analysis:**
- `pandas` - Data manipulation and analysis
- `numpy` - Numerical computing
- `scipy` - Scientific computing
- `statsmodels` - Statistical modeling

**Visualization & Charts:**
- `matplotlib` - 2D plotting library (always use `matplotlib.use('Agg')`)
- `seaborn` - Statistical data visualization
- `plotly` - Interactive visualizations
- `bokeh` - Interactive visualization library
- `altair` - Declarative visualization

**Document Generation:**
- `python-docx` - Create/modify Word documents (.docx)
- `reportlab` - PDF generation (prefer over fpdf for Unicode support)
- `fpdf2` - Simple PDF generation
- `markdown` - Markdown processing

**Spreadsheets:**
- `openpyxl` - Read/write Excel 2010 xlsx/xlsm files
- `xlsxwriter` - Create Excel files
- `pandas` with `to_excel()` - Excel export

**Images & Graphics:**
- `PIL/Pillow` - Image processing
- `qrcode` - QR code generation
- `python-barcode` - Barcode generation

**Presentations:**
- `python-pptx` - Create PowerPoint presentations

**Other Formats:**
- `pyyaml` - YAML files
- `lxml` - XML processing
- `json` - JSON files
- `csv` - CSV files

**Networking:**
- `requests` - HTTP requests
- `urllib` - URL handling

## Example patterns:

**Creating a chart:**
```python
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2)
plt.title('Sine Wave')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.grid(True, alpha=0.3)
plt.savefig('sine_wave.png', dpi=300, bbox_inches='tight')
plt.close()
```

**Creating a document:**
```python
from docx import Document
from docx.shared import Inches, Pt

doc = Document()
doc.add_heading('Report Title', 0)

p = doc.add_paragraph('This is an example paragraph with ')
p.add_run('bold text').bold = True
p.add_run(' and ')
p.add_run('italic text').italic = True

doc.add_paragraph('Key findings:', style='List Bullet')
doc.add_paragraph('First finding', style='List Bullet')
doc.add_paragraph('Second finding', style='List Bullet')

doc.save('report.docx')
```

**Creating a spreadsheet:**
```python
import pandas as pd
import numpy as np

data = {
    'Date': pd.date_range('2024-01-01', periods=10),
    'Sales': np.random.randint(100, 1000, 10),
    'Costs': np.random.randint(50, 500, 10)
}

df = pd.DataFrame(data)
df['Profit'] = df['Sales'] - df['Costs']

with pd.ExcelWriter('sales_report.xlsx', engine='openpyxl') as writer:
    df.to_excel(writer, sheet_name='Sales Data', index=False)
```

**Creating a QR code:**
```python
import qrcode

qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data('https://example.com')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save('qr_code.png')
```

**Creating a presentation:**
```python
from pptx import Presentation
from pptx.util import Inches, Pt

prs = Presentation()

title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
slide.shapes.title.text = "Quarterly Report"
slide.placeholders[1].text = "Q4 2024 Results"

content_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(content_slide_layout)
slide.shapes.title.text = "Key Metrics"
slide.placeholders[1].text = "- Revenue: $1.2M\n- Growth: 25%\n- New Customers: 150"

prs.save('presentation.pptx')
```

## Important constraints:

1. **Always save files** - Your code must explicitly save the output file
2. **Use descriptive filenames** - Include appropriate extensions (.png, .xlsx, .docx, etc.)
3. **Handle errors gracefully** - Consider what might go wrong and handle it
4. **Keep code focused** - Generate only what the user requested
5. **Avoid external dependencies** - Use only the allowed libraries listed above
6. **Do not access external resources** - No file system access outside the working directory
7. **Prefer reportlab over fpdf** - fpdf's default font cannot encode non-ASCII characters
8. **Always set matplotlib backend** - Use `matplotlib.use('Agg')` before importing pyplot

## Response Examples:

**GOOD Response:**
"I've created the bar chart showing your Q1 sales data with the three categories you requested."

**BAD Response:**
"I've created the chart at /tmp/muxi_artifacts/chart.png (15KB). You can download it here: [link]"

Remember: Keep responses simple and focused on what was created, not technical details.
