from abc import abstractmethod import importlib from typing import List from toolformers.base import Tool class Executor: @abstractmethod def run_routine(self, protocol_id, task_data, tools): pass class UnsafeExecutor(Executor): def run_routine(self, protocol_id, code, task_data, tools : List[Tool]): protocol_id = protocol_id.replace('-', '_').replace('.', '_').replace('/', '_') # TODO: This should be done in a safe, containerized environment spec = importlib.util.spec_from_loader(protocol_id, loader=None) loaded_module = importlib.util.module_from_spec(spec) #spec.loader.exec_module(loaded_module) exec(code, loaded_module.__dict__) for tool in tools: loaded_module.__dict__[tool.name] = tool.as_executable_function() return loaded_module.run(task_data)