Update agent.py

pull/1182/head
CI-DEV 2 months ago committed by GitHub
parent 1cc1eb406b
commit 0be07159f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1783,11 +1783,11 @@ class Agent:
# Handle REACT tools # Handle REACT tools
if function_name == "action": if function_name == "action":
action_desc = arguments.get("action_description", "") action_desc = arguments.get(
action_result = arguments.get("action_result", "") "action_description", ""
action_msg = (
f"Action: {action_desc}\nResult: {action_result}"
) )
action_result = arguments.get("action_result", "")
action_msg = f"Action: {action_desc}\nResult: {action_result}"
self.short_memory.add( self.short_memory.add(
role="Tool Executor", content=action_msg role="Tool Executor", content=action_msg
@ -1798,10 +1798,10 @@ class Agent:
elif function_name == "subtask_complete": elif function_name == "subtask_complete":
subtask_num = arguments.get("subtask_number", 0) subtask_num = arguments.get("subtask_number", 0)
subtask_summary = arguments.get("subtask_summary", "") subtask_summary = arguments.get(
subtask_msg = ( "subtask_summary", ""
f"Subtask {subtask_num} Complete: {subtask_summary}"
) )
subtask_msg = f"Subtask {subtask_num} Complete: {subtask_summary}"
self.short_memory.add( self.short_memory.add(
role=self.agent_name, content=subtask_msg role=self.agent_name, content=subtask_msg
@ -1814,9 +1814,7 @@ class Agent:
elif function_name == "objective_complete": elif function_name == "objective_complete":
final_summary = arguments.get("final_summary", "") final_summary = arguments.get("final_summary", "")
objective_msg = ( objective_msg = f"Objective Complete!\nFinal Summary: {final_summary}"
f"Objective Complete!\nFinal Summary: {final_summary}"
)
self.short_memory.add( self.short_memory.add(
role=self.agent_name, content=objective_msg role=self.agent_name, content=objective_msg
@ -1868,7 +1866,9 @@ class Agent:
# Add REACT system prompt if not already added # Add REACT system prompt if not already added
if not self.react_on: if not self.react_on:
react_prompt = REACT_SYS_PROMPT + """ react_prompt = (
REACT_SYS_PROMPT
+ """
You are now in REACT workflow mode. Follow this process: You are now in REACT workflow mode. Follow this process:
1. Review the plan and current subtask 1. Review the plan and current subtask
@ -1882,6 +1882,7 @@ Available tools:
- subtask_complete: Mark current subtask as complete - subtask_complete: Mark current subtask as complete
- objective_complete: Mark entire objective as complete (use only when ALL subtasks are done) - objective_complete: Mark entire objective as complete (use only when ALL subtasks are done)
""" """
)
self.short_memory.add( self.short_memory.add(
role="system", content=react_prompt role="system", content=react_prompt
) )
@ -1916,7 +1917,9 @@ Available tools:
max_iterations = 100 # Safety limit max_iterations = 100 # Safety limit
objective_complete = False objective_complete = False
while not objective_complete and loop_count < max_iterations: while (
not objective_complete and loop_count < max_iterations
):
loop_count += 1 loop_count += 1
# Build context for current iteration # Build context for current iteration
@ -1949,7 +1952,7 @@ Remember to use 'subtask_complete' when you finish a subtask, and 'objective_com
# Call LLM with REACT tools # Call LLM with REACT tools
attempt = 0 attempt = 0
success = False success = False
while attempt < self.retry_attempts and not success: while attempt < self.retry_attempts and not success:
try: try:
if img is not None: if img is not None:
@ -1996,15 +1999,19 @@ Remember to use 'subtask_complete' when you finish a subtask, and 'objective_com
# Extract subtask number from response if possible # Extract subtask number from response if possible
# This is a fallback - ideally it comes from the tool call # This is a fallback - ideally it comes from the tool call
for item in ( for item in (
response if isinstance(response, list) else [] response
if isinstance(response, list)
else []
): ):
if isinstance(item, dict): if isinstance(item, dict):
tool_calls = item.get("tool_calls", []) tool_calls = item.get(
"tool_calls", []
)
for tool_call in tool_calls: for tool_call in tool_calls:
if ( if (
tool_call.get("function", {}).get( tool_call.get(
"name" "function", {}
) ).get("name")
== "subtask_complete" == "subtask_complete"
): ):
args = tool_call.get( args = tool_call.get(
@ -2012,7 +2019,9 @@ Remember to use 'subtask_complete' when you finish a subtask, and 'objective_com
).get("arguments", {}) ).get("arguments", {})
if isinstance(args, str): if isinstance(args, str):
try: try:
args = json.loads(args) args = json.loads(
args
)
except: except:
pass pass
if isinstance(args, dict): if isinstance(args, dict):
@ -2067,7 +2076,9 @@ Remember to use 'subtask_complete' when you finish a subtask, and 'objective_com
self.stopping_condition is not None self.stopping_condition is not None
and self._check_stopping_condition(response) and self._check_stopping_condition(response)
): ):
logger.info("Stopping condition met in REACT workflow") logger.info(
"Stopping condition met in REACT workflow"
)
break break
# Restore original tools and context_length # Restore original tools and context_length
@ -2095,7 +2106,9 @@ Remember to use 'subtask_complete' when you finish a subtask, and 'objective_com
if hasattr(self, "context_length"): if hasattr(self, "context_length"):
self.context_length = original_context_length self.context_length = original_context_length
if hasattr(self, "short_memory"): if hasattr(self, "short_memory"):
self.short_memory.context_length = original_context_length self.short_memory.context_length = (
original_context_length
)
raise error raise error
async def run_concurrent(self, task: str, *args, **kwargs): async def run_concurrent(self, task: str, *args, **kwargs):

Loading…
Cancel
Save