2024-10-30 11:53:37 -07:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// HelloAIAgent.cs
|
|
|
|
|
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-23 14:23:36 -07:00
|
|
|
using Microsoft.Extensions.AI;
|
|
|
|
|
|
|
|
namespace Hello;
|
2024-12-03 08:09:02 -08:00
|
|
|
[TopicSubscription("agents")]
|
2024-10-23 14:23:36 -07:00
|
|
|
public class HelloAIAgent(
|
2024-12-17 13:04:37 -08:00
|
|
|
IAgentWorker worker,
|
2024-10-23 14:23:36 -07:00
|
|
|
[FromKeyedServices("EventTypes")] EventTypes typeRegistry,
|
2024-11-12 11:04:59 -08:00
|
|
|
IHostApplicationLifetime hostApplicationLifetime,
|
2024-10-23 14:23:36 -07:00
|
|
|
IChatClient client) : HelloAgent(
|
2024-12-17 13:04:37 -08:00
|
|
|
worker,
|
2024-11-12 11:04:59 -08:00
|
|
|
typeRegistry,
|
|
|
|
hostApplicationLifetime),
|
2024-10-23 14:23:36 -07:00
|
|
|
IHandle<NewMessageReceived>
|
|
|
|
{
|
|
|
|
// This Handle supercedes the one in the base class
|
|
|
|
public new async Task Handle(NewMessageReceived item)
|
|
|
|
{
|
|
|
|
var prompt = "Please write a limerick greeting someone with the name " + item.Message;
|
|
|
|
var response = await client.CompleteAsync(prompt);
|
2024-11-08 14:16:24 +00:00
|
|
|
var evt = new Output { Message = response.Message.Text };
|
|
|
|
await PublishMessageAsync(evt).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var goodbye = new ConversationClosed { UserId = this.AgentId.Key, UserMessage = "Goodbye" };
|
|
|
|
await PublishMessageAsync(goodbye).ConfigureAwait(false);
|
2024-10-23 14:23:36 -07:00
|
|
|
}
|
|
|
|
}
|