Skip to content

Commit 261b472

Browse files
authored
fix add functions to agent(#4937) (#5070)
fix bug: in class AgentBuilder~build() add function(list_of_functions to agent,the return agent name is ‘None’,and the app crashed ## Related issue number #4937
1 parent 37efa75 commit 261b472

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

autogen/agentchat/contrib/agent_builder.py

+22-20
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class AgentBuilder:
182182
{agent_details}
183183
184184
Hint:
185-
# Only respond with the name of the agent that is most suited to execute the function and nothing else.
185+
# Only respond with the name of the agent that is most suited to execute the function and nothing else.if there is no agent that is most suited to execute the function, respond with exactly "None"
186186
"""
187187

188188
UPDATED_AGENT_SYSTEM_MESSAGE = """
@@ -730,7 +730,7 @@ def _build_agents(
730730

731731
if list_of_functions:
732732
for func in list_of_functions:
733-
resp = (
733+
perferred_agent_name = (
734734
self.builder_model.create(
735735
messages=[
736736
{
@@ -747,28 +747,30 @@ def _build_agents(
747747
.choices[0]
748748
.message.content
749749
)
750+
if perferred_agent_name in self.agent_procs_assign.keys():
751+
autogen.agentchat.register_function(
752+
func["function"],
753+
caller=self.agent_procs_assign[perferred_agent_name][0],
754+
executor=agent_list[0],
755+
name=func["name"],
756+
description=func["description"],
757+
)
750758

751-
autogen.agentchat.register_function(
752-
func["function"],
753-
caller=self.agent_procs_assign[resp][0],
754-
executor=agent_list[0],
755-
name=func["name"],
756-
description=func["description"],
757-
)
758-
759-
agents_current_system_message = [
760-
agent["system_message"] for agent in agent_configs if agent["name"] == resp
761-
][0]
759+
agents_current_system_message = [
760+
agent["system_message"] for agent in agent_configs if agent["name"] == perferred_agent_name
761+
][0]
762762

763-
self.agent_procs_assign[resp][0].update_system_message(
764-
self.UPDATED_AGENT_SYSTEM_MESSAGE.format(
765-
agent_system_message=agents_current_system_message,
766-
function_name=func["name"],
767-
function_description=func["description"],
763+
self.agent_procs_assign[perferred_agent_name][0].update_system_message(
764+
self.UPDATED_AGENT_SYSTEM_MESSAGE.format(
765+
agent_system_message=agents_current_system_message,
766+
function_name=func["name"],
767+
function_description=func["description"],
768+
)
768769
)
769-
)
770770

771-
print(f"Function {func['name']} is registered to agent {resp}.")
771+
print(f"Function {func['name']} is registered to agent {perferred_agent_name}.")
772+
else:
773+
logger.debug(f"Function {func['name']} is not registered to any agent.")
772774

773775
return agent_list, self.cached_configs.copy()
774776

0 commit comments

Comments
 (0)