-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and publish documentation site for 0.4 dotnet (#5275)
<img width="1840" alt="Screenshot 2025-01-30 at 6 26 02 PM" src="https://github.com/user-attachments/assets/5b4c9ebf-0880-4b2e-aa1f-f2b956922b49" />
- Loading branch information
1 parent
465a974
commit b05878a
Showing
25 changed files
with
512 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
############### | ||
# folder # | ||
############### | ||
/**/DROP/ | ||
/**/TEMP/ | ||
/**/packages/ | ||
/**/bin/ | ||
/**/obj/ | ||
|
||
# build artifacts for web | ||
_site/ | ||
api/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## How to build and run the website | ||
|
||
### Prerequisites | ||
- dotnet 7.0 or later | ||
|
||
### Build | ||
Firstly, go to autogen/dotnet folder and run the following command to build the website: | ||
```bash | ||
dotnet tool restore | ||
dotnet tool run docfx website/docfx.json --serve | ||
``` | ||
|
||
After the command is executed, you can open your browser and navigate to `http://localhost:8080` to view the website. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Differences from Python | ||
|
||
## Publishing to a topic that an agent is also subscribed to | ||
|
||
> [!NOTE] | ||
> TLDR; Default behavior is identical. | ||
When an agent publishes a message to a topic to which it also listens, the message will not be received by the agent that sent it. This is also the behavior in the Python runtime. However to support previous usage, in @Microsoft.AutoGen.Core.InProcessRuntime, you can set the @Microsoft.AutoGen.Core.InProcessRuntime.DeliverToSelf property to true in the TopicSubscription attribute to allow an agent to receive messages it sends. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# AutoGen Core | ||
|
||
AutoGen Core for .NET follows the same concepts and conventions of its Python counterpart. In fact, in order to understand the concepts in the .NET version, we recommend reading the Python documentation first. Unless otherwise stated, the concepts in the Python version map to .NET. | ||
|
||
Any important differences between the language versions are documented in the [Differences from Python](./differences-from-python.md) section. For things that only affect a given language, such as dependency injection or host builder patterns, these will not be specified in the differences document. | ||
|
||
For .NET we are starting with the core functionality and will be expanding support progressively. So far the core abstractions of Agent and Runtime are available. The InProcessRuntime is the only runtime available at this time. We will be expanding to cross language support in upcoming releases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Installation | ||
|
||
Install via `.NET cli` | ||
|
||
```sh | ||
dotnet add package Microsoft.AutoGen.Contracts --version 0.4.0-dev.1 | ||
dotnet add package Microsoft.AutoGen.Core --version 0.4.0-dev.1 | ||
``` | ||
|
||
Or, install via `Package Manager` | ||
|
||
```pwsh | ||
PM> NuGet\Install-Package Microsoft.AutoGen.Contracts -Version 0.4.0-dev.1 | ||
PM> NuGet\Install-Package Microsoft.AutoGen.Core -Version 0.4.0-dev.1 | ||
``` | ||
|
||
Or, add via `<PackageReference>` | ||
|
||
```xml | ||
<PackageReference Include="Microsoft.AutoGen.Contracts" Version="0.4.0-dev.1" /> | ||
<PackageReference Include="Microsoft.AutoGen.Core" Version="0.4.0-dev.1" /> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Using Protocol Buffers to Define Message Types | ||
|
||
For a message to be sent using a runtime other than the @Microsoft.AutoGen.Core.InProcessRuntime, it must be defined as a Protocol Buffers message. This is because the message is serialized and deserialized using Protocol Buffers. This requirement may be relaxed in future by allowing for converters, custom serialization, or other mechanisms. | ||
|
||
## How to include Protocol Buffers in a .NET project | ||
|
||
The .proto file which defines the message types must be included in the project, which will automatically generate the C# classes for the messages. | ||
|
||
1. Include `Grpc.Tools` package in your `.csproj` file: | ||
|
||
```xml | ||
<PackageReference Include="Grpc.Tools" PrivateAssets="All" /> | ||
``` | ||
|
||
2. Create an include a `.proto` file in the project: | ||
|
||
```xml | ||
<ItemGroup> | ||
<Protobuf Include="messages.proto" GrpcServices="Client;Server" Link="messages.proto" /> | ||
</ItemGroup> | ||
``` | ||
|
||
3. define your messages as specified in the [Protocol Buffers Language Guide](https://protobuf.dev/programming-guides/proto3/) | ||
|
||
```proto | ||
syntax = "proto3"; | ||
package HelloAgents; | ||
option csharp_namespace = "MyAgentsProtocol"; | ||
message TextMessage { | ||
string Source = 1; | ||
string Content = 2; | ||
} | ||
``` | ||
|
||
4. Code against the generated class for handling, sending and publishing messages: | ||
|
||
```csharp | ||
using Microsoft.AutoGen.Contracts; | ||
using Microsoft.AutoGen.Core; | ||
using MyAgentsProtocol; | ||
|
||
[TypeSubscription("default")] | ||
public class Checker( | ||
AgentId id, | ||
IAgentRuntime runtime, | ||
) : | ||
BaseAgent(id, runtime, "MyAgent", null), | ||
IHandle<TextMessage> | ||
{ | ||
public async ValueTask HandleAsync(TextMessage item, MessageContext messageContext) | ||
{ | ||
Console.WriteLine($"Received message from {item.Source}: {item.Content}"); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
- name: Overview | ||
href: index.md | ||
- name: Installation | ||
href: installation.md | ||
- name: Tutorial | ||
href: tutorial.md | ||
- name: Differences from Python | ||
href: differences-from-python.md | ||
- name: Protobuf message types | ||
href: protobuf-message-types.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
# Tutorial | ||
|
||
> [!TIP] | ||
> 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). | ||
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. | ||
|
||
## Defining the message types | ||
|
||
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. | ||
|
||
We're going to use `CountMessage` to pass the current count and `CountUpdate` to pass the updated count. | ||
|
||
[!code-csharp[](../../../dotnet/samples/GettingStarted/CountMessage.cs#snippet_CountMessage)] | ||
[!code-csharp[](../../../dotnet/samples/GettingStarted/CountUpdate.cs#snippet_CountUpdate)] | ||
|
||
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. | ||
|
||
## Creating the agents | ||
|
||
### Inherit from `BaseAgent` | ||
|
||
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: | ||
|
||
```csharp | ||
using Microsoft.AutoGen.Contracts; | ||
using Microsoft.AutoGen.Core; | ||
|
||
public class Modifier( | ||
AgentId id, | ||
IAgentRuntime runtime, | ||
) : | ||
BaseAgent(id, runtime, "MyAgent", null), | ||
{ | ||
} | ||
``` | ||
|
||
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. | ||
|
||
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). | ||
|
||
### Create a handler | ||
|
||
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: | ||
|
||
```csharp | ||
public class Modifier( | ||
// ... | ||
) : | ||
BaseAgent(...), | ||
IHandle<CountMessage> | ||
{ | ||
|
||
public async ValueTask HandleAsync(CountMessage item, MessageContext messageContext) | ||
{ | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
### Add a subscription | ||
|
||
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: | ||
|
||
```csharp | ||
[TypeSubscription("default")] | ||
public class Modifier( | ||
// ... | ||
``` | ||
|
||
Learn more about topics and subscriptions [here](https://microsoft.github.io/autogen/stable/user-guide/core-user-guide/core-concepts/topic-and-subscription.html). | ||
### Publish a message | ||
|
||
Now that we have a handler for `CountMessage`, and we have the subscription in place we can publish a result out of the handler. | ||
|
||
```csharp | ||
public async ValueTask HandleAsync(CountMessage item, MessageContext messageContext) | ||
{ | ||
int newValue = item.Content - 1; | ||
Console.WriteLine($"\nModifier:\nModified {item.Content} to {newValue}"); | ||
|
||
CountUpdate updateMessage = new CountUpdate { NewCount = newValue }; | ||
await this.PublishMessageAsync(updateMessage, topic: new TopicId("default")); | ||
} | ||
``` | ||
|
||
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. | ||
|
||
### Passing arguments to the agent | ||
|
||
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. | ||
|
||
```csharp | ||
using ModifyF = System.Func<int, int>; | ||
|
||
// ... | ||
[TypeSubscription("default")] | ||
public class Modifier( | ||
AgentId id, | ||
IAgentRuntime runtime, | ||
ModifyF modifyFunc // <-- Add this | ||
) : | ||
BaseAgent(...), | ||
IHandle<CountMessage> | ||
{ | ||
|
||
public async ValueTask HandleAsync(CountMessage item, MessageContext messageContext) | ||
{ | ||
int newValue = modifyFunc(item.Content); // <-- use it here | ||
// ... | ||
} | ||
} | ||
|
||
``` | ||
|
||
### Final Modifier implementation | ||
|
||
Here is the final implementation of the Modifier agent: | ||
|
||
[!code-csharp[](../../../dotnet/samples/GettingStarted/Modifier.cs#snippet_Modifier)] | ||
|
||
### Checker | ||
|
||
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. | ||
|
||
[!code-csharp[](../../../dotnet/samples/GettingStarted/Checker.cs#snippet_Checker)] | ||
|
||
## Putting it all together | ||
|
||
Now that we have our agents defined, we can put them together in a simple application that will count down from 10 to 1. | ||
|
||
After includes, the first thing to do is to define the two functions for modifying and checking for completion. | ||
|
||
[!code-csharp[](../../../dotnet/samples/GettingStarted/Program.cs#snippet_Program_funcs)] | ||
|
||
Then, we create a builder and do the following things: | ||
|
||
- Specify that we are using the in process runtime | ||
- Register our functions as services | ||
- Register the agent classes we defined earlier | ||
- Finally, build and start our app | ||
|
||
[!code-csharp[](../../../dotnet/samples/GettingStarted/Program.cs#snippet_Program_builder)] | ||
|
||
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. | ||
Importantly we publish this to the "default" topic which is what our agents are subscribed to. Finally, we wait for the application to stop. | ||
|
||
[!code-csharp[](../../../dotnet/samples/GettingStarted/Program.cs#snippet_Program_publish)] | ||
|
||
That's it! You should see the count down from 10 to 1 in the console. | ||
|
||
Here's the full code for the `Program` class: | ||
|
||
[!code-csharp[](../../../dotnet/samples/GettingStarted/Program.cs#snippet_Program)] | ||
|
||
## Things to try | ||
|
||
Here are some ideas to try with this sample: | ||
|
||
- Change the initial count | ||
- Create a new modifier function that counts up instead. (Don't forget to change the checker too!) | ||
- Create an agent that outputs to the console instead of the modifier or checker agent doing it themselves (hint: use a new message type) |
Oops, something went wrong.