27
27
28
28
from .utils import console , parse_code_blob , parse_json_tool_call , truncate_content
29
29
from .types import AgentAudio , AgentImage
30
- from .default_tools import BASE_PYTHON_TOOLS , FinalAnswerTool
30
+ from .default_tools . base import FinalAnswerTool
31
31
from .llm_engines import HfApiEngine , MessageRole
32
32
from .monitoring import Monitor
33
33
from .prompts import (
42
42
SYSTEM_PROMPT_PLAN_UPDATE ,
43
43
SYSTEM_PROMPT_PLAN ,
44
44
)
45
- from .local_python_executor import LIST_SAFE_MODULES , evaluate_python_code
46
- from .tool import (
45
+ from .local_python_executor import BASE_BUILTIN_MODULES , LocalPythonExecutor
46
+ from .e2b_executor import E2BExecutor
47
+ from .tools import (
47
48
DEFAULT_TOOL_DESCRIPTION_TEMPLATE ,
48
49
Tool ,
49
50
get_tool_description_with_args ,
@@ -169,17 +170,6 @@ def format_prompt_with_managed_agents_descriptions(
169
170
else :
170
171
return prompt_template .replace (agent_descriptions_placeholder , "" )
171
172
172
-
173
- def format_prompt_with_imports (
174
- prompt_template : str , authorized_imports : List [str ]
175
- ) -> str :
176
- if "<<authorized_imports>>" not in prompt_template :
177
- raise AgentError (
178
- "Tag '<<authorized_imports>>' should be provided in the prompt."
179
- )
180
- return prompt_template .replace ("<<authorized_imports>>" , str (authorized_imports ))
181
-
182
-
183
173
class BaseAgent :
184
174
def __init__ (
185
175
self ,
@@ -264,11 +254,6 @@ def initialize_system_prompt(self):
264
254
self .system_prompt = format_prompt_with_managed_agents_descriptions (
265
255
self .system_prompt , self .managed_agents
266
256
)
267
- if hasattr (self , "authorized_imports" ):
268
- self .system_prompt = format_prompt_with_imports (
269
- self .system_prompt ,
270
- list (set (LIST_SAFE_MODULES ) | set (getattr (self , "authorized_imports" ))),
271
- )
272
257
273
258
return self .system_prompt
274
259
@@ -439,9 +424,7 @@ def execute_tool_call(self, tool_name: str, arguments: Dict[str, str]) -> Any:
439
424
tool_name (`str`): Name of the Tool to execute (should be one from self.toolbox).
440
425
arguments (Dict[str, str]): Arguments passed to the Tool.
441
426
"""
442
- available_tools = self .toolbox .tools
443
- if self .managed_agents is not None :
444
- available_tools = {** available_tools , ** self .managed_agents }
427
+ available_tools = {** self .toolbox .tools , ** self .managed_agents }
445
428
if tool_name not in available_tools :
446
429
error_msg = f"Error: unknown tool { tool_name } , should be instead one of { list (available_tools .keys ())} ."
447
430
console .print (f"[bold red]{ error_msg } " )
@@ -674,8 +657,6 @@ def planning_step(self, task, is_first_step: bool, iteration: int):
674
657
),
675
658
managed_agents_descriptions = (
676
659
show_agents_descriptions (self .managed_agents )
677
- if self .managed_agents is not None
678
- else ""
679
660
),
680
661
answer_facts = answer_facts ,
681
662
),
@@ -729,8 +710,6 @@ def planning_step(self, task, is_first_step: bool, iteration: int):
729
710
),
730
711
managed_agents_descriptions = (
731
712
show_agents_descriptions (self .managed_agents )
732
- if self .managed_agents is not None
733
- else ""
734
713
),
735
714
facts_update = facts_update ,
736
715
remaining_steps = (self .max_iterations - iteration ),
@@ -891,6 +870,7 @@ def __init__(
891
870
grammar : Optional [Dict [str , str ]] = None ,
892
871
additional_authorized_imports : Optional [List [str ]] = None ,
893
872
planning_interval : Optional [int ] = None ,
873
+ use_e2b_executor : bool = False ,
894
874
** kwargs ,
895
875
):
896
876
if llm_engine is None :
@@ -909,17 +889,24 @@ def __init__(
909
889
** kwargs ,
910
890
)
911
891
912
- self .python_evaluator = evaluate_python_code
913
892
self .additional_authorized_imports = (
914
893
additional_authorized_imports if additional_authorized_imports else []
915
894
)
895
+ all_tools = {** self .toolbox .tools , ** self .managed_agents }
896
+ if use_e2b_executor :
897
+ self .python_executor = E2BExecutor (self .additional_authorized_imports , list (all_tools .values ()))
898
+ else :
899
+ self .python_executor = LocalPythonExecutor (self .additional_authorized_imports , all_tools )
916
900
self .authorized_imports = list (
917
- set (LIST_SAFE_MODULES ) | set (self .additional_authorized_imports )
901
+ set (BASE_BUILTIN_MODULES ) | set (self .additional_authorized_imports )
918
902
)
903
+ if "{{authorized_imports}}" not in self .system_prompt :
904
+ raise AgentError (
905
+ "Tag '{{authorized_imports}}' should be provided in the prompt."
906
+ )
919
907
self .system_prompt = self .system_prompt .replace (
920
908
"{{authorized_imports}}" , str (self .authorized_imports )
921
909
)
922
- self .custom_tools = {}
923
910
924
911
def step (self , log_entry : ActionStep ) -> Union [None , Any ]:
925
912
"""
@@ -991,22 +978,12 @@ def step(self, log_entry: ActionStep) -> Union[None, Any]:
991
978
)
992
979
993
980
try :
994
- static_tools = {
995
- ** BASE_PYTHON_TOOLS .copy (),
996
- ** self .toolbox .tools ,
997
- }
998
- if self .managed_agents is not None :
999
- static_tools = {** static_tools , ** self .managed_agents }
1000
- output = self .python_evaluator (
981
+ output , execution_logs = self .python_executor (
1001
982
code_action ,
1002
- static_tools = static_tools ,
1003
- custom_tools = self .custom_tools ,
1004
- state = self .state ,
1005
- authorized_imports = self .authorized_imports ,
1006
983
)
1007
- if len (self . state [ "print_outputs" ] ) > 0 :
1008
- console .print (Group (Text ("Print outputs :" , style = "bold" ), Text (self . state [ "print_outputs" ] )))
1009
- observation = "Print outputs :\n " + self . state [ "print_outputs" ]
984
+ if len (execution_logs ) > 0 :
985
+ console .print (Group (Text ("Execution logs :" , style = "bold" ), Text (execution_logs )))
986
+ observation = "Execution logs :\n " + execution_logs
1010
987
if output is not None :
1011
988
truncated_output = truncate_content (
1012
989
str (output )
@@ -1026,7 +1003,7 @@ def step(self, log_entry: ActionStep) -> Union[None, Any]:
1026
1003
console .print (Group (Text ("Final answer:" , style = "bold" ), Text (str (output ), style = "bold green" )))
1027
1004
log_entry .action_output = output
1028
1005
return output
1029
- return None
1006
+
1030
1007
1031
1008
1032
1009
class ManagedAgent :
0 commit comments