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
2024-10-23 14:23:36 -07:00
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
2024-10-02 09:14:54 -07:00
{
Message = "World"
2024-10-23 14:23:36 -07:00
} , builder , agentTypes , local : true ) ;
2024-10-08 10:02:48 -07:00
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
{
2024-12-03 08:09:02 -08:00
[TopicSubscription("agents")]
2024-10-09 11:31:12 -07:00
public class HelloAgent (
2024-12-17 13:04:37 -08:00
IAgentWorker worker ,
2024-11-12 11:04:59 -08:00
[FromKeyedServices("EventTypes")] EventTypes typeRegistry ,
IHostApplicationLifetime hostApplicationLifetime ) : ConsoleAgent (
2024-12-17 13:04:37 -08:00
worker ,
2024-10-09 11:31:12 -07:00
typeRegistry ) ,
ISayHello ,
IHandle < NewMessageReceived > ,
IHandle < ConversationClosed >
2024-10-02 09:14:54 -07:00
{
2024-10-09 11:31:12 -07:00
public async Task Handle ( NewMessageReceived item )
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
}
public async Task Handle ( ConversationClosed item )
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
}
}