Prerequisites

  • Python 3.10 or higher
  • PraisonAI Agents package installed
  • e2b_code_interpreter package installed
  • Basic understanding of Python

Quick Start

Create AI agents that can write and execute Python code in a sandboxed environment!

1

Install Packages

First, install the required packages:

pip install praisonaiagents e2b_code_interpreter
2

Import Required Components

Import the necessary components:

from praisonaiagents import Agent, Task, PraisonAIAgents
from e2b_code_interpreter import Sandbox
3

Define Code Interpreter

Create a code interpreter function:

def code_interpreter(code: str):
    print(f"\n{'='*50}\n> Running following AI-generated code:\n{code}\n{'='*50}")
    exec_result = Sandbox().run_code(code)
    if exec_result.error:
        print("[Code Interpreter error]", exec_result.error)
        return {"error": str(exec_result.error)}
    else:
        results = []
        for result in exec_result.results:
            if hasattr(result, '__iter__'):
                results.extend(list(result))
            else:
                results.append(str(result))
        logs = {"stdout": list(exec_result.logs.stdout), "stderr": list(exec_result.logs.stderr)}
        return json.dumps({"results": results, "logs": logs})
4

Create Code Agents

Set up your code writing and execution agents:

code_agent = Agent(
    name="code_agent",
    backstory="Expert in writing Python scripts",
    self_reflect=False
)

execution_agent = Agent(
    name="execution_agent",
    backstory="Expert in executing Python scripts",
    self_reflect=False,
    tools=[code_interpreter]
)

Understanding Code Agents

What are Code Agents?

Code agents are specialized AI agents that can:

  • Write Python code based on requirements
  • Execute code safely in a sandboxed environment
  • Handle code execution results and errors
  • Work together in a pipeline (writer → executor)

Key Components

Code Writer Agent

Specialized in writing Python code

Agent(
    name="code_agent",
    backstory="Expert in writing Python scripts"
)

Code Executor Agent

Executes code using sandbox

Agent(
    name="execution_agent",
    tools=[code_interpreter]
)

Sandbox Environment

Safe code execution environment using e2b

Execution Results

Structured output with results and logs

Examples

Simple Hello World Example

from praisonaiagents import Agent, Task, PraisonAIAgents
from e2b_code_interpreter import Sandbox

# Create agents
code_agent = Agent(
    name="code_agent",
    backstory="Expert in writing Python scripts",
    self_reflect=False
)

execution_agent = Agent(
    name="execution_agent",
    backstory="Expert in executing Python scripts",
    self_reflect=False,
    tools=[code_interpreter]
)

# Define tasks
code_task = Task(
    description="Write a simple Python script to print 'Hello, World!'",
    expected_output="A Python script that prints 'Hello, World!'",
    agent=code_agent
)

execution_task = Task(
    description="Execute the Python script",
    expected_output="The output of the Python script",
    agent=execution_agent
)

# Initialize and run agents
agents = PraisonAIAgents(
    agents=[code_agent, execution_agent],
    tasks=[code_task, execution_task]
)
result = agents.start()

Advanced Code Generation

Best Practices

Common Patterns

Code Generation and Testing

# Generate code
code_task = Task(
    description="Write a function to calculate fibonacci numbers",
    agent=code_agent
)

# Generate test cases
test_task = Task(
    description="Write test cases for the fibonacci function",
    agent=code_agent
)

# Execute tests
execution_task = Task(
    description="Run the test cases",
    agent=execution_agent
)

Interactive Code Development

# Write initial code
initial_code_task = Task(
    description="Write initial version of the script",
    agent=code_agent
)

# Test and improve
improvement_task = Task(
    description="Analyze execution results and improve the code",
    agent=code_agent
)

Troubleshooting

Execution Errors

Check sandbox execution results:

if exec_result.error:
    print("[Error]", exec_result.error)

Code Generation

Provide clear requirements and constraints in task description

Next Steps

Code execution agents provide a powerful way to generate and run Python code safely. They’re perfect for automated code generation, testing, and execution in a controlled environment.

Was this page helpful?