File size: 863 Bytes
3cad23b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)