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.Abstractions ;
using Microsoft.AutoGen.Agents ;
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-10-09 11:31:12 -07:00
[TopicSubscription("HelloAgents")]
public class HelloAgent (
IAgentContext context ,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry ) : ConsoleAgent (
context ,
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
} . ToCloudEvent ( this . AgentId . Key ) ;
await PublishEvent ( evt ) . ConfigureAwait ( false ) ;
var goodbye = new ConversationClosed
{
UserId = this . AgentId . Key ,
UserMessage = "Goodbye"
} . ToCloudEvent ( this . AgentId . Key ) ;
await PublishEvent ( goodbye ) . ConfigureAwait ( false ) ;
}
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
} . ToCloudEvent ( this . AgentId . Key ) ;
await PublishEvent ( evt ) . ConfigureAwait ( false ) ;
2024-10-23 14:23:36 -07:00
//sleep30 seconds
await Task . Delay ( 30000 ) . ConfigureAwait ( false ) ;
await AgentsApp . ShutdownAsync ( ) . ConfigureAwait ( false ) ;
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
}
}