fix(agents): fix ReActAgent (#2588)

This commit is contained in:
Cooper 2025-04-07 13:04:55 +08:00 committed by GitHub
parent 3ceba89102
commit 0fd578cf87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 10 deletions

View File

@ -81,9 +81,12 @@ async def main():
await user_proxy.initiate_chat(
recipient=tool_engineer,
reviewer=user_proxy,
message="Calculate the product of 10 and 99, then count the number of files in /tmp",
# message="Calculate the product of 10 and 99",
# message="Count the number of files in /tmp",
message="Calculate the product of 10 and 99",
)
await user_proxy.initiate_chat(
recipient=tool_engineer,
reviewer=user_proxy,
message="Count the number of files in /tmp",
)
# dbgpt-vis message infos

View File

@ -364,9 +364,8 @@ class ConversableAgent(Role, Agent):
fail_reason = None
current_retry_counter = 0
is_success = True
done = False
observation = received_message.content or ""
while not done and current_retry_counter < self.max_retry_count:
while current_retry_counter < self.max_retry_count:
if current_retry_counter > 0:
a_reply_message: Optional[
AgentMessage
@ -500,6 +499,7 @@ class ConversableAgent(Role, Agent):
action_output=act_out,
check_pass=check_pass,
check_fail_reason=fail_reason,
current_retry_counter=current_retry_counter,
)
else:
# Successful reply
@ -509,6 +509,7 @@ class ConversableAgent(Role, Agent):
ai_message=ai_message,
action_output=act_out,
check_pass=check_pass,
current_retry_counter=current_retry_counter,
)
if self.run_mode != AgentRunMode.LOOP or act_out.terminate:
logger.debug(f"Agent {self.name} reply success!{reply_message}")
@ -1127,7 +1128,7 @@ class ConversableAgent(Role, Agent):
**context,
)
if not user_prompt:
user_prompt = "Observation: "
user_prompt = f"Observation: {observation}"
agent_messages = []
if system_prompt:

View File

@ -226,6 +226,7 @@ class Role(ABC, BaseModel):
action_output: Optional[ActionOutput] = None,
check_pass: bool = True,
check_fail_reason: Optional[str] = None,
current_retry_counter: Optional[int] = None,
) -> AgentMemoryFragment:
"""Write the memories to the memory.

View File

@ -68,14 +68,11 @@ _REACT_USER_TEMPLATE = """\
Most recent message:
{{ most_recent_memories }}
{% endif %}\
{% if question %}\
Question: {{ question }}
{% endif %}
"""
_REACT_WRITE_MEMORY_TEMPLATE = """\
{% if question %}Question: {{ question }} {% endif %}
{% if thought %}Thought: {{ thought }} {% endif %}
{% if action %}Action: {{ action }} {% endif %}
{% if action_input %}Action Input: {{ action_input }} {% endif %}
@ -253,6 +250,7 @@ class ReActAgent(ConversableAgent):
action_output: Optional[ActionOutput] = None,
check_pass: bool = True,
check_fail_reason: Optional[str] = None,
current_retry_counter: Optional[int] = None,
) -> AgentMemoryFragment:
"""Write the memories to the memory.
@ -285,6 +283,9 @@ class ReActAgent(ConversableAgent):
if action_input:
memory_map["action_input"] = action_input
if current_retry_counter is not None and current_retry_counter == 0:
memory_map["question"] = question
write_memory_template = self.write_memory_template
memory_content = self._render_template(write_memory_template, **memory_map)
fragment = AgentMemoryFragment(memory_content)