microsoft / autogen

A programming framework for agentic AI 🤖
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
34.65k stars 5.01k forks source link

AgentAssistant Tool Execution Loop Model Client Call should include System_Message #4308

Open gziz opened 6 hours ago

gziz commented 6 hours ago

What happened?

The AssistantAgent determines when it needs to use tools. After all necessary tools are used during the tool execution while loop the agent makes one model_client call to generate a response based on the tool results. The issue is that this model client call doesn't include the system message.

Example

Consider this setup:

math_agent = AssistantAgent(...)  # Responsible for computations
web_search_agent = AssistantAgent(
    "WebSearchAgent",
    tools=[search_tool],
    system_message="You are a web search agent. Do not make any calculations on your findings.",
)
# Note the system message to not make any calculations
team.run_stream(task="Get me the percentage change of XYZ stock price from 2023 to 2024")

Here’s what happens step by step:

  1. The WebSearchAgent is assigned the task of finding the stock prices for 2023 and 2024.
  2. The WebSearchAgent recognizes that tools are needed and enters the tool execution while loop.
  3. The WebSearchAgent finds the prices using the tool execution while loop.
  4. Since the system message is not included, the WebSearchAgent attempts to compute the percentage change on its own, which it shouldn't.

Fix

Before: https://github.com/microsoft/autogen/blob/eb67e4ac93ea85621e6a604c95d38d47104a73b8/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py#L282-L284

After:

result = await self._model_client.create(
    self._system_messages + self._model_context, tools=self._tools + self._handoff_tools, cancellation_token=cancellation_token
)

What did you expect to happen?

It should include the system message

AutoGen version

0.4

Which package was this bug in

AgentChat

Model used

No response

Python version

No response

Operating system

No response

Any additional info you think would be helpful for fixing this bug

No response