Code

cookbook/tools/file_generation_tools.py
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.file_generation import FileGenerationTools
from agno.db.sqlite import SqliteDb

agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    db=SqliteDb(db_file="tmp/test.db"),
    tools=[FileGenerationTools(output_directory="tmp")],  
    description="You are a helpful assistant that can generate files in various formats.",
    instructions=[
        "When asked to create files, use the appropriate file generation tools.",
        "Always provide meaningful content and appropriate filenames.",
        "Explain what you've created and how it can be used.",
    ],
    markdown=True,
)

def process_file_generation_output(response, example_name: str):
    """Process and display the file generation output"""
    print(f"=== {example_name} ===")
    print(response.content)
    if response.files:
        for file in response.files:
            print(f"Generated file: {file.filename} ({file.size} bytes)")
            if file.url:
                print(f"File location: {file.url}")
    print()

if __name__ == "__main__":
    print("File Generation Tool Cookbook Examples")
    print("=" * 50)

    # JSON File Generation Example
    response = agent.run(
        "Create a JSON file containing information about 3 fictional employees with name, position, department, and salary."
    )
    process_file_generation_output(response, "JSON File Generation Example")

    # CSV File Generation Example
    response = agent.run(
        "Create a CSV file with sales data for the last 6 months. Include columns for month, product, units_sold, and revenue."
    )
    process_file_generation_output(response, "CSV File Generation Example")

    # PDF File Generation Example
    response = agent.run(
        "Create a PDF report about renewable energy trends in 2024. Include sections on solar, wind, and hydroelectric power."
    )
    process_file_generation_output(response, "PDF File Generation Example")

    # Text File Generation Example
    response = agent.run(
        "Create a text file with a list of best practices for remote work productivity."
    )
    process_file_generation_output(response, "Text File Generation Example")

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.
python3 -m venv .venv
source .venv/bin/activate
2

Set your API key

export OPENAI_API_KEY=xxx
3

Install libraries

pip install -U openai agno reportlab
4

Run Agent

python cookbook/tools/file_generation_tools.py