Skip to content

Commit

Permalink
.Net Agents - Make messages parameter optional for `OpenAIAssistant…
Browse files Browse the repository at this point in the history
…Agent.InvokeStreamingAsync` (#8915)

### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Customer input identified that `messages` parameter may not always be
required by the caller.

Fixes: #8857

### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

Providing the complete messages is a convenience and shouldn't be
required.

### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->

- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄
  • Loading branch information
crickman authored Sep 19, 2024
1 parent 51639f7 commit 6bba2b6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, string threadId,
ChatHistory history = [];

bool isFirst = false;
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(threadId, history))
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(threadId, messages: history))
{
if (string.IsNullOrEmpty(response.Content))
{
Expand Down
8 changes: 4 additions & 4 deletions dotnet/src/Agents/OpenAI/Internal/AssistantThreadActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public static async IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamin
OpenAIAssistantAgent agent,
AssistantClient client,
string threadId,
IList<ChatMessageContent> messages,
IList<ChatMessageContent>? messages,
OpenAIAssistantInvocationOptions? invocationOptions,
ILogger logger,
Kernel kernel,
Expand Down Expand Up @@ -413,7 +413,7 @@ public static async IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamin
if (functionCalls.Length > 0)
{
// Emit function-call content
messages.Add(GenerateFunctionCallContent(agent.GetName(), functionCalls));
messages?.Add(GenerateFunctionCallContent(agent.GetName(), functionCalls));

// Invoke functions for each tool-step
IEnumerable<Task<FunctionResultContent>> functionResultTasks = ExecuteFunctionSteps(agent, functionCalls, cancellationToken);
Expand All @@ -425,7 +425,7 @@ public static async IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamin
ToolOutput[] toolOutputs = GenerateToolOutputs(functionResults);
asyncUpdates = client.SubmitToolOutputsToRunStreamingAsync(run, toolOutputs);

messages.Add(GenerateFunctionResultsContent(agent.GetName(), functionResults));
messages?.Add(GenerateFunctionResultsContent(agent.GetName(), functionResults));
}
}

Expand All @@ -440,7 +440,7 @@ public static async IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamin
if (message != null)
{
ChatMessageContent content = GenerateMessageContent(agent.GetName(), message);
messages.Add(content);
messages?.Add(content);
}
}

Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/Agents/OpenAI/OpenAIAssistantAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,41 +298,41 @@ public async IAsyncEnumerable<ChatMessageContent> InvokeAsync(
/// Invoke the assistant on the specified thread.
/// </summary>
/// <param name="threadId">The thread identifier</param>
/// <param name="messages">The receiver for the completed messages generated</param>
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
/// <param name="messages">Optional receiver of the completed messages generated</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>Asynchronous enumeration of messages.</returns>
/// <remarks>
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
/// </remarks>
public IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(
string threadId,
ChatHistory messages,
KernelArguments? arguments = null,
Kernel? kernel = null,
ChatHistory? messages = null,
CancellationToken cancellationToken = default)
=> this.InvokeStreamingAsync(threadId, messages, options: null, arguments, kernel, cancellationToken);
=> this.InvokeStreamingAsync(threadId, options: null, arguments, kernel, messages, cancellationToken);

/// <summary>
/// Invoke the assistant on the specified thread.
/// </summary>
/// <param name="threadId">The thread identifier</param>
/// <param name="messages">The receiver for the completed messages generated</param>
/// <param name="options">Optional invocation options</param>
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
/// <param name="messages">Optional receiver of the completed messages generated</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>Asynchronous enumeration of messages.</returns>
/// <remarks>
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
/// </remarks>
public IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(
string threadId,
ChatHistory messages,
OpenAIAssistantInvocationOptions? options,
KernelArguments? arguments = null,
Kernel? kernel = null,
ChatHistory? messages = null,
CancellationToken cancellationToken = default)
{
this.ThrowIfDeleted();
Expand Down

0 comments on commit 6bba2b6

Please sign in to comment.