Support context managers for agent cleanup by tobiasofsn · Pull Request #1422 · huggingface/smolagents (original) (raw)
Support context managers for cleanup of resources used by agents.
- Support context managers like
withstatements. - Support calling the agent's
cleanupmethod directly. - Implement cleanup for the E2B executor. The E2B sandbox would previously stay alive for up to the default timeout of 5 minutes as per the E2B documentation.
Intended usage patterns
With
agent = CodeAgent( model=model, tools=[], executor_type="docker" )
with agent: task = "... some task ..." agent.run(task)
With-As
with CodeAgent(model=model, tools=[], executor_type="docker" ) as agent: task = "... some task ..." agent.run(task)
Try-Finally
agent = CodeAgent( model=model, tools=[], executor_type="docker" )
try: task = "... some task ..." agent.run(task) finally: agent.cleanup()
Manual
agent = CodeAgent( model=model, tools=[], executor_type="docker" )
task = "... some task ..." agent.run(task)
agent.cleanup()