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.

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()