Skip to content

Commit

Permalink
AutoGPT: Fix prompt state pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
Pwuts committed Oct 7, 2023
1 parent a00d880 commit 683257b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
5 changes: 3 additions & 2 deletions autogpts/autogpt/autogpt/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ def __init__(
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')}"),
Expand Down
4 changes: 2 additions & 2 deletions autogpts/autogpt/autogpt/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ def build_prompt(
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
Expand Down
7 changes: 5 additions & 2 deletions autogpts/autogpt/autogpt/agents/features/context.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -49,9 +49,12 @@ def __init__(self, **kwargs: Any):
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(
Expand Down
12 changes: 7 additions & 5 deletions autogpts/autogpt/autogpt/agents/prompt_strategies/one_shot.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def build_prompt(
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:
Expand All @@ -209,12 +209,14 @@ def build_prompt(
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))

Expand Down

0 comments on commit 683257b

Please sign in to comment.