Day 3 - Module 8: Exercises
These exercises are based on the concepts and code presented in Module 8: Society of Agents (AutoGen/MagenticOne) (src/08-society-of-agents/).
Exercise 8.1: Changing the Task (Simple Group)
Goal: Observe how the MagenticOne orchestrator directs different agents based on the initial task.
- Open the
src/08-society-of-agents/simple-group.pyscript. - Locate the line where the chat is initiated:
- Change the initial
taskstring to ask for different information that requires different agents. Examples:task="Where does Dennis live?"(Should primarily involveusers_agentandlocation_agent)task="Who is the current user?"(Should primarily involveusers_agent)
- Run the script (
python simple-group.py). - Observe the conversation flow printed to the console. Does the orchestrator correctly involve the agents relevant to the new task? Does the
summary_agentstill get involved appropriately?
Exercise 8.2: Adding a Weather Agent (Simple Group)
Goal: Practice adding a new agent with a specific capability to the group chat.
- Open the
src/08-society-of-agents/simple-group.pyscript. - Define a weather tool: Add a
get_weather(location: str)function (you can make it return a fixed string like "It is sunny in [location]" for simplicity). - Define a weather agent: Create a new
AssistantAgentnamedweather_agent.- Give it a relevant
description(e.g., "Knows the weather."). - Provide it with the
get_weathertool function. - Use the same
model_clientas the other agents.
- Give it a relevant
- Add agent to group: Include the
weather_agentin theagentslist passed toMagenticOneGroupChat. - Modify the task: Change the initial
taskto include asking about the weather, e.g.,task="What is the weather like where Dennis lives?". - Run the script (
python simple-group.py). - Observe the conversation. Does the orchestrator involve the new
weather_agentafter finding Dennis's location? Does the final summary include the weather information?
Exercise 8.3: Testing Chef Agent Logic (Chef Group)
Goal: Verify if the chef agent correctly handles missing information (allergies) based on its instructions.
- Open the
src/08-society-of-agents/chef-and-group.pyscript. - Locate the
get_medical_historytool function. - Modify the function to return an empty string or a message indicating no known allergies:
- Run the script (
python chef-and-group.py) with the task "I want to have something to eat. What would you recommend?." - Carefully read the conversation flow. Does the
chef_agent, after finding out there are no known allergies from theusers_agent, explicitly ask something like "Do you have any allergies I should be aware of?" before recommending a dish? (This depends on its system prompt instructions being followed correctly by the LLM).
Exercise 8.4: Adding Constraints and preferences into a planning agent
Goal: Understand how a group of agents can collaborate without a moderator. Learn how context agents (location, user preferences) feed into a planning agent (chef). Work with a real agent configuration and extend it with constraints.
-
Add Domain Expertise (10–15 min):
-
Add a new agent:
nutritionist_agent.- Role: Evaluate the nutritional quality of the suggested meal.
- Provide it a tool:
analyze_nutrition(meal: str)— mock this as a function returning “balanced”, “high-fat”, etc.
-
Inject a Constraint (5 min):
-
Modify
users_agentto include a dietary preference, e.g.,"low-carb"or"vegan". -
Ensure the
chef_agentrespects this constraint when generating meal options. -
Optional Extensions:
-
Add a
budget_agentwith aget_cost_estimate(meal)tool. - Let agents negotiate (by reasoning in messages) over trade-offs between cost and nutrition.
Exercise 8.5: Modifying Reasoning Oversight (o1 Group)
Goal: Change the focus of the quality check performed by the reasoning agent.
- Open the
src/08-society-of-agents/o1-with-chef-group.pyscript. - Locate the
check_conversationasync function (which acts as a tool for theconsultation_agent). - Find the prompt string passed to
reasoning_agent.on_messagesinside this function: - Modify this
promptto ask thereasoning_agent(o1-mini) to check for something specific. Examples:prompt = "Review the conversation. Did the chef confirm the user's allergies before recommending a meal? Answer yes or no and explain briefly."prompt = "Analyze the conversation. Was the final meal recommendation appropriate given the available ingredients mentioned earlier?"
- Run the script (
python o1-with-chef-group.py). - Observe the output from the
consultation_agent(which includes the response from thereasoning_agent). Does the feedback now focus specifically on the new check you asked thereasoning_agentto perform?