|
| 1 | +""" |
| 2 | +Agent Computer Use Example: Create and Debug a PyTorch Model |
| 3 | +
|
| 4 | +This example demonstrates an agent using the computer_use toolkit to: |
| 5 | +1. Create a PyTorch model file |
| 6 | +2. Run tests to verify it works |
| 7 | +3. Debug and fix any issues over multiple loops |
| 8 | +
|
| 9 | +The agent has access to all computer_use tools: |
| 10 | +- read_file, list_directory, grep_files (read-only) |
| 11 | +- write_file, edit_file, patch_file, delete_file (write) |
| 12 | +- run_command (shell) |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +from swarms import Agent |
| 17 | +from swarms.tools.computer_use import create_computer_use_tools |
| 18 | + |
| 19 | +tools = create_computer_use_tools() |
| 20 | + |
| 21 | + |
| 22 | +agent = Agent( |
| 23 | + agent_name="PyTorch-Debugger", |
| 24 | + system_prompt=""" |
| 25 | +You are an expert Python/PyTorch developer agent. Your task is to create and debug |
| 26 | +a PyTorch model file called `/$HOME/pytorch_model.py`. |
| 27 | +
|
| 28 | +Follow this workflow in each loop: |
| 29 | +
|
| 30 | +1. PLAN: Decide what needs to be done based on current state |
| 31 | +2. CREATE/MODIFY: Use write_file/edit_file to create or fix code |
| 32 | +3. TEST: Use run_command to run `python -c "from examples.models.pytorch_model import *; print('Import OK')"` |
| 33 | +4. FIX: If there are errors, use edit_file to fix them |
| 34 | +5. REPORT: Summarize what was done |
| 35 | +
|
| 36 | +The model should: |
| 37 | +- Define a NeuralNetwork class with at least 3 layers |
| 38 | +- Include forward() method |
| 39 | +- Include a training step function |
| 40 | +- Include an evaluation function |
| 41 | +- Be well-documented with docstrings |
| 42 | +- Handle both CPU and GPU (cuda) devices |
| 43 | +- Have proper error handling |
| 44 | +
|
| 45 | +When you see import errors or runtime errors: |
| 46 | +- Read the model file to understand the code |
| 47 | +- Identify the bug |
| 48 | +- Fix it with edit_file |
| 49 | +- Re-run the test |
| 50 | +
|
| 51 | +Continue until the model imports and runs without errors. |
| 52 | +When fully working, create a simple test that demonstrates the model training. |
| 53 | +
|
| 54 | +Return a final report with: |
| 55 | +- The final working code |
| 56 | +- Test results showing successful training |
| 57 | +- Any bugs that were fixed |
| 58 | +""", |
| 59 | + model_name="gpt-4o", |
| 60 | + max_loops=10, |
| 61 | + tools=list(tools.values()), |
| 62 | +) |
| 63 | + |
| 64 | +result = agent.run(""" |
| 65 | +Create and debug a PyTorch model at `/$HOME/pytorch_model.py`. |
| 66 | +
|
| 67 | +Start by creating the initial model file, then test it. |
| 68 | +If there are errors, fix them. Keep iterating until the model works correctly. |
| 69 | +
|
| 70 | +The model should demonstrate: |
| 71 | +1. A feedforward neural network |
| 72 | +2. Forward pass |
| 73 | +3. Training loop with loss computation |
| 74 | +4. Evaluation mode |
| 75 | +5. GPU/CPU device handling |
| 76 | +""") |
| 77 | + |
| 78 | +print(result) |
0 commit comments