mirror of
https://github.com/microsoft/autogen.git
synced 2025-07-11 19:11:39 +00:00

closes #3950 closes #3702 What this is doing: I am refactoring the services on the .NET runtime and attempting to clarify the naming and organization. I added this doc to help capture the naming and concepts. AgentRuntime / Worker should work similar to the python version and enables running the whole agent system in one process. For remote the system uses the versions of the services in the grpc folder. lots of other bug fixes/threading cleanup - passing cancellation token throughout Services update clarifies the naming and roles: Worker: Hosts the Agents and is a client to the Gateway Gateway: -- RPC gateway for the other services APIs -- Provides an RPC bridge between the workers and the Event Bus Registry: keeps track of the agents in the system and which events they can handle AgentState: persistent state for agents
83 lines
3.0 KiB
C#
83 lines
3.0 KiB
C#
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Program.cs
|
|
|
|
using Hello;
|
|
using Microsoft.AutoGen.Abstractions;
|
|
using Microsoft.AutoGen.Agents;
|
|
|
|
// send a message to the agent
|
|
var builder = WebApplication.CreateBuilder();
|
|
// put these in your environment or appsettings.json
|
|
builder.Configuration["HelloAIAgents:ModelType"] = "azureopenai";
|
|
builder.Configuration["HelloAIAgents:LlmModelName"] = "gpt-3.5-turbo";
|
|
Environment.SetEnvironmentVariable("AZURE_OPENAI_CONNECTION_STRING", "Endpoint=https://TODO.openai.azure.com/;Key=TODO;Deployment=TODO");
|
|
if (Environment.GetEnvironmentVariable("AZURE_OPENAI_CONNECTION_STRING") == null)
|
|
{
|
|
throw new InvalidOperationException("AZURE_OPENAI_CONNECTION_STRING not set, try something like AZURE_OPENAI_CONNECTION_STRING = \"Endpoint=https://TODO.openai.azure.com/;Key=TODO;Deployment=TODO\"");
|
|
}
|
|
builder.Configuration["ConectionStrings:HelloAIAgents"] = Environment.GetEnvironmentVariable("AZURE_OPENAI_CONNECTION_STRING");
|
|
builder.AddChatCompletionService("HelloAIAgents");
|
|
var agentTypes = new AgentTypes(new Dictionary<string, Type>
|
|
{
|
|
{ "HelloAIAgents", typeof(HelloAIAgent) }
|
|
});
|
|
var app = await AgentsApp.PublishMessageAsync("HelloAgents", new NewMessageReceived
|
|
{
|
|
Message = "World"
|
|
}, builder, agentTypes, local: true);
|
|
|
|
await app.WaitForShutdownAsync();
|
|
|
|
namespace Hello
|
|
{
|
|
[TopicSubscription("HelloAgents")]
|
|
public class HelloAgent(
|
|
IAgentRuntime context,
|
|
[FromKeyedServices("EventTypes")] EventTypes typeRegistry,
|
|
IHostApplicationLifetime hostApplicationLifetime) : ConsoleAgent(
|
|
context,
|
|
typeRegistry),
|
|
ISayHello,
|
|
IHandle<NewMessageReceived>,
|
|
IHandle<ConversationClosed>
|
|
{
|
|
public async Task Handle(NewMessageReceived item)
|
|
{
|
|
var response = await SayHello(item.Message).ConfigureAwait(false);
|
|
var evt = new Output
|
|
{
|
|
Message = response
|
|
};
|
|
await PublishMessageAsync(evt).ConfigureAwait(false);
|
|
var goodbye = new ConversationClosed
|
|
{
|
|
UserId = this.AgentId.Key,
|
|
UserMessage = "Goodbye"
|
|
};
|
|
await PublishMessageAsync(goodbye).ConfigureAwait(false);
|
|
}
|
|
public async Task Handle(ConversationClosed item)
|
|
{
|
|
var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************";
|
|
var evt = new Output
|
|
{
|
|
Message = goodbye
|
|
};
|
|
await PublishMessageAsync(evt).ConfigureAwait(false);
|
|
//sleep30 seconds
|
|
await Task.Delay(30000).ConfigureAwait(false);
|
|
hostApplicationLifetime.StopApplication();
|
|
|
|
}
|
|
public async Task<string> SayHello(string ask)
|
|
{
|
|
var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n";
|
|
return response;
|
|
}
|
|
}
|
|
public interface ISayHello
|
|
{
|
|
public Task<string> SayHello(string ask);
|
|
}
|
|
}
|