Skip to content

.Net: Add invoke overloads for string and no message. #11219

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private async IAsyncEnumerable<ChatMessageContent> CompleteAsync(ChatHistory cha
{
var thread = new ChatHistoryAgentThread(chatHistory);
IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> content =
this._agent.InvokeAsync([], thread, options: new() { KernelArguments = arguments }, cancellationToken: cancellationToken);
this._agent.InvokeAsync(thread, options: new() { KernelArguments = arguments }, cancellationToken: cancellationToken);

await foreach (ChatMessageContent item in content.ConfigureAwait(false))
{
Expand All @@ -90,7 +90,7 @@ private async IAsyncEnumerable<StreamingChatMessageContent> CompleteSteamingAsyn
{
var thread = new ChatHistoryAgentThread(chatHistory);
IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> content =
this._agent.InvokeStreamingAsync([], thread, options: new() { KernelArguments = arguments }, cancellationToken: cancellationToken);
this._agent.InvokeStreamingAsync(thread, options: new() { KernelArguments = arguments }, cancellationToken: cancellationToken);

await foreach (StreamingChatMessageContent item in content.ConfigureAwait(false))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ await InvokeAgentAsync(
// Local function to invoke agent and display the response.
async Task InvokeAgentAsync(KernelArguments? arguments = null)
{
await foreach (ChatMessageContent response in agent.InvokeAsync([], thread, new() { KernelArguments = arguments }))
await foreach (ChatMessageContent response in agent.InvokeAsync(thread, new() { KernelArguments = arguments }))
{
WriteAgentChatMessage(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ await InvokeAgentAsync(
// Local function to invoke agent and display the response.
async Task InvokeAgentAsync(KernelArguments? arguments = null)
{
await foreach (ChatMessageContent response in agent.InvokeAsync([], thread, options: new() { KernelArguments = arguments }))
await foreach (ChatMessageContent response in agent.InvokeAsync(thread, options: new() { KernelArguments = arguments }))
{
WriteAgentChatMessage(response);
}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStartedWithAgents/Step01_Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ await InvokeAgentAsync(
async Task InvokeAgentAsync(KernelArguments? arguments = null)
{
// Invoke the agent without any messages, since the agent has all that it needs via the template and arguments.
await foreach (ChatMessageContent content in agent.InvokeAsync([], options: new() { KernelArguments = arguments }))
await foreach (ChatMessageContent content in agent.InvokeAsync(options: new() { KernelArguments = arguments }))
{
WriteAgentChatMessage(content);
}
Expand Down
99 changes: 97 additions & 2 deletions dotnet/src/Agents/Abstractions/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel.ChatCompletion;

namespace Microsoft.SemanticKernel.Agents;

Expand Down Expand Up @@ -43,6 +44,51 @@ public abstract class Agent
/// </summary>
public ILoggerFactory? LoggerFactory { get; init; }

/// <summary>
/// Invoke the agent with no message assuming that all required instructions are already provided to the agent or on the thread.
/// </summary>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameters for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An async list of response items that each contain a <see cref="ChatMessageContent"/> and an <see cref="AgentThread"/>.</returns>
/// <remarks>
/// To continue this thread in the future, use an <see cref="AgentThread"/> returned in one of the response items.
/// </remarks>
public virtual IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
AgentThread? thread = null,
AgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
return this.InvokeAsync([], thread, options, cancellationToken);
}

/// <summary>
/// Invoke the agent with the provided message and arguments.
/// </summary>
/// <param name="message">The message to pass to the agent.</param>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps be explicit that this will be treated as a user message

/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameters for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An async list of response items that each contain a <see cref="ChatMessageContent"/> and an <see cref="AgentThread"/>.</returns>
/// <remarks>
/// <para>
/// The provided message string will be treated as a user message.
/// </para>
/// <para>
/// To continue this thread in the future, use an <see cref="AgentThread"/> returned in one of the response items.
/// </para>
/// </remarks>
public virtual IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
string message,
AgentThread? thread = null,
AgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
Verify.NotNull(message);

return this.InvokeAsync(new ChatMessageContent(AuthorRole.User, message), thread, options, cancellationToken);
}

/// <summary>
/// Invoke the agent with the provided message and arguments.
/// </summary>
Expand All @@ -60,7 +106,9 @@ public virtual IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsy
AgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
return this.InvokeAsync(new[] { message }, thread, options, cancellationToken);
Verify.NotNull(message);

return this.InvokeAsync([message], thread, options, cancellationToken);
}

/// <summary>
Expand All @@ -80,6 +128,51 @@ public abstract IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAs
AgentInvokeOptions? options = null,
CancellationToken cancellationToken = default);

/// <summary>
/// Invoke the agent with no message assuming that all required instructions are already provided to the agent or on the thread.
/// </summary>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameters for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An async list of response items that each contain a <see cref="ChatMessageContent"/> and an <see cref="AgentThread"/>.</returns>
/// <remarks>
/// To continue this thread in the future, use an <see cref="AgentThread"/> returned in one of the response items.
/// </remarks>
public virtual IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> InvokeStreamingAsync(
AgentThread? thread = null,
AgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
return this.InvokeStreamingAsync([], thread, options, cancellationToken);
}

/// <summary>
/// Invoke the agent with the provided message and arguments.
/// </summary>
/// <param name="message">The message to pass to the agent.</param>
/// <param name="thread">The conversation thread to continue with this invocation. If not provided, creates a new thread.</param>
/// <param name="options">Optional parameters for agent invocation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>An async list of response items that each contain a <see cref="ChatMessageContent"/> and an <see cref="AgentThread"/>.</returns>
/// <remarks>
/// <para>
/// The provided message string will be treated as a user message.
/// </para>
/// <para>
/// To continue this thread in the future, use an <see cref="AgentThread"/> returned in one of the response items.
/// </para>
/// </remarks>
public virtual IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> InvokeStreamingAsync(
string message,
AgentThread? thread = null,
AgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
Verify.NotNull(message);

return this.InvokeStreamingAsync(new ChatMessageContent(AuthorRole.User, message), thread, options, cancellationToken);
}

/// <summary>
/// Invoke the agent with the provided message and arguments.
/// </summary>
Expand All @@ -97,7 +190,9 @@ public virtual IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>>
AgentInvokeOptions? options = null,
CancellationToken cancellationToken = default)
{
return this.InvokeStreamingAsync(new[] { message }, thread, options, cancellationToken);
Verify.NotNull(message);

return this.InvokeStreamingAsync([message], thread, options, cancellationToken);
}

/// <summary>
Expand Down
Loading
Loading