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’ll 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 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ await foreach (var content in chat.GetChatMessagesAsync(personalShopperAgent).Re

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

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

Expand Down
10 changes: 5 additions & 5 deletions dotnet/samples/Concepts/Agents/MixedChat_Agents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;

namespace Agents;
namespace Examples;
RogerBarreto marked this conversation as resolved.
Show resolved Hide resolved
/// <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,15 +77,15 @@ 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));
Console.WriteLine($"# {AuthorRole.User}: '{input}'");
chat.Add(new ChatMessageContent(AuthorRole.User, input));
this.WriteLine($"# {AuthorRole.User}: '{input}'");

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

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

private sealed class ApprovalTerminationStrategy : TerminationStrategy
Expand Down
8 changes: 4 additions & 4 deletions dotnet/samples/Concepts/Agents/OpenAIAssistant_Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.SemanticKernel.ChatCompletion;
using Plugins;

namespace Agents;
namespace Examples;
/// <summary>
/// Demonstrate creation of <see cref="OpenAIAssistantAgent"/> and
/// eliciting its response to three explicit user messages.
Expand Down Expand Up @@ -58,13 +58,13 @@ 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}'");
this.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync(agent))
{
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;

namespace Agents;

namespace Examples;
/// <summary>
/// Demonstrate using code-interpreter with <see cref="OpenAIAssistantAgent"/> to
/// produce image content displays the requested charts.
Expand Down Expand Up @@ -64,20 +63,20 @@ 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}'");
this.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var message in chat.InvokeAsync(agent))
{
if (!string.IsNullOrWhiteSpace(message.Content))
{
Console.WriteLine($"# {message.Role} - {message.AuthorName ?? "*"}: '{message.Content}'");
this.WriteLine($"# {message.Role} - {message.AuthorName ?? "*"}: '{message.Content}'");
}

foreach (var fileReference in message.Items.OfType<FileReferenceContent>())
{
Console.WriteLine($"# {message.Role} - {message.AuthorName ?? "*"}: #{fileReference.FileId}");
this.WriteLine($"# {message.Role} - {message.AuthorName ?? "*"}: #{fileReference.FileId}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
using Microsoft.SemanticKernel.Agents.OpenAI;
using Microsoft.SemanticKernel.ChatCompletion;

namespace Agents;

namespace Examples;
/// <summary>
/// Demonstrate using code-interpreter on <see cref="OpenAIAssistantAgent"/> .
/// </summary>
Expand Down Expand Up @@ -42,13 +41,13 @@ 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}'");
this.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync(agent))
{
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions dotnet/samples/Concepts/Agents/OpenAIAssistant_Retrieval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Resources;

namespace Agents;

namespace Examples;
/// <summary>
/// Demonstrate using retrieval on <see cref="OpenAIAssistantAgent"/> .
/// </summary>
Expand All @@ -28,7 +27,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,13 +59,13 @@ 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}'");
this.WriteLine($"# {AuthorRole.User}: '{input}'");
RogerBarreto marked this conversation as resolved.
Show resolved Hide resolved

await foreach (var content in chat.InvokeAsync(agent))
{
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions dotnet/samples/GettingStartedWithAgents/Step1_Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ 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}'");
this.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync(agent))
{
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions dotnet/samples/GettingStartedWithAgents/Step2_Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ 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));
Console.WriteLine($"# {AuthorRole.User}: '{input}'");
chat.Add(new ChatMessageContent(AuthorRole.User, input));
this.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync(agent))
{
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions dotnet/samples/GettingStartedWithAgents/Step3_Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ 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));
Console.WriteLine($"# {AuthorRole.User}: '{input}'");
chat.Add(new ChatMessageContent(AuthorRole.User, input));
this.WriteLine($"# {AuthorRole.User}: '{input}'");

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

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

private sealed class ApprovalTerminationStrategy : TerminationStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ 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));
Console.WriteLine($"# {AuthorRole.User}: '{input}'");
chat.Add(new ChatMessageContent(AuthorRole.User, input));
this.WriteLine($"# {AuthorRole.User}: '{input}'");

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

Console.WriteLine($"# IS COMPLETE: {chat.IsComplete}");
this.WriteLine($"# IS COMPLETE: {chat.IsComplete}");
}
}
8 changes: 4 additions & 4 deletions dotnet/samples/GettingStartedWithAgents/Step5_JsonResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ 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}'");
this.WriteLine($"# {AuthorRole.User}: '{input}'");

await foreach (var content in chat.InvokeAsync(agent))
{
Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
Console.WriteLine($"# IS COMPLETE: {chat.IsComplete}");
this.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'");
this.WriteLine($"# IS COMPLETE: {chat.IsComplete}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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