8000 GitHub - alvinhenrick/agents: Python sandbox template for experimenting with AI agents. Β· GitHub
[go: up one dir, main page]

Skip to content
alvinhenrickPublic

About

Python sandbox template for experimenting with AI agents.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Coding Agents Framework

Modular framework for building autonomous coding agents with E2B sandboxes and OpenAI.

πŸš€ Quick Start

1. Install

# Install as package (recommended)
pip install -e .

# Or install dependencies only
pip install -r requirements.txt

2. Configure Environment

cp .env.example .env
# Edit .env and add your API keys:
# OPENAI_API_KEY=your_key_here
# E2B_API_KEY=your_key_here

Get API keys:

3. Run Examples

python examples/simple_example.py
python examples/sandbox_example.py
python examples/web_builder_example.py

πŸ“ Project Structure

agents/
β”œβ”€β”€ src/agents/              # Main package βœ… USE THIS
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ core/                # Core functionality
β”‚   β”‚   β”œβ”€β”€ agent.py         # CodingAgent class
β”‚   β”‚   β”œβ”€β”€ tools.py         # Tool implementations
β”‚   β”‚   └── sandbox.py       # E2B sandbox utilities
β”‚   └── use_cases/           # Pre-configured agents
β”‚       └── presets.py       # 5 agent types
β”‚
β”œβ”€β”€ examples/                # Ready-to-run examples
β”‚   β”œβ”€β”€ simple_example.py
β”‚   β”œβ”€β”€ sandbox_example.py
β”‚   └── web_builder_example.py
β”‚
β”œβ”€β”€ tests/                   # Test suite
β”‚   └── test_tools.py
β”‚
β”œβ”€β”€ docs.md                  # E2B/OpenAI API reference
β”œβ”€β”€ project.ipynb            # Jupyter notebook
β”œβ”€β”€ requirements.txt
└── setup.py

πŸ’‘ Usage

Import as Package

from agents import CodingAgent, ToolRegistry, SandboxManager
from agents import create_data_analyzer, create_web_builder

Basic Example

from openai import OpenAI
from agents import CodingAgent, ToolRegistry

client = OpenAI()

# Create tools
registry = ToolRegistry(use_sandbox=False)
registry.register_execute_code()

# Create agent
agent = CodingAgent(
    client=client,
    tools=registry.get_tools(),
    tool_schemas=registry.get_schemas(),
    system_prompt="You are a helpful Python assistant.",
    max_steps=10
)

# Run query
messages, metadata = agent.run("Create a fibonacci function")

With E2B Sandbox

from openai import OpenAI
from agents import create_data_analyzer, SandboxManager, display_images_from_metadata

client = OpenAI()
agent = create_data_analyzer(client)

with SandboxManager() as sbx:
    messages, metadata = agent.run(
        "Generate 100 random numbers and create a histogram",
        sbx=sbx
    )

    # Display generated charts (in Jupyter)
    display_images_from_metadata(metadata)

Web Builder Example

from openai import OpenAI
from agents import create_web_builder, SandboxManager, display_web_app

client = OpenAI()
agent = create_web_builder(client)

with SandboxManager() as sbx:
    agent.run("Create a calculator app", sbx=sbx)
    display_web_app(sbx)  # Opens in Jupyter

πŸ€– Pre-Built Agents

5 ready-to-use agent types:

from agents import (
    create_data_analyzer,      # Data analysis & visualization
    create_web_builder,        # HTML/CSS/JS applications
    create_code_tutor,         # Programming education
    create_data_scientist,     # ML & statistical analysis
    create_devops_agent,       # Automation & deployment
)

Data Analyzer

  • Generate synthetic datasets
  • Statistical analysis
  • Create visualizations (matplotlib, seaborn)
  • Process CSV/JSON data

Web Builder

  • Build interactive web apps
  • HTML/CSS/JS development
  • Responsive designs
  • Serve apps in sandbox

Code Tutor

  • Explain programming concepts
  • Demonstrate with examples
  • Debug and explain errors
  • Teaching best practices

Data Scientist

  • Train ML models
  • Feature engineering
  • Hypothesis testing
  • Model evaluation

DevOps Agent

  • Write automation scripts
  • Set up environments
  • Create deployment pipelines
  • System monitoring

