Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net - Agents: chat.Add*(...) => chat.Add(...) #6066

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions dotnet/samples/Concepts/Agents/ComplexChat_NestedShopper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class ComplexChat_NestedShopper(ITestOutputHelper output) : BaseTest(outp
[Fact]
public async Task RunAsync()
{
this.WriteLine($"! {Model}");
Console.WriteLine($"! {Model}");

OpenAIPromptExecutionSettings jsonSettings = new() { ResponseFormat = ChatCompletionsResponseFormat.JsonObject };
OpenAIPromptExecutionSettings autoInvokeSettings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
Expand Down Expand Up @@ -137,9 +137,9 @@ public async Task RunAsync()
};

// Invoke chat and display messages.
this.WriteLine("\n######################################");
this.WriteLine("# DYNAMIC CHAT");
this.WriteLine("######################################");
Console.WriteLine("\n######################################");
Console.WriteLine("# DYNAMIC CHAT");
Console.WriteLine("######################################");

await InvokeChatAsync("Can you provide three original birthday gift ideas. I don't want a gift that someone else will also pick.");

Expand All @@ -150,27 +150,27 @@ public async Task RunAsync()
await InvokeChatAsync("He likes photography.");
}

this.WriteLine("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
this.WriteLine(">>>> AGGREGATED CHAT");
this.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Console.WriteLine("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Console.WriteLine(">>>> AGGREGATED CHAT");
Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

await foreach (var content in chat.GetChatMessagesAsync(personalShopperAgent).Reverse())
{
this.WriteLine($">>>> {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
Console.WriteLine($">>>> {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
}

async Task InvokeChatAsync(string input)
{
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));

this.WriteLine($"# {AuthorRole.User}: '{input}'");
Console.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync(personalShopperAgent))
{
this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
}

this.WriteLine($"\n# IS COMPLETE: {chat.IsComplete}");
Console.WriteLine($"\n# IS COMPLETE: {chat.IsComplete}");
}

ChatCompletionAgent CreateAgent(string agentName, string agentInstructions) =>
Expand Down Expand Up @@ -198,7 +198,7 @@ await foreach (var content in chat.InvokeAsync(personalShopperAgent))
string? agentName = string.IsNullOrWhiteSpace(jsonResult?.name) ? null : jsonResult?.name;
agentName ??= InternalGiftIdeaAgentName;

this.WriteLine($"\t>>>> INNER TURN: {agentName}");
Console.WriteLine($"\t>>>> INNER TURN: {agentName}");

return agentName;
}
Expand Down
3 changes: 2 additions & 1 deletion dotnet/samples/Concepts/Agents/MixedChat_Agents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.SemanticKernel.ChatCompletion;

namespace Agents;

/// <summary>
/// Demonstrate that two different agent types are able to participate in the same conversation.
/// In this case a <see cref="ChatCompletionAgent"/> and <see cref="OpenAIAssistantAgent"/> participate.
Expand Down Expand Up @@ -77,7 +78,7 @@ public async Task RunAsync()

// Invoke chat and display messages.
string input = "concept: maps made out of egg cartons.";
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));
Console.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync())
Expand Down
3 changes: 2 additions & 1 deletion dotnet/samples/Concepts/Agents/OpenAIAssistant_Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Plugins;

namespace Agents;

/// <summary>
/// Demonstrate creation of <see cref="OpenAIAssistantAgent"/> and
/// eliciting its response to three explicit user messages.
Expand Down Expand Up @@ -58,7 +59,7 @@ public async Task RunAsync()
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));

Console.WriteLine($"# {AuthorRole.User}: '{input}'");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task RunAsync()
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));

Console.WriteLine($"# {AuthorRole.User}: '{input}'");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task RunAsync()
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));

Console.WriteLine($"# {AuthorRole.User}: '{input}'");

Expand Down
4 changes: 2 additions & 2 deletions dotnet/samples/Concepts/Agents/OpenAIAssistant_Retrieval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task RunAsync()
new BinaryContent(() => Task.FromResult(EmbeddedResource.ReadStream("travelinfo.txt")!)),
new OpenAIFileUploadExecutionSettings("travelinfo.txt", OpenAIFilePurpose.Assistants));

Console.WriteLine(this.ApiKey);
WriteLine(this.ApiKey);

// Define the agent
OpenAIAssistantAgent agent =
Expand Down Expand Up @@ -60,7 +60,7 @@ public async Task RunAsync()
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));

Console.WriteLine($"# {AuthorRole.User}: '{input}'");

Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStartedWithAgents/Step1_Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task RunAsync()
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));

Console.WriteLine($"# {AuthorRole.User}: '{input}'");

Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStartedWithAgents/Step2_Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task RunAsync()
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));
Console.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync(agent))
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStartedWithAgents/Step3_Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task RunAsync()

// Invoke chat and display messages.
string input = "concept: maps made out of egg cartons.";
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));
Console.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public async Task RunAsync()

// Invoke chat and display messages.
string input = "concept: maps made out of egg cartons.";
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));
Console.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task RunAsync()
// Local function to invoke agent and display the conversation messages.
async Task InvokeAgentAsync(string input)
{
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));

