Skip to content

Commit

Permalink
fix: wrong order of history prompts in ReAct agent mode (#5236)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinomoe authored Jun 15, 2024
1 parent 12c815c commit 4f0488a
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions api/core/agent/cot_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def _organize_historic_prompt_messages(self, current_session_messages: list[Prom
organize historic prompt messages
"""
result: list[PromptMessage] = []
scratchpad: list[AgentScratchpadUnit] = []
scratchpads: list[AgentScratchpadUnit] = []
current_scratchpad: AgentScratchpadUnit = None

self.history_prompt_messages = AgentHistoryPromptTransform(
Expand All @@ -391,13 +391,15 @@ def _organize_historic_prompt_messages(self, current_session_messages: list[Prom

for message in self.history_prompt_messages:
if isinstance(message, AssistantPromptMessage):
current_scratchpad = AgentScratchpadUnit(
agent_response=message.content,
thought=message.content or 'I am thinking about how to help you',
action_str='',
action=None,
observation=None,
)
if not current_scratchpad:
current_scratchpad = AgentScratchpadUnit(
agent_response=message.content,
thought=message.content or 'I am thinking about how to help you',
action_str='',
action=None,
observation=None,
)
scratchpads.append(current_scratchpad)
if message.tool_calls:
try:
current_scratchpad.action = AgentScratchpadUnit.Action(
Expand All @@ -409,24 +411,23 @@ def _organize_historic_prompt_messages(self, current_session_messages: list[Prom
)
except:
pass

scratchpad.append(current_scratchpad)
elif isinstance(message, ToolPromptMessage):
if current_scratchpad:
current_scratchpad.observation = message.content
elif isinstance(message, UserPromptMessage):
result.append(message)

if scratchpad:
if scratchpads:
result.append(AssistantPromptMessage(
content=self._format_assistant_message(scratchpad)
content=self._format_assistant_message(scratchpads)
))
scratchpads = []
current_scratchpad = None

result.append(message)

scratchpad = []

if scratchpad:
if scratchpads:
result.append(AssistantPromptMessage(
content=self._format_assistant_message(scratchpad)
content=self._format_assistant_message(scratchpads)
))

return result

0 comments on commit 4f0488a

Please sign in to comment.