πŸ› οΈ Core Modules

agents.core.agent

CodingAgent - Main agent with iterative loop

  • Conversation memory
  • Tool execution
  • Configurable max steps
  • Verbose output option

simple_agent_loop() - Functional alternative

agents.core.tools

ToolRegistry - Manage tool configurations

  • register_execute_code() - Python code execution
  • register_write_file() - File writing
  • register_read_file() - File reading (sandbox only)

execute_tool() - Execute tools by name

agents.core.sandbox

SandboxManager - E2B sandbox lifecycle

  • Context manager (auto cleanup)
  • Create/connect/kill sandboxes
  • Query by metadata

SandboxCodeExecutor - Code execution wrapper

Utilities:

  • display_images_from_metadata() - Show charts
  • serve_web_app() - Start HTTP server
  • display_web_app() - Display in IFrame

🎯 Custom Agent

from openai import OpenAI
from agents import CodingAgent, ToolRegistry, SandboxManager

client = OpenAI()

# Custom system prompt
system_prompt = """You are a Python expert specializing in algorithms.
Help users implement efficient data structures."""

# Create custom tools
registry = ToolRegistry(use_sandbox=True)
registry.register_execute_code()
registry.register_write_file()

# Build agent
agent = CodingAgent(
    client=client,
    tools=registry.get_tools(),
    tool_schemas=registry.get_schemas(),
    system_prompt=system_prompt,
    max_steps=10,
    verbose=True
)

# Use it
with SandboxManager() as sbx:
    agent.run("Implement a binary search tree", sbx=sbx)

πŸ”§ Advanced Features

Sandbox Management

from agents import SandboxManager

# Create with custom settings
manager = SandboxManager(
    timeout=7200,  # 2 hours
    metadata={"project": "my-app"}
)

# List running sandboxes
sandboxes = SandboxManager.list_running()

# Find by metadata
sandboxes = SandboxManager.find_by_metadata({"project": "my-app"})

# Reconnect to existing
manager.connect("sbx-abc123")

Conversation Memory

agent = CodingAgent(...)

# Multiple queries with context
with SandboxManager() as sbx:
    agent.run("Create a function to add numbers", sbx=sbx)
    agent.run("Now modify it to handle strings", sbx=sbx)
    agent.run("Add error handling", sbx=sbx)

# Reset memory
agent.reset()

Manual Tool Execution

from agents import execute_tool

result, metadata = execute_tool(
    name="execute_code",
    args='{"code": "print(\'Hello\')"}',
    tools=registry.get_tools(),
    sbx=sandbox
)

πŸ§ͺ Testing

# Run tests
python -m pytest tests/

# Run specific test
python tests/test_tools.py

# With verbose output
pytest tests/ -v

πŸ“š Documentation

  • API Reference: See docstrings in source code
  • E2B + OpenAI Docs: docs.md
  • Examples: examples/ directory
  • Jupyter Notebook: project.ipynb

πŸ› Troubleshooting

Import Errors

pip install --upgrade e2b-code-interpreter openai python-dotenv

API Key Issues

  • Check .env file exists with valid keys
  • Verify keys are not expired
  • Use load_dotenv() before creating clients

Sandbox Issues

  • Increase timeout: SandboxManager(timeout=7200)
  • Kill unused sandboxes: manager.kill()
  • List running: SandboxManager.list_running()

Image Display

  • Requires Jupyter notebook
  • Install: pip install ipython jupyter

πŸŽ“ Use Case Ideas

Data Analysis: Synthetic datasets, statistical analysis, visualizations, data processing

Web Development: Interactive apps, landing pages, form validators, responsive layouts

Education: Teaching concepts, demonstrating algorithms, debugging, best practices

Data Science: ML models, clustering, feature engineering, model evaluation

DevOps: Automation scripts, environment setup, CI/CD pipelines, monitoring

🀝 Contributing

Extend the framework:

  • Add new tools in src/agents/core/tools.py
  • Create agent presets in src/agents/use_cases/presets.py
  • Add examples in examples/
  • Write tests in tests/

πŸ“„ License

MIT License - free to use and modify for your projects.

About

Python sandbox template for experimenting with AI agents.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0