Skip to content

Commit

Permalink
Add summary to agent logs
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-roucher committed Aug 30, 2024
1 parent ac933dc commit 8ed5ebd
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/transformers/agents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,11 +1136,12 @@ def step(self):


class ManagedAgent:
def __init__(self, agent, name, description, additional_prompting=None):
def __init__(self, agent, name, description, additional_prompting=None, provide_run_summary=False):
self.agent = agent
self.name = name
self.description = description
self.additional_prompting = additional_prompting
self.provide_run_summary = provide_run_summary

def write_full_task(self, task):
full_task = f"""You're a helpful agent named '{self.name}'.
Expand All @@ -1164,4 +1165,30 @@ def write_full_task(self, task):

def __call__(self, task, **kwargs):
full_task = self.write_full_task(task)
return self.agent.run(full_task, **kwargs)
output = self.agent.run(full_task, **kwargs)
if self.provide_run_summary:
answer = f"Here is the report from the work of your managed agent '{self.name}':\n"
for message in self.agent.write_inner_memory_from_logs(summary_mode=True):
content = message["content"]
if "tool_arguments" in str(content):
if len(str(content)) < 1000 or "[FACTS]" in str(content):
answer += "" + str(content) + "\n"
else:
try:
answer += f"{json.loads(content)['tool_name']}\n"
except Exception:
answer += f"{content[:1000]}(...)\n"
else:
if len(str(content)) > 2000:
answer += (
">>> Tool output too long to show, showing only the beginning:\n"
+ str(content)[:500]
+ "\n(...)\n\n"
)
else:
answer += ">>> " + str(content) + "\n\n"
answer += "\nNow here is the managed agent's final answer deducted from the above steps:\n"
answer += str(output)
return answer
else:
return output

0 comments on commit 8ed5ebd

Please sign in to comment.