autogen/dotnet/samples/AutoGen.BasicSamples/CodeSnippet/BuildInMessageCodeSnippet.cs
Xiaoyun Zhang 9ba14ee15b
Fix dotnet test and reformat dotnet code (#3603)
* fix test

* install aspire workload

* format

* fix build error

* fix format

* format
2024-10-02 14:42:27 -04:00

43 lines
1.4 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved.
// BuildInMessageCodeSnippet.cs
using AutoGen.Core;
namespace AutoGen.BasicSample.CodeSnippet;
internal class BuildInMessageCodeSnippet
{
public async Task StreamingCallCodeSnippetAsync()
{
IStreamingAgent agent = default;
#region StreamingCallCodeSnippet
var helloTextMessage = new TextMessage(Role.User, "Hello");
var reply = agent.GenerateStreamingReplyAsync([helloTextMessage]);
var finalTextMessage = new TextMessage(Role.Assistant, string.Empty, from: agent.Name);
await foreach (var message in reply)
{
if (message is TextMessageUpdate textMessage)
{
Console.Write(textMessage.Content);
finalTextMessage.Update(textMessage);
}
}
#endregion StreamingCallCodeSnippet
#region StreamingCallWithFinalMessage
reply = agent.GenerateStreamingReplyAsync([helloTextMessage]);
TextMessage finalMessage = null;
await foreach (var message in reply)
{
if (message is TextMessageUpdate textMessage)
{
Console.Write(textMessage.Content);
}
else if (message is TextMessage txtMessage)
{
finalMessage = txtMessage;
}
}
#endregion StreamingCallWithFinalMessage
}
}