|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +from semantic_kernel.agents import ( |
| 6 | + Agent, |
| 7 | + ChatCompletionAgent, |
| 8 | + MagenticOrchestration, |
| 9 | + OpenAIAssistantAgent, |
| 10 | + StandardMagenticManager, |
| 11 | +) |
| 12 | +from semantic_kernel.agents.runtime import InProcessRuntime |
| 13 | +from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion |
| 14 | +from semantic_kernel.contents import ChatMessageContent |
| 15 | + |
| 16 | +""" |
| 17 | +The following sample demonstrates how to create a Magentic orchestration with two agents: |
| 18 | +- A Research agent that can perform web searches |
| 19 | +- A Coder agent that can run code using the code interpreter |
| 20 | +
|
| 21 | +Read more about Magentic here: |
| 22 | +https://www.microsoft.com/en-us/research/articles/magentic-one-a-generalist-multi-agent-system-for-solving-complex-tasks/ |
| 23 | +
|
| 24 | +This sample demonstrates the basic steps of creating and starting a runtime, creating |
| 25 | +a Magentic orchestration with two agents and a Magentic manager, invoking the |
| 26 | +orchestration, and finally waiting for the results. |
| 27 | +
|
| 28 | +The Magentic manager requires a chat completion model that supports structured output. |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +async def agents() -> list[Agent]: |
| 33 | + """Return a list of agents that will participate in the Magentic orchestration. |
| 34 | +
|
| 35 | + Feel free to add or remove agents. |
| 36 | + """ |
| 37 | + research_agent = ChatCompletionAgent( |
| 38 | + name="ResearchAgent", |
| 39 | + description="A helpful assistant with access to web search. Ask it to perform web searches.", |
| 40 | + instructions=( |
| 41 | + "You are a Researcher. You find information without additional computation or quantitative analysis." |
| 42 | + ), |
| 43 | + # This agent requires the gpt-4o-search-preview model to perform web searches. |
| 44 | + # Feel free to explore with other agents that support web search, for example, |
| 45 | + # the `OpenAIResponseAgent` or `AzureAIAgent` with bing grounding. |
| 46 | + service=OpenAIChatCompletion(ai_model_id="gpt-4o-search-preview"), |
| 47 | + ) |
| 48 | + |
| 49 | + # Create an OpenAI Assistant agent with code interpreter capability |
| 50 | + client, model = OpenAIAssistantAgent.setup_resources() |
| 51 | + code_interpreter_tool, code_interpreter_tool_resources = OpenAIAssistantAgent.configure_code_interpreter_tool() |
| 52 | + definition = await client.beta.assistants.create( |
| 53 | + model=model, |
| 54 | + name="CoderAgent", |
| 55 | + description="A helpful assistant that writes and executes code to process and analyze data.", |
| 56 | + instructions="You solve questions using code. Please provide detailed analysis and computation process.", |
| 57 | + tools=code_interpreter_tool, |
| 58 | + tool_resources=code_interpreter_tool_resources, |
| 59 | + ) |
| 60 | + coder_agent = OpenAIAssistantAgent( |
| 61 | + client=client, |
| 62 | + definition=definition, |
| 63 | + ) |
| 64 | + |
| 65 | + return [research_agent, coder_agent] |
| 66 | + |
| 67 | + |
| 68 | +def agent_response_callback(message: ChatMessageContent) -> None: |
| 69 | + """Observer function to print the messages from the agents.""" |
| 70 | + print(f"**{message.name}**\n{message.content}") |
| 71 | + |
| 72 | + |
| 73 | +async def main(): |
| 74 | + """Main function to run the agents.""" |
| 75 | + # 1. Create a Magentic orchestration with two agents and a Magentic manager |
| 76 | + # Note, the Standard Magentic manager uses prompts that have been tuned very |
| 77 | + # carefully but it accepts custom prompts for advanced users and scenarios. |
| 78 | + # For even more advanced scenarios, you can subclass the MagenticManagerBase |
| 79 | + # and implement your own manager logic. |
| 80 | + # The standard manager also requires a chat completion model that supports |
| 81 | + # structured output. |
| 82 | + magentic_orchestration = MagenticOrchestration( |
| 83 | + members=await agents(), |
| 84 | + manager=StandardMagenticManager(chat_completion_service=OpenAIChatCompletion()), |
| 85 | + agent_response_callback=agent_response_callback, |
| 86 | + ) |
| 87 | + |
| 88 | + # 2. Create a runtime and start it |
| 89 | + runtime = InProcessRuntime() |
| 90 | + runtime.start() |
| 91 | + |
| 92 | + # 3. Invoke the orchestration with a task and the runtime |
| 93 | + orchestration_result = await magentic_orchestration.invoke( |
| 94 | + task=( |
| 95 | + "I am preparing a report on the energy efficiency of different machine learning model architectures. " |
| 96 | + "Compare the estimated training and inference energy consumption of ResNet-50, BERT-base, and GPT-2 " |
| 97 | + "on standard datasets (e.g., ImageNet for ResNet, GLUE for BERT, WebText for GPT-2). " |
| 98 | + "Then, estimate the CO2 emissions associated with each, assuming training on an Azure Standard_NC6s_v3 VM " |
| 99 | + "for 24 hours. Provide tables for clarity, and recommend the most energy-efficient model " |
| 100 | + "per task type (image classification, text classification, and text generation)." |
| 101 | + ), |
| 102 | + runtime=runtime, |
| 103 | + ) |
| 104 | + |
| 105 | + # 4. Wait for the results |
| 106 | + value = await orchestration_result.get() |
| 107 | + |
| 108 | + print(f"\nFinal result:\n{value}") |
| 109 | + |
| 110 | + # 5. Stop the runtime when idle |
| 111 | + await runtime.stop_when_idle() |
| 112 | + |
| 113 | + """ |
| 114 | + Sample output: |
| 115 | + **ResearchAgent** |
| 116 | + Estimating the energy consumption and associated CO₂ emissions for training and inference of ResNet-50, BERT-base... |
| 117 | +
|
| 118 | + **CoderAgent** |
| 119 | + Here is the comparison of energy consumption and CO₂ emissions for each model (ResNet-50, BERT-base, and GPT-2) |
| 120 | + over a 24-hour period: |
| 121 | +
|
| 122 | + | Model | Training Energy (kWh) | Inference Energy (kWh) | Total Energy (kWh) | CO₂ Emissions (kg) | |
| 123 | + |-----------|------------------------|------------------------|---------------------|---------------------| |
| 124 | + | ResNet-50 | 21.11 | 0.08232 | 21.19232 | 19.50 | |
| 125 | + | BERT-base | 0.048 | 0.23736 | 0.28536 | 0.26 | |
| 126 | + | GPT-2 | 42.22 | 0.35604 | 42.57604 | 39.17 | |
| 127 | +
|
| 128 | + ### Recommendations: |
| 129 | + ... |
| 130 | +
|
| 131 | + **CoderAgent** |
| 132 | + Here are the recalibrated results for energy consumption and CO₂ emissions, assuming a more conservative approach |
| 133 | + for models like GPT-2: |
| 134 | +
|
| 135 | + | Model | Training Energy (kWh) | Inference Energy (kWh) | Total Energy (kWh) | CO₂ Emissions (kg) | |
| 136 | + |------------------|------------------------|------------------------|---------------------|---------------------| |
| 137 | + | ResNet-50 | 21.11 | 0.08232 | 21.19232 | 19.50 | |
| 138 | + | BERT-base | 0.048 | 0.23736 | 0.28536 | 0.26 | |
| 139 | + | GPT-2 (Adjusted) | 42.22 | 0.35604 | 42.57604 | 39.17 | |
| 140 | +
|
| 141 | + ... |
| 142 | +
|
| 143 | + **ResearchAgent** |
| 144 | + Estimating the energy consumption and associated CO₂ emissions for training and inference of machine learning ... |
| 145 | +
|
| 146 | + **ResearchAgent** |
| 147 | + Estimating the energy consumption and CO₂ emissions of training and inference for ResNet-50, BERT-base, and ... |
| 148 | +
|
| 149 | + **CoderAgent** |
| 150 | + Here is the estimated energy use and CO₂ emissions for a full day of operation for each model on an Azure ... |
| 151 | +
|
| 152 | + **ResearchAgent** |
| 153 | + Recent analyses have highlighted the substantial energy consumption and carbon emissions associated with ... |
| 154 | +
|
| 155 | + **CoderAgent** |
| 156 | + Here's the refined estimation for the energy use and CO₂ emissions for optimized models on an Azure ... |
| 157 | +
|
| 158 | + **CoderAgent** |
| 159 | + To provide precise estimates for CO₂ emissions based on Azure's regional data centers' carbon intensity, we need ... |
| 160 | +
|
| 161 | + **ResearchAgent** |
| 162 | + To refine the CO₂ emission estimates for training and inference of ResNet-50, BERT-base, and GPT-2 on an Azure ... |
| 163 | +
|
| 164 | + **CoderAgent** |
| 165 | + Here's the refined comparative table for energy consumption and CO₂ emissions for ResNet-50, BERT-base, and GPT-2, |
| 166 | + taking into account carbon intensity data for Azure's West Europe and Sweden Central regions: |
| 167 | +
|
| 168 | + | Model | Energy (kWh) | CO₂ Emissions West Europe (kg) | CO₂ Emissions Sweden Central (kg) | |
| 169 | + |------------|--------------|--------------------------------|-----------------------------------| |
| 170 | + | ResNet-50 | 5.76 | 0.639 | 0.086 | |
| 171 | + | BERT-base | 9.18 | 1.019 | 0.138 | |
| 172 | + | GPT-2 | 12.96 | 1.439 | 0.194 | |
| 173 | +
|
| 174 | + **Refined Recommendations:** |
| 175 | +
|
| 176 | + ... |
| 177 | +
|
| 178 | + Final result: |
| 179 | + Here is the comprehensive report on energy efficiency and CO₂ emissions for ResNet-50, BERT-base, and GPT-2 models |
| 180 | + when trained and inferred on an Azure Standard_NC6s_v3 VM for 24 hours. |
| 181 | +
|
| 182 | + ### Energy Consumption and CO₂ Emissions: |
| 183 | +
|
| 184 | + Based on refined analyses, here are the estimated energy consumption and CO₂ emissions for each model: |
| 185 | +
|
| 186 | + | Model | Energy (kWh) | CO₂ Emissions West Europe (kg) | CO₂ Emissions Sweden Central (kg) | |
| 187 | + |------------|--------------|--------------------------------|-----------------------------------| |
| 188 | + | ResNet-50 | 5.76 | 0.639 | 0.086 | |
| 189 | + | BERT-base | 9.18 | 1.019 | 0.138 | |
| 190 | + | GPT-2 | 12.96 | 1.439 | 0.194 | |
| 191 | +
|
| 192 | + ### Recommendations for Energy Efficiency: |
| 193 | +
|
| 194 | + ... |
| 195 | + """ |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + asyncio.run(main()) |
0 commit comments