|
| 1 | +# Tutorial |
| 2 | + |
| 3 | +> [!TIP] |
| 4 | +> If you'd prefer to just see the code the entire sample is available as a [project here](https://github.com/microsoft/autogen/tree/main/dotnet/samples/GettingStarted). |
| 5 | +
|
| 6 | +In this tutorial we are going to define two agents, `Modifier` and `Checker`, that will count down from 10 to 1. The `Modifier` agent will modify the count and the `Checker` agent will check the count and stop the application when the count reaches 1. |
| 7 | + |
| 8 | +## Defining the message types |
| 9 | + |
| 10 | +The first thing we need to do is to define the messages that will be passed between the agents, we're simply going to define them as classes. |
| 11 | + |
| 12 | +We're going to use `CountMessage` to pass the current count and `CountUpdate` to pass the updated count. |
| 13 | + |
| 14 | +[!code-csharp[](../../../dotnet/samples/GettingStarted/CountMessage.cs#snippet_CountMessage)] |
| 15 | +[!code-csharp[](../../../dotnet/samples/GettingStarted/CountUpdate.cs#snippet_CountUpdate)] |
| 16 | + |
| 17 | +By separating out the message types into strongly typed classes, we can build a workflow where agents react to certain types and produce certain types. |
| 18 | + |
| 19 | +## Creating the agents |
| 20 | + |
| 21 | +### Inherit from `BaseAgent` |
| 22 | + |
| 23 | +In AutoGen an agent is a class that can receive and send messages. The agent defines its own logic of what to do with the messages it receives. To define an agent, create a class that inherits from @Microsoft.AutoGen.Core.BaseAgent, like so: |
| 24 | + |
| 25 | +```csharp |
| 26 | +using Microsoft.AutoGen.Contracts; |
| 27 | +using Microsoft.AutoGen.Core; |
| 28 | + |
| 29 | +public class Modifier( |
| 30 | + AgentId id, |
| 31 | + IAgentRuntime runtime, |
| 32 | + ) : |
| 33 | + BaseAgent(id, runtime, "MyAgent", null), |
| 34 | +{ |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +We will see how to pass arguments to an agent when it is constructed, but for now you just need to know that @Microsoft.AutoGen.Contracts.AgentId and @Microsoft.AutoGen.Core.IAgentRuntime will always be passed to the constructor, and those should be forwarded to the base class constructor. The other two arguments are a description of the agent and an optional logger. |
| 39 | + |
| 40 | +Learn more about what an Agent ID is [here](https://microsoft.github.io/autogen/stable/user-guide/core-user-guide/core-concepts/agent-identity-and-lifecycle.html#agent-id). |
| 41 | + |
| 42 | +### Create a handler |
| 43 | + |
| 44 | +Now, we want `Modifier` to receive `CountMessage` and produce `CountUpdate` after it modifies the count. To do this, we need to implement the `IHandle<CountMessage>` interface: |
| 45 | + |
| 46 | +```csharp |
| 47 | +public class Modifier( |
| 48 | + // ... |
| 49 | + ) : |
| 50 | + BaseAgent(...), |
| 51 | + IHandle<CountMessage> |
| 52 | +{ |
| 53 | + |
| 54 | + public async ValueTask HandleAsync(CountMessage item, MessageContext messageContext) |
| 55 | + { |
| 56 | + // ... |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Add a subscription |
| 62 | + |
| 63 | +We've defined a function that will be called when a `CountMessage` is delivered to this agent, but there is still one step before the message will actually be delivered to the agent. The agent must subscribe to the topic to the message is published to. We can do this by adding the `TypeSubscription` attribute to the class: |
| 64 | + |
| 65 | +```csharp |
| 66 | +[TypeSubscription("default")] |
| 67 | +public class Modifier( |
| 68 | + // ... |
| 69 | +``` |
| 70 | + |
| 71 | +Learn more about topics and subscriptions [here](https://microsoft.github.io/autogen/stable/user-guide/core-user-guide/core-concepts/topic-and-subscription.html). |
| 72 | +
|
| 73 | +### Publish a message |
| 74 | + |
| 75 | +Now that we have a handler for `CountMessage`, and we have the subscription in place we can publish a result out of the handler. |
| 76 | + |
| 77 | +```csharp |
| 78 | +public async ValueTask HandleAsync(CountMessage item, MessageContext messageContext) |
| 79 | +{ |
| 80 | + int newValue = item.Content - 1; |
| 81 | + Console.WriteLine($"\nModifier:\nModified {item.Content} to {newValue}"); |
| 82 | + |
| 83 | + CountUpdate updateMessage = new CountUpdate { NewCount = newValue }; |
| 84 | + await this.PublishMessageAsync(updateMessage, topic: new TopicId("default")); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +You'll notice that when we publish the message, we specify the topic to publish to. We're using a topic called `default` in this case, which is the same topic which we subscribed to. We could have used a different topic, but in this case we're keeping it simple. |
| 89 | + |
| 90 | +### Passing arguments to the agent |
| 91 | + |
| 92 | +Let's extend our agent to make what we do to the count configurable. We'll do this by passing a function to the agent that will be used to modify the count. |
| 93 | + |
| 94 | +```csharp |
| 95 | +using ModifyF = System.Func<int, int>; |
| 96 | + |
| 97 | +// ... |
| 98 | +
|
| 99 | +[TypeSubscription("default")] |
| 100 | +public class Modifier( |
| 101 | + AgentId id, |
| 102 | + IAgentRuntime runtime, |
| 103 | + ModifyF modifyFunc // <-- Add this |
| 104 | + ) : |
| 105 | + BaseAgent(...), |
| 106 | + IHandle<CountMessage> |
| 107 | +{ |
| 108 | + |
| 109 | + public async ValueTask HandleAsync(CountMessage item, MessageContext messageContext) |
| 110 | + { |
| 111 | + int newValue = modifyFunc(item.Content); // <-- use it here |
| 112 | +
|
| 113 | + // ... |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +``` |
| 118 | + |
| 119 | +### Final Modifier implementation |
| 120 | + |
| 121 | +Here is the final implementation of the Modifier agent: |
| 122 | + |
| 123 | +[!code-csharp[](../../../dotnet/samples/GettingStarted/Modifier.cs#snippet_Modifier)] |
| 124 | + |
| 125 | +### Checker |
| 126 | + |
| 127 | +We'll also define a Checker agent that will check the count and stop the application when the count reaches 1. Additionally, we'll use dependency injection to get a reference to the `IHostApplicationLifetime` service, which we can use to stop the application. |
| 128 | + |
| 129 | +[!code-csharp[](../../../dotnet/samples/GettingStarted/Checker.cs#snippet_Checker)] |
| 130 | + |
| 131 | +## Putting it all together |
| 132 | + |
| 133 | +Now that we have our agents defined, we can put them together in a simple application that will count down from 10 to 1. |
| 134 | + |
| 135 | +After includes, the first thing to do is to define the two functions for modifying and checking for completion. |
| 136 | + |
| 137 | +[!code-csharp[](../../../dotnet/samples/GettingStarted/Program.cs#snippet_Program_funcs)] |
| 138 | + |
| 139 | +Then, we create a builder and do the following things: |
| 140 | + |
| 141 | +- Specify that we are using the in process runtime |
| 142 | +- Register our functions as services |
| 143 | +- Register the agent classes we defined earlier |
| 144 | +- Finally, build and start our app |
| 145 | + |
| 146 | +[!code-csharp[](../../../dotnet/samples/GettingStarted/Program.cs#snippet_Program_builder)] |
| 147 | + |
| 148 | +The app is now running, but we need to kick off the process with a message. We do this by publishing a `CountMessage` with an initial value of 10. |
| 149 | +Importantly we publish this to the "default" topic which is what our agents are subscribed to. Finally, we wait for the application to stop. |
| 150 | + |
| 151 | +[!code-csharp[](../../../dotnet/samples/GettingStarted/Program.cs#snippet_Program_publish)] |
| 152 | + |
| 153 | +That's it! You should see the count down from 10 to 1 in the console. |
| 154 | + |
| 155 | +Here's the full code for the `Program` class: |
| 156 | + |
| 157 | +[!code-csharp[](../../../dotnet/samples/GettingStarted/Program.cs#snippet_Program)] |
| 158 | + |
| 159 | +## Things to try |
| 160 | + |
| 161 | +Here are some ideas to try with this sample: |
| 162 | + |
| 163 | +- Change the initial count |
| 164 | +- Create a new modifier function that counts up instead. (Don't forget to change the checker too!) |
| 165 | +- Create an agent that outputs to the console instead of the modifier or checker agent doing it themselves (hint: use a new message type) |
0 commit comments