Skip to content

Commit fabc59a

Browse files
authored
Fix missing python modules in CodeAgent system prompt (#226)
* fix modules in system prompt + test
1 parent 11a738e commit fabc59a

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/smolagents/agents.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -895,37 +895,32 @@ def __init__(
895895
):
896896
if system_prompt is None:
897897
system_prompt = CODE_SYSTEM_PROMPT
898-
super().__init__(
899-
tools=tools,
900-
model=model,
901-
system_prompt=system_prompt,
902-
grammar=grammar,
903-
planning_interval=planning_interval,
904-
**kwargs,
905-
)
898+
906899
self.additional_authorized_imports = (
907900
additional_authorized_imports if additional_authorized_imports else []
908901
)
909902
self.authorized_imports = list(
910903
set(BASE_BUILTIN_MODULES) | set(self.additional_authorized_imports)
911904
)
912-
if "{{authorized_imports}}" not in self.system_prompt:
905+
if "{{authorized_imports}}" not in system_prompt:
913906
raise AgentError(
914907
"Tag '{{authorized_imports}}' should be provided in the prompt."
915908
)
916-
self.system_prompt = self.system_prompt.replace(
917-
"{{authorized_imports}}",
918-
"You can import from any package you want."
919-
if "*" in self.authorized_imports
920-
else str(self.authorized_imports),
921-
)
922909

923910
if "*" in self.additional_authorized_imports:
924911
self.logger.log(
925912
"Caution: you set an authorization for all imports, meaning your agent can decide to import any package it deems necessary. This might raise issues if the package is not installed in your environment.",
926913
0,
927914
)
928915

916+
super().__init__(
917+
tools=tools,
918+
model=model,
919+
system_prompt=system_prompt,
920+
grammar=grammar,
921+
planning_interval=planning_interval,
922+
**kwargs,
923+
)
929924
if use_e2b_executor and len(self.managed_agents) > 0:
930925
raise Exception(
931926
f"You passed both {use_e2b_executor=} and some managed agents. Managed agents is not yet supported with remote code execution."
@@ -944,6 +939,16 @@ def __init__(
944939
all_tools,
945940
)
946941

942+
def initialize_system_prompt(self):
943+
super().initialize_system_prompt()
944+
self.system_prompt = self.system_prompt.replace(
945+
"{{authorized_imports}}",
946+
"You can import from any package you want."
947+
if "*" in self.authorized_imports
948+
else str(self.authorized_imports),
949+
)
950+
return self.system_prompt
951+
947952
def step(self, log_entry: ActionStep) -> Union[None, Any]:
948953
"""
949954
Perform one step in the ReAct framework: the agent thinks, acts, and observes the result.

tests/test_agents.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
ChatMessageToolCall,
3636
ChatMessageToolCallDefinition,
3737
)
38+
from smolagents.utils import BASE_BUILTIN_MODULES
3839

3940

4041
def get_new_path(suffix="") -> str:
@@ -381,6 +382,12 @@ def test_tool_descriptions_get_baked_in_system_prompt(self):
381382
assert tool.name in agent.system_prompt
382383
assert tool.description in agent.system_prompt
383384

385+
def test_module_imports_get_baked_in_system_prompt(self):
386+
agent = CodeAgent(tools=[], model=fake_code_model)
387+
agent.run("Empty task")
388+
for module in BASE_BUILTIN_MODULES:
389+
assert module in agent.system_prompt
390+
384391
def test_init_agent_with_different_toolsets(self):
385392
toolset_1 = []
386393
agent = CodeAgent(tools=toolset_1, model=fake_code_model)

0 commit comments

Comments
 (0)