microsoft / autogen

A programming framework for agentic AI. Discord: https://aka.ms/autogen-dc. Roadmap: https://aka.ms/autogen-roadmap
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
29.88k stars 4.35k forks source link

Autogen tries to call non-existent functions that are the names of agents #781

Open MikeBirdTech opened 8 months ago

MikeBirdTech commented 8 months ago

Autogen is trying to call functions that are the names of agents but don't exist.

No agent can execute the function document_writer

There's no function called document_writer but there's an agent called document_writer

Prompt: "Who should read this paper: https://arxiv.org/abs/2308.08155"

Functions:

def exec_python(cell):
    ipython = get_ipython()
    result = ipython.run_cell(cell)
    log = str(result.result)
    if result.error_before_exec is not None:
        log += f"\n{result.error_before_exec}"
    if result.error_in_exec is not None:
        log += f"\n{result.error_in_exec}"
    return log

def exec_sh(script):
    return user_proxy.execute_code_blocks([("sh", script)])

# Register functions
user_proxy.register_function(
    function_map={
        "python": exec_python,
        "sh": exec_sh,
    }
)

Agents:

user_proxy = autogen.UserProxyAgent(
    name="User_proxy",
    human_input_mode="ALWAYS",
    max_consecutive_auto_reply=5,
    is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
    code_execution_config={"work_dir": "coding"},
    llm_config=llm_config,
)

document_writer = autogen.AssistantAgent(
    name="document_writer",
    system_message=document_writer_message,
    llm_config=llm_config,
)

Logs:

***** Suggested function Call: python *****
Arguments: 
{
  "cell": "import requests\nfrom bs4 import BeautifulSoup\n\n# paper url\nurl = 'https://arxiv.org/abs/2308.08155'\n\n# send request\nresponse = requests.get(url)\n\n# create soup\nsoup = BeautifulSoup(response.text, 'html.parser')\n\n# find all 'blockquote' tags - the abstract is in the second one\nabstract = soup.find_all('blockquote')[1].text\n\nabstract"
}
*******************************************

--------------------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/<USER>/.../groupchat.py", line 136, in <module>
    user_proxy.initiate_chat(
  File "/opt/homebrew/anaconda3/envs/.../lib/python3.11/site-packages/autogen/agentchat/conversable_agent.py", line 531, in initiate_chat
    self.send(self.generate_init_message(**context), recipient, silent=silent)
  File "/opt/homebrew/anaconda3/envs/.../lib/python3.11/site-packages/autogen/agentchat/conversable_agent.py", line 334, in send
    recipient.receive(message, self, request_reply, silent)
  File "/opt/homebrew/anaconda3/envs/.../lib/python3.11/site-packages/autogen/agentchat/conversable_agent.py", line 462, in receive
    reply = self.generate_reply(messages=self.chat_messages[sender], sender=sender)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/anaconda3/envs/.../lib/python3.11/site-packages/autogen/agentchat/conversable_agent.py", line 781, in generate_reply
    final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/anaconda3/envs/.../lib/python3.11/site-packages/autogen/agentchat/groupchat.py", line 162, in run_chat
    speaker = groupchat.select_speaker(speaker, self)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/anaconda3/envs/.../lib/python3.11/site-packages/autogen/agentchat/groupchat.py", line 78, in select_speaker
    raise ValueError(
ValueError: No agent can execute the function document_writer. Please check the function_map of the agents.
sonichi commented 8 months ago

@kevin666aa are you aware of this issue? There seem to be two problems:

  1. the function is registered in user_proxy but not found
  2. the error message is wrong.
yiranwu0 commented 8 months ago

For 2. this is a mist print in groupchat. I will fix that.

                    raise ValueError(
                        f"No agent can execute the function {self.messages[-1]['name']}. " 
                        "Please check the function_map of the agents."
                    )
# should be self.messages[-1]["function_call"]["name"]

Still looking into the issues

yiranwu0 commented 8 months ago

@Arrendy I need to know a bit more about this run. Can you print this after initialize groupchat:

for agent in groupchat.agents:
    print(agent.name, agent._function_map)

after running this code and getting the error?

print(groupchat.messages[-1])
for agent in groupchat.agents:
    print(agent.name, agent._function_map)
    if agent.can_execute_function(groupchat.messages[-1]["function_call"]["name"]):
        print(f'{agent.name} can execute this function', groupchat.messages[-1]["function_call"]["name"])
stefan-beskow-aera commented 8 months ago

I'm also getting this error when using a group chat with multiple agents. Is there anything I can provide that would help diagnose this problem?

xyin-anl commented 7 months ago

I am encountering the same issue here. The function is registered using llm_config but the function_map is empty and it returns that "No agent can execute the function ...". Let me know what I can provide to debug this issue.

ekzhu commented 7 months ago

Registering in the llm_config only provides the agent with awareness of the function. To allow an agent to execute the function, you also need to manually add it to function_map. The latest version in the main branch also supports decorator syntax for doing this. See https://microsoft.github.io/autogen/docs/reference/agentchat/conversable_agent/#register_for_llm. Make sure you clone the repo for the latest code. The next release to Pypi should cover this functionality.

thinkall commented 2 months ago

@MikeBirdTech , have you tried with the latest version?