2024-10-30 11:53:37 -07:00
// Copyright (c) Microsoft Corporation. All rights reserved.
// Program.cs
2024-10-23 14:23:36 -07:00
using Hello ;
2024-10-16 20:09:39 -07:00
using Microsoft.AutoGen.Agents ;
2024-12-12 19:43:26 -08:00
using Microsoft.AutoGen.Contracts ;
2024-12-13 11:55:43 -08:00
using Microsoft.AutoGen.Core ;
2024-10-02 09:14:54 -07:00
2024-10-08 10:02:48 -07:00
// send a message to the agent
2025-01-20 10:06:06 -08:00
var builder = new HostApplicationBuilder ( ) ;
2024-10-23 14:23:36 -07:00
// 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\"" ) ;
}
2025-01-24 14:26:29 -05:00
builder . Configuration [ "ConnectionStrings:HelloAIAgents" ] = Environment . GetEnvironmentVariable ( "AZURE_OPENAI_CONNECTION_STRING" ) ;
2024-10-23 14:23:36 -07:00
builder . AddChatCompletionService ( "HelloAIAgents" ) ;
2025-01-24 19:24:00 -08:00
var _ = new AgentTypes ( new Dictionary < string , Type >
2024-10-23 14:23:36 -07:00
{
{ "HelloAIAgents" , typeof ( HelloAIAgent ) }
} ) ;
2025-01-24 19:24:00 -08:00
var local = true ;
if ( Environment . GetEnvironmentVariable ( "AGENT_HOST" ) ! = null ) { local = false ; }
var app = await Microsoft . AutoGen . Core . Grpc . AgentsApp . PublishMessageAsync ( "HelloAgents" , new NewMessageReceived
2024-10-02 09:14:54 -07:00
{
Message = "World"
2025-01-24 19:24:00 -08:00
} , local : local ) . ConfigureAwait ( false ) ;
2024-10-02 09:14:54 -07:00
await app . WaitForShutdownAsync ( ) ;
2024-10-09 11:31:12 -07:00
namespace Hello
2024-10-02 09:14:54 -07:00
{
2025-01-24 19:24:00 -08:00
[TopicSubscription("HelloAgents")]
2024-10-09 11:31:12 -07:00
public class HelloAgent (
2025-01-24 19:24:00 -08:00
[FromKeyedServices("AgentsMetadata")] AgentsMetadata typeRegistry ,
2024-11-12 11:04:59 -08:00
IHostApplicationLifetime hostApplicationLifetime ) : ConsoleAgent (
2024-10-09 11:31:12 -07:00
typeRegistry ) ,
ISayHello ,
IHandle < NewMessageReceived > ,
IHandle < ConversationClosed >
2024-10-02 09:14:54 -07:00
{
2025-01-24 19:24:00 -08:00
public async Task Handle ( NewMessageReceived item , CancellationToken cancellationToken = default )
2024-10-02 09:14:54 -07:00
{
2024-10-09 11:31:12 -07:00
var response = await SayHello ( item . Message ) . ConfigureAwait ( false ) ;
var evt = new Output
{
Message = response
2024-11-08 14:16:24 +00:00
} ;
await PublishMessageAsync ( evt ) . ConfigureAwait ( false ) ;
2024-10-09 11:31:12 -07:00
var goodbye = new ConversationClosed
{
UserId = this . AgentId . Key ,
UserMessage = "Goodbye"
2024-11-08 14:16:24 +00:00
} ;
await PublishMessageAsync ( goodbye ) . ConfigureAwait ( false ) ;
2024-10-09 11:31:12 -07:00
}
2025-01-24 19:24:00 -08:00
public async Task Handle ( ConversationClosed item , CancellationToken cancellationToken = default )
2024-10-08 10:02:48 -07:00
{
2024-10-09 11:31:12 -07:00
var goodbye = $"********************* {item.UserId} said {item.UserMessage} ************************" ;
var evt = new Output
{
Message = goodbye
2024-11-08 14:16:24 +00:00
} ;
await PublishMessageAsync ( evt ) . ConfigureAwait ( false ) ;
2024-10-23 14:23:36 -07:00
//sleep30 seconds
await Task . Delay ( 30000 ) . ConfigureAwait ( false ) ;
2024-11-12 11:04:59 -08:00
hostApplicationLifetime . StopApplication ( ) ;
2024-10-23 14:23:36 -07:00
2024-10-09 11:31:12 -07:00
}
public async Task < string > SayHello ( string ask )
2024-10-02 09:14:54 -07:00
{
2024-10-09 11:31:12 -07:00
var response = $"\n\n\n\n***************Hello {ask}**********************\n\n\n\n" ;
return response ;
}
2024-10-02 09:14:54 -07:00
}
2024-10-09 11:31:12 -07:00
public interface ISayHello
2024-10-02 09:14:54 -07:00
{
2024-10-09 11:31:12 -07:00
public Task < string > SayHello ( string ask ) ;
2024-10-02 09:14:54 -07:00
}
}