import autogen
# Define the function that the MACC agent will execute
def execute_custom_function(input_data):
"""
Custom function that will be executed by the MACC agent
when it receives a message.
"""
print(f"Executing function with input: {input_data}")
# Replace this with your actual function logic
result = f"Function executed successfully. Processed: {input_data}"
return result
# Create a MACC agent that executes functions instead of giving default responses
macc_agent = autogen.ConversableAgent(
name="MACCAgent",
system_message="I am a MACC agent that executes functions when called upon."
)
# Create a function handler that will be triggered when the MACC agent is addressed
def function_execution_handler(message, sender):
"""Handler that executes our custom function when the MACC agent is addressed."""
content = message.get("content", "")
# Check if the message is addressed to the MACC agent
if "MACCAgent" in content:
# Execute the custom function
result = execute_custom_function(content)
return True, result
# If the message isn't for this agent, don't handle it
return False, None
# Register the function handler with the MACC agent
macc_agent.register_reply(
trigger=lambda message, sender: sender != macc_agent, # Only respond to messages from others
reply_func=function_execution_handler
)
# Create a user proxy agent
user_proxy = autogen.UserProxyAgent(
name="User",
human_input_mode="TERMINATE",
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
code_execution_config={"use_docker": False}
)
# Create an assistant agent
assistant = autogen.AssistantAgent(
name="Assistant",
llm_config={
"temperature": 0.7,
"model": "gpt-3.5-turbo",
}
)
# Create a group chat with all three agents
groupchat = autogen.GroupChat(
agents=[user_proxy, assistant, macc_agent],
messages=[],
max_round=10
)
# Create a group chat manager
manager = autogen.GroupChatManager(groupchat=groupchat)
# Start the conversation
user_proxy.initiate_chat(
manager,
message="I need some help with data analysis. MACCAgent, can you process this request?"
)
Would you like me to explain or break down the code?