Skip to content

Commit dfe7875

Browse files
committed
fix(prompts): render AGENT_SYSTEM_PROMPT_3's Time line per call
The prompt was a module-level f-string, so get_time() ran once at import and every agent was told the process start time. The default parameter in Agent.__init__ froze it a second time, since defaults evaluate at def time. Split the text into timeless header and body, add build_agent_system_prompt() to render the Time line on demand, and resolve the default inside __init__. Fixes kyegomez#2131
1 parent 7a6eeeb commit dfe7875

3 files changed

Lines changed: 66 additions & 4 deletions

File tree

swarms/prompts/agent_system_prompts.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,14 @@ def agent_system_prompt_2(name: str):
127127
return AGENT_SYSTEM_PROMPT_2
128128

129129

130-
AGENT_SYSTEM_PROMPT_3 = f"""
130+
_AGENT_SYSTEM_PROMPT_3_HEADER = """
131131
You are an autonomous agent designed to serve users by automating complex tasks, workflows, and activities with precision and intelligence.
132132
Agents leverage custom instructions, specialized capabilities, and curated data to optimize large language models for specific domains and use cases.
133133
134-
Time: {get_time()}
134+
"""
135+
135136

137+
_AGENT_SYSTEM_PROMPT_3_BODY = """
136138
You possess the ability to engage in both internal reasoning and external interactions to achieve optimal results.
137139
Through self-reflection and user collaboration, you can break down complex problems, identify optimal solutions, and execute tasks with high efficiency.
138140
@@ -145,3 +147,21 @@ def agent_system_prompt_2(name: str):
145147
146148
Always aim to exceed expectations by delivering comprehensive, well-structured, and contextually appropriate solutions that address both the explicit and implicit needs of the task.
147149
"""
150+
151+
152+
AGENT_SYSTEM_PROMPT_3 = (
153+
_AGENT_SYSTEM_PROMPT_3_HEADER + _AGENT_SYSTEM_PROMPT_3_BODY
154+
)
155+
156+
157+
def build_agent_system_prompt() -> str:
158+
"""Render the default agent system prompt with the current time.
159+
160+
Returns:
161+
str: The prompt with a freshly interpolated ``Time:`` line.
162+
"""
163+
return (
164+
_AGENT_SYSTEM_PROMPT_3_HEADER
165+
+ f"Time: {get_time()}\n"
166+
+ _AGENT_SYSTEM_PROMPT_3_BODY
167+
)

swarms/structs/agent.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
from swarms.agents.autonomous_loop import AutonomousAgentLoop
4141
from swarms.agents.llm_manager import LLMManager
4242
from swarms.agents.skills_manager import SkillsManager
43-
from swarms.prompts.agent_system_prompts import AGENT_SYSTEM_PROMPT_3
43+
from swarms.prompts.agent_system_prompts import (
44+
build_agent_system_prompt,
45+
)
4446
from swarms.prompts.autonomous_agent_prompt import (
4547
get_autonomous_agent_prompt,
4648
)
@@ -313,7 +315,7 @@ def __init__(
313315
agent_description: Optional[
314316
str
315317
] = "An autonomous agent that can perform tasks and learn from experience powered by Swarms",
316-
system_prompt: Optional[str] = AGENT_SYSTEM_PROMPT_3,
318+
system_prompt: Optional[str] = None,
317319
llm: Optional[Any] = None,
318320
max_loops: Optional[Union[int, str]] = 1,
319321
stopping_condition: Optional[Callable[[str], bool]] = None,
@@ -429,6 +431,8 @@ def __init__(
429431
self.sop = sop
430432
self.sop_list = sop_list
431433
self.tools = tools
434+
if system_prompt is None:
435+
system_prompt = build_agent_system_prompt()
432436
self.system_prompt = system_prompt or ""
433437
self.agent_name = agent_name
434438
self.agent_description = agent_description
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import re
2+
3+
import swarms.prompts.agent_system_prompts as prompt_module
4+
5+
TIMESTAMP = re.compile(r"Current date and time: [^\n]*")
6+
7+
8+
def _freeze(monkeypatch, value):
9+
monkeypatch.setattr(
10+
prompt_module,
11+
"get_time",
12+
lambda: f"Current date and time: {value}\n",
13+
)
14+
15+
16+
def test_constant_carries_no_timestamp():
17+
assert not TIMESTAMP.search(prompt_module.AGENT_SYSTEM_PROMPT_3)
18+
19+
20+
def test_builder_reflects_the_clock_at_call_time(monkeypatch):
21+
_freeze(monkeypatch, "FIRST")
22+
first = prompt_module.build_agent_system_prompt()
23+
_freeze(monkeypatch, "SECOND")
24+
second = prompt_module.build_agent_system_prompt()
25+
26+
assert "FIRST" in first
27+
assert "SECOND" in second
28+
assert first != second
29+
30+
31+
def test_builder_is_the_constant_plus_a_time_line(monkeypatch):
32+
_freeze(monkeypatch, "ONLY")
33+
built = prompt_module.build_agent_system_prompt()
34+
35+
assert (
36+
built.replace("Time: Current date and time: ONLY\n\n", "")
37+
== prompt_module.AGENT_SYSTEM_PROMPT_3
38+
)

0 commit comments

Comments
 (0)