Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

AutoGPT: Fix prompt state pollution

Reinier van der Leer683257b69739
diff --git a/autogpts/autogpt/autogpt/agents/agent.py b/autogpts/autogpt/autogpt/agents/agent.py
index 05ce17f221..43d39a6c6f 100644
--- a/autogpts/autogpt/autogpt/agents/agent.py
+++ b/autogpts/autogpt/autogpt/agents/agent.py
@@ -108,12 +108,13 @@ class Agent(
def build_prompt(
self,
*args,
- extra_messages: [list[ChatMessage]] = None,
+ extra_messages: Optional[list[ChatMessage]] = None,
include_os_info: Optional[bool] = None,
**kwargs,
) -> ChatPrompt:
- if extra_messages is None:
+ if not extra_messages:
extra_messages = []
+
# Clock
extra_messages.append(
ChatMessage.system(f"The current time and date is {time.strftime('%c')}"),
diff --git a/autogpts/autogpt/autogpt/agents/base.py b/autogpts/autogpt/autogpt/agents/base.py
index c951f20ae6..c8a0636f37 100644
--- a/autogpts/autogpt/autogpt/agents/base.py
+++ b/autogpts/autogpt/autogpt/agents/base.py
@@ -256,9 +256,9 @@ class BaseAgent(Configurable[BaseAgentSettings], ABC):
Params:
cycle_instruction: The final instruction for a thinking cycle
"""
- if extra_commands is None:
+ if not extra_commands:
extra_commands = []
- if extra_messages is None:
+ if not extra_messages:
extra_messages = []
# Apply additions from plugins
diff --git a/autogpts/autogpt/autogpt/agents/features/context.py b/autogpts/autogpt/autogpt/agents/features/context.py
index ff4b7d2365..b91b2a06e9 100644
--- a/autogpts/autogpt/autogpt/agents/features/context.py
+++ b/autogpts/autogpt/autogpt/agents/features/context.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import TYPE_CHECKING, Any
+from typing import TYPE_CHECKING, Any, Optional
if TYPE_CHECKING:
from autogpt.core.prompting import ChatPrompt
@@ -49,9 +49,12 @@ class ContextMixin:
def build_prompt(
self,
*args: Any,
- extra_messages: list[ChatMessage] = [],
+ extra_messages: Optional[list[ChatMessage]] = None,
**kwargs: Any,
) -> ChatPrompt:
+ if not extra_messages:
+ extra_messages = []
+
# Add context section to prompt
if self.context:
extra_messages.insert(
diff --git a/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py b/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py
index 5fc344e3c8..506c2c4126 100644
--- a/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py
+++ b/autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py
@@ -198,7 +198,7 @@ class OneShotAgentPromptStrategy(PromptStrategy):
max_prompt_tokens: int,
count_tokens: Callable[[str], int],
count_message_tokens: Callable[[ChatMessage | list[ChatMessage]], int],
- extra_messages: list[ChatMessage] = [],
+ extra_messages: Optional[list[ChatMessage]] = None,
**extras,
) -> ChatPrompt:
"""Constructs and returns a prompt with the following structure:
@@ -209,12 +209,14 @@ class OneShotAgentPromptStrategy(PromptStrategy):
Params:
cycle_instruction: The final instruction for a thinking cycle
"""
+ if not extra_messages:
+ extra_messages = []
system_prompt = self.build_system_prompt(
- ai_config,
- ai_directives,
- commands,
- include_os_info,
+ ai_config=ai_config,
+ ai_directives=ai_directives,
+ commands=commands,
+ include_os_info=include_os_info,
)
system_prompt_tlength = count_message_tokens(ChatMessage.system(system_prompt))