Skip to content

A programming framework for agentic AI

getting started sample

Jack Gerritsa4d8d571b0f8
diff --git a/dotnet/AutoGen.sln b/dotnet/AutoGen.sln
index 5088b7a32..9496ca5d7 100644
--- a/dotnet/AutoGen.sln
+++ b/dotnet/AutoGen.sln
@@ -142,6 +142,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Core.Grpc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Tests.Shared", "test\Microsoft.AutoGen.Tests.Shared\Microsoft.AutoGen.Tests.Shared.csproj", "{58AD8E1D-83BD-4950-A324-1A20677D78D9}"
EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{CE0AA8D5-12B8-4628-9589-DAD8CB0DDCF6}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "getting-started", "samples\getting-started\getting-started.csproj", "{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
CoreOnly|Any CPU = CoreOnly|Any CPU
@@ -433,6 +437,12 @@ Global
{58AD8E1D-83BD-4950-A324-1A20677D78D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58AD8E1D-83BD-4950-A324-1A20677D78D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58AD8E1D-83BD-4950-A324-1A20677D78D9}.Release|Any CPU.Build.0 = Release|Any CPU
+ {70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.CoreOnly|Any CPU.ActiveCfg = Debug|Any CPU
+ {70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.CoreOnly|Any CPU.Build.0 = Debug|Any CPU
+ {70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -498,6 +508,7 @@ Global
{599E1971-1DA9-453F-A7A8-42510BBC95C2} = {05B9C173-6441-4DCA-9AC4-E897EF75F331}
{33A28A4B-123B-4416-9631-0F759B8D6172} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{58AD8E1D-83BD-4950-A324-1A20677D78D9} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
+ {70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D} = {CE0AA8D5-12B8-4628-9589-DAD8CB0DDCF6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}
diff --git a/dotnet/samples/getting-started/Checker.cs b/dotnet/samples/getting-started/Checker.cs
new file mode 100644
index 000000000..f79af5f80
--- /dev/null
+++ b/dotnet/samples/getting-started/Checker.cs
@@ -0,0 +1,34 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Checker.cs
+
+using Microsoft.AutoGen.Contracts;
+using Microsoft.AutoGen.Core;
+using Microsoft.Extensions.Hosting;
+using TerminationF = System.Func<int, bool>;
+
+namespace Samples;
+
+[TypeSubscription("default")]
+public class Checker(
+ AgentId id,
+ IAgentRuntime runtime,
+ IHostApplicationLifetime hostApplicationLifetime,
+ TerminationF runUntilFunc
+ ) :
+ BaseAgent(id, runtime, "Modifier", null),
+ IHandle<CountUpdate>
+{
+ public async ValueTask HandleAsync(CountUpdate item, MessageContext messageContext)
+ {
+ if (!runUntilFunc(item.NewCount))
+ {
+ Console.WriteLine($"\nChecker:\n{item.NewCount} passed the check, continue.");
+ await this.PublishMessageAsync(new CountMessage { Content = item.NewCount }, new TopicId("default"));
+ }
+ else
+ {
+ Console.WriteLine($"\nChecker:\n{item.NewCount} failed the check, stopping.");
+ hostApplicationLifetime.StopApplication();
+ }
+ }
+}
\ No newline at end of file
diff --git a/dotnet/samples/getting-started/CountMessage.cs b/dotnet/samples/getting-started/CountMessage.cs
new file mode 100644
index 000000000..da006b90b
--- /dev/null
+++ b/dotnet/samples/getting-started/CountMessage.cs
@@ -0,0 +1,9 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// CountMessage.cs
+
+namespace Samples;
+
+public class CountMessage
+{
+ public int Content { get; set; }
+}
diff --git a/dotnet/samples/getting-started/CountUpdate.cs b/dotnet/samples/getting-started/CountUpdate.cs
new file mode 100644
index 000000000..376c2581d
--- /dev/null
+++ b/dotnet/samples/getting-started/CountUpdate.cs
@@ -0,0 +1,9 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// CountUpdate.cs
+
+namespace Samples;
+
+public class CountUpdate
+{
+ public int NewCount { get; set; }
+}
diff --git a/dotnet/samples/getting-started/Modifier.cs b/dotnet/samples/getting-started/Modifier.cs
new file mode 100644
index 000000000..777d1fcf6
--- /dev/null
+++ b/dotnet/samples/getting-started/Modifier.cs
@@ -0,0 +1,29 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Modifier.cs
+
+using Microsoft.AutoGen.Contracts;
+using Microsoft.AutoGen.Core;
+
+using ModifyF = System.Func<int, int>;
+
+namespace Samples;
+
+[TypeSubscription("default")]
+public class Modifier(
+ AgentId id,
+ IAgentRuntime runtime,
+ ModifyF modifyFunc
+ ) :
+ BaseAgent(id, runtime, "Modifier", null),
+ IHandle<CountMessage>
+{
+
+ public async ValueTask HandleAsync(CountMessage item, MessageContext messageContext)
+ {
+ int newValue = modifyFunc(item.Content);
+ Console.WriteLine($"\nModifier:\nModified {item.Content} to {newValue}");
+
+ CountUpdate updateMessage = new CountUpdate { NewCount = newValue };
+ await this.PublishMessageAsync(updateMessage, topic: new TopicId("default"));
+ }
+}
\ No newline at end of file
diff --git a/dotnet/samples/getting-started/Program.cs b/dotnet/samples/getting-started/Program.cs
new file mode 100644
index 000000000..efbb0a14f
--- /dev/null
+++ b/dotnet/samples/getting-started/Program.cs
@@ -0,0 +1,34 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Program.cs
+
+using TerminationF = System.Func<int, bool>;
+using ModifyF = System.Func<int, int>;
+using Microsoft.AutoGen.Core;
+using Microsoft.Extensions.Hosting;
+using Samples;
+using Microsoft.Extensions.DependencyInjection.Extensions;
+using Microsoft.AutoGen.Contracts;
+
+ModifyF modifyFunc = (int x) => x - 1;
+TerminationF runUntilFunc = (int x) =>
+{
+ return x <= 1;
+};
+
+HostApplicationBuilder builder = new HostApplicationBuilder();
+builder.Services.TryAddSingleton(modifyFunc);
+builder.Services.TryAddSingleton(runUntilFunc);
+
+AgentsAppBuilder agentApp = new(builder);
+agentApp.AddAgent<Checker>("Checker");
+agentApp.AddAgent<Modifier>("Modifier");
+var app = await agentApp.BuildAsync();
+
+// Send the initial count to the agents app, running on the `local` runtime, and pass through the registered services via the application `builder`
+await app.PublishMessageAsync(new CountMessage
+{
+ Content = 10
+}, new TopicId("default"));
+
+// Run until application shutdown
+await app.WaitForShutdownAsync();
\ No newline at end of file
diff --git a/dotnet/samples/getting-started/getting-started.csproj b/dotnet/samples/getting-started/getting-started.csproj
new file mode 100644
index 000000000..a706e6022
--- /dev/null
+++ b/dotnet/samples/getting-started/getting-started.csproj
@@ -0,0 +1,15 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net8.0</TargetFramework>
+ <RootNamespace>getting_started</RootNamespace>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\..\src\Microsoft.AutoGen\Contracts\Microsoft.AutoGen.Contracts.csproj" />
+ <ProjectReference Include="..\..\src\Microsoft.AutoGen\Core\Microsoft.AutoGen.Core.csproj" />
+ </ItemGroup>
+</Project>
diff --git a/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs b/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs
index 8533fc1b4..9643f9f2e 100644
--- a/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs
@@ -83,10 +83,6 @@ public class AgentsApp
public async ValueTask ShutdownAsync() => await this.Host.StopAsync();
- public ValueTask PublishMessageAsync<TMessage>(TMessage message, string topic, string? messageId = null, CancellationToken? cancellationToken = default)
- where TMessage : notnull
- => this.AgentRuntime.PublishMessageAsync(message, TopicId.FromStr(topic), messageId: messageId, cancellationToken: cancellationToken);
-
public ValueTask PublishMessageAsync<TMessage>(TMessage message, TopicId topic, string? messageId = null, CancellationToken? cancellationToken = default)
where TMessage : notnull
{
diff --git a/dotnet/src/Microsoft.AutoGen/readme.md b/dotnet/src/Microsoft.AutoGen/readme.md
new file mode 100644
index 000000000..69f40b9b3
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/readme.md
@@ -0,0 +1,3 @@
+# Microsoft.AutoGen
+
+- [Getting started sample](../../samples/getting-started/)