Console.WriteLine($"# {AuthorRole.User}: '{input}'");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public async Task RunAsync()
// Local function to invoke agent and display the conversation messages.
async Task WriteAgentResponse(string input)
{
this.WriteLine($"# {AuthorRole.User}: {input}");
Console.WriteLine($"# {AuthorRole.User}: {input}");

await foreach (var content in agentClient.RunDemoAsync(input))
{
this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
}
}
}
Expand All @@ -107,7 +107,7 @@ public IAsyncEnumerable<ChatMessageContent> RunDemoAsync(string input)
{
// Create a chat for agent interaction.

this._chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
this._chat.Add(new ChatMessageContent(AuthorRole.User, input));

return this._chat.InvokeAsync(agent);
}
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/Agents/Abstractions/AgentChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ await foreach (ChatMessageContent message in messages.ConfigureAwait(false))
/// Any <see cref="AgentChat" /> instance does not support concurrent invocation and
/// will throw exception if concurrent activity is attempted.
/// </remarks>
public void AddChatMessage(ChatMessageContent message)
public void Add(ChatMessageContent message)
{
this.AddChatMessages([message]);
this.Add([message]);
}

/// <summary>
Expand All @@ -136,7 +136,7 @@ public void AddChatMessage(ChatMessageContent message)
/// Any <see cref="AgentChat" /> instance does not support concurrent invocation and
/// will throw exception if concurrent activity is attempted.
/// </remarks>
public void AddChatMessages(IReadOnlyList<ChatMessageContent> messages)
public void Add(IReadOnlyList<ChatMessageContent> messages)
{
this.SetActivityOrThrow(); // Disallow concurrent access to chat history

Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Agents/Abstractions/AggregatorChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ await foreach (ChatMessageContent message in this._chat.InvokeAsync(cancellation
protected internal override Task ReceiveAsync(IReadOnlyList<ChatMessageContent> history, CancellationToken cancellationToken = default)
{
// Always receive the initial history from the owning chat.
this._chat.AddChatMessages([.. history]);
this._chat.Add([.. history]);

return Task.CompletedTask;
}
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/Agents/Core/AgentGroupChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public sealed class AgentGroupChat : AgentChat
/// Add a <see cref="Agent"/> to the chat.
/// </summary>
/// <param name="agent">The <see cref="KernelAgent"/> to add.</param>
public void AddAgent(Agent agent)
public void Add(Agent agent)
{
if (this._agentIds.Add(agent.Id))
{
Expand Down Expand Up @@ -121,7 +121,7 @@ await foreach (var message in base.InvokeAgentAsync(agent, cancellationToken).Co
{
if (isJoining)
{
this.AddAgent(agent);
this.Add(agent);
}

await foreach (var message in base.InvokeAgentAsync(agent, cancellationToken).ConfigureAwait(false))
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/Agents/UnitTests/AgentChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public async Task VerifyAgentChatLifecycleAsync()
await this.VerifyHistoryAsync(expectedCount: 0, chat.GetChatMessagesAsync(chat.Agent)); // Agent history

// Inject history
chat.AddChatMessages([new ChatMessageContent(AuthorRole.User, "More")]);
chat.AddChatMessages([new ChatMessageContent(AuthorRole.User, "And then some")]);
chat.Add([new ChatMessageContent(AuthorRole.User, "More")]);
chat.Add([new ChatMessageContent(AuthorRole.User, "And then some")]);

// Verify updated history
await this.VerifyHistoryAsync(expectedCount: 2, chat.GetChatMessagesAsync()); // Primary history
await this.VerifyHistoryAsync(expectedCount: 0, chat.GetChatMessagesAsync(chat.Agent)); // Agent hasn't joined

// Invoke with input & verify (agent joins chat)
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "hi"));
chat.Add(new ChatMessageContent(AuthorRole.User, "hi"));
await chat.InvokeAsync().ToArrayAsync();
Assert.Equal(1, chat.Agent.InvokeCount);

Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/Agents/UnitTests/AggregatorAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task VerifyAggregatorAgentUsageAsync(AggregatorMode mode, int modeO
AgentGroupChat uberChat = new();

// Add message to outer chat (no agent has joined)
uberChat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "test uber"));
uberChat.Add(new ChatMessageContent(AuthorRole.User, "test uber"));

var messages = await uberChat.GetChatMessagesAsync().ToArrayAsync();
Assert.Single(messages);
Expand All @@ -57,7 +57,7 @@ public async Task VerifyAggregatorAgentUsageAsync(AggregatorMode mode, int modeO
Assert.Empty(messages); // Agent hasn't joined chat, no broadcast

// Add message to inner chat (not visible to parent)
groupChat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "test inner"));
groupChat.Add(new ChatMessageContent(AuthorRole.User, "test inner"));

messages = await uberChat.GetChatMessagesAsync().ToArrayAsync();
Assert.Single(messages);
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Agents/UnitTests/Core/AgentGroupChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task VerifyGroupAgentChatAgentMembershipAsync()
AgentGroupChat chat = new(agent1, agent2);
Assert.Equal(2, chat.Agents.Count);

chat.AddAgent(agent3);
chat.Add(agent3);
Assert.Equal(3, chat.Agents.Count);

var messages = await chat.InvokeAsync(agent4, isJoining: false).ToArrayAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public async Task VerifyOpenAIAssistantAgentAddMessagesAsync()
ChatMessageContent[] messages = await chat.InvokeAsync(agent).ToArrayAsync();
Assert.Single(messages);

chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "hi"));
chat.Add(new ChatMessageContent(AuthorRole.User, "hi"));

messages = await chat.GetChatMessagesAsync().ToArrayAsync();
Assert.Equal(2, messages.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task AzureOpenAIAssistantAgentAsync(string input, string expectedAn
});

AgentGroupChat chat = new();
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));
chat.Add(new ChatMessageContent(AuthorRole.User, input));

// Act
StringBuilder builder = new();
Expand Down