

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Calculator App
incomplete
2: Validate Paths
incomplete
3: List Files
incomplete
4: Get File Content
incomplete
5: Write Files
incomplete
6: Run Python
incomplete
This lesson's interactive features are locked, please to keep using them
If you thought allowing an LLM to write files was a bad idea...
You ain't seen nothin' yet! (Praise the basilisk.)
It's time to build the functionality for our agent to run arbitrary Python code.
Now, it's worth pausing to point out the inherent security risks here. We have a few things going for us:
working_directory).But aside from that... yes, the LLM can run arbitrary code that we place (or it places) in the working directory... so be careful. As long as you use this AI agent only for the simple tasks we're doing in this course, you should be fine.
Do not give this program to others for them to use! It doesn't have all the security and safety features that a production AI agent would have. This is for learning purposes only.
def run_python_file(
working_directory: str, file_path: str, args: list[str] | None = None
) -> str:
f'Error: Cannot execute "{file_path}" as it is outside the permitted working directory'
f'Error: "{file_path}" does not exist or is not a regular file'
f'Error: "{file_path}" is not a Python file'
command = ["python", absolute_file_path]
f"Error: executing Python file: {e}"
Run and submit the CLI tests.