Skip to content

Commit b8c4486

Browse files
.Net: Remove obsoleted code for agent abstractions (#12391)
### Description Remove obsoleted code for agent abstractions. I also had to add some additional unit tests to keep the code coverage rate above the threshold. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] 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 - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 --------- Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
1 parent 2036d81 commit b8c4486

15 files changed

+124
-1536
lines changed

dotnet/samples/Concepts/Agents/ChatCompletion_ServiceSelection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Agents;
1111
/// <summary>
1212
/// Demonstrate service selection for <see cref="ChatCompletionAgent"/> through setting service-id
1313
/// on <see cref="Agent.Arguments"/> and also providing override <see cref="KernelArguments"/>
14-
/// when calling <see cref="ChatCompletionAgent.InvokeAsync(ChatHistory, KernelArguments?, Kernel?, CancellationToken)"/>
14+
/// when calling <see cref="ChatCompletionAgent.InvokeAsync(ICollection{ChatMessageContent}, AgentThread?, AgentInvokeOptions?, CancellationToken)"/>
1515
/// </summary>
1616
public class ChatCompletion_ServiceSelection(ITestOutputHelper output) : BaseAgentsTest(output)
1717
{

dotnet/src/Agents/AzureAI/AzureAIAgent.cs

Lines changed: 14 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -96,54 +96,6 @@ public AzureAIAgent(
9696
/// </summary>
9797
public PersistentAgentsClient Client { get; }
9898

99-
/// <summary>
100-
/// Adds a message to the specified thread.
101-
/// </summary>
102-
/// <param name="threadId">The thread identifier.</param>
103-
/// <param name="message">A non-system message to append to the conversation.</param>
104-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
105-
/// <remarks>
106-
/// Only supports messages with <see href="https://platform.openai.com/docs/api-reference/runs/createRun#runs-createrun-additional_messages">role = User or agent</see>.
107-
/// </remarks>
108-
[Obsolete("Pass messages directly to Invoke instead. This method will be removed after May 1st 2025.")]
109-
public Task AddChatMessageAsync(string threadId, ChatMessageContent message, CancellationToken cancellationToken = default)
110-
{
111-
return AgentThreadActions.CreateMessageAsync(this.Client, threadId, message, cancellationToken);
112-
}
113-
114-
/// <summary>
115-
/// Gets messages for a specified thread.
116-
/// </summary>
117-
/// <param name="threadId">The thread identifier.</param>
118-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
119-
/// <returns>An asynchronous enumeration of messages.</returns>
120-
[Obsolete("Use the AzureAIAgentThread to retrieve messages instead. This method will be removed after May 1st 2025.")]
121-
public IAsyncEnumerable<ChatMessageContent> GetThreadMessagesAsync(string threadId, CancellationToken cancellationToken = default)
122-
{
123-
return AgentThreadActions.GetMessagesAsync(this.Client, threadId, null, cancellationToken);
124-
}
125-
126-
/// <summary>
127-
/// Invokes the assistant on the specified thread.
128-
/// </summary>
129-
/// <param name="threadId">The thread identifier.</param>
130-
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
131-
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
132-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
133-
/// <returns>An asynchronous enumeration of response messages.</returns>
134-
/// <remarks>
135-
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
136-
/// </remarks>
137-
[Obsolete("Use InvokeAsync with AgentThread instead. This method will be removed after May 1st 2025.")]
138-
public IAsyncEnumerable<ChatMessageContent> InvokeAsync(
139-
string threadId,
140-
KernelArguments? arguments = null,
141-
Kernel? kernel = null,
142-
CancellationToken cancellationToken = default)
143-
{
144-
return this.InvokeAsync(threadId, options: null, arguments, kernel, cancellationToken);
145-
}
146-
14799
/// <inheritdoc/>
148100
public override IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> InvokeAsync(
149101
ICollection<ChatMessageContent> messages,
@@ -236,44 +188,6 @@ async IAsyncEnumerable<ChatMessageContent> InternalInvokeAsync()
236188
}
237189
}
238190

239-
/// <summary>
240-
/// Invokes the assistant on the specified thread.
241-
/// </summary>
242-
/// <param name="threadId">The thread identifier.</param>
243-
/// <param name="options">Optional invocation options.</param>
244-
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
245-
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
246-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
247-
/// <returns>An asynchronous enumeration of response messages.</returns>
248-
/// <remarks>
249-
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
250-
/// </remarks>
251-
[Obsolete("Use InvokeAsync with AgentThread instead. This method will be removed after May 1st 2025.")]
252-
public IAsyncEnumerable<ChatMessageContent> InvokeAsync(
253-
string threadId,
254-
AzureAIInvocationOptions? options,
255-
KernelArguments? arguments = null,
256-
Kernel? kernel = null,
257-
CancellationToken cancellationToken = default)
258-
{
259-
return ActivityExtensions.RunWithActivityAsync(
260-
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
261-
() => InternalInvokeAsync(),
262-
cancellationToken);
263-
264-
async IAsyncEnumerable<ChatMessageContent> InternalInvokeAsync()
265-
{
266-
kernel ??= this.Kernel;
267-
await foreach ((bool isVisible, ChatMessageContent message) in AgentThreadActions.InvokeAsync(this, this.Client, threadId, options, this.Logger, kernel, arguments, cancellationToken).ConfigureAwait(false))
268-
{
269-
if (isVisible)
270-
{
271-
yield return message;
272-
}
273-
}
274-
}
275-
}
276-
277191
/// <inheritdoc/>
278192
public override IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> InvokeStreamingAsync(
279193
ICollection<ChatMessageContent> messages,
@@ -328,17 +242,22 @@ public async IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> In
328242
new AzureAIAgentInvokeOptions() { AdditionalInstructions = mergedAdditionalInstructions } :
329243
new AzureAIAgentInvokeOptions(options) { AdditionalInstructions = mergedAdditionalInstructions };
330244

331-
#pragma warning disable CS0618 // Type or member is obsolete
332-
// Invoke the Agent with the thread that we already added our message to.
245+
// Invoke the Agent with the thread that we already added our message to, and with
246+
// a chat history to receive complete messages.
333247
var newMessagesReceiver = new ChatHistory();
334-
var invokeResults = this.InvokeStreamingAsync(
335-
azureAIAgentThread.Id!,
336-
extensionsContextOptions.ToAzureAIInvocationOptions(),
337-
options?.KernelArguments,
338-
kernel,
339-
newMessagesReceiver,
248+
var invokeResults = ActivityExtensions.RunWithActivityAsync(
249+
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
250+
() => AgentThreadActions.InvokeStreamingAsync(
251+
this,
252+
this.Client,
253+
azureAIAgentThread.Id!,
254+
newMessagesReceiver,
255+
extensionsContextOptions.ToAzureAIInvocationOptions(),
256+
this.Logger,
257+
kernel,
258+
options?.KernelArguments,
259+
cancellationToken),
340260
cancellationToken);
341-
#pragma warning restore CS0618 // Type or member is obsolete
342261

343262
// Return the chunks to the caller.
344263
await foreach (var result in invokeResults.ConfigureAwait(false))
@@ -358,63 +277,6 @@ public async IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> In
358277
}
359278
}
360279

361-
/// <summary>
362-
/// Invokes the assistant on the specified thread with streaming response.
363-
/// </summary>
364-
/// <param name="threadId">The thread identifier.</param>
365-
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
366-
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
367-
/// <param name="messages">Optional receiver of the completed messages that are generated.</param>
368-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
369-
/// <returns>An asynchronous enumeration of messages.</returns>
370-
/// <remarks>
371-
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
372-
/// </remarks>
373-
[Obsolete("Use InvokeStreamingAsync with AgentThread instead. This method will be removed after May 1st 2025.")]
374-
public IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(
375-
string threadId,
376-
KernelArguments? arguments = null,
377-
Kernel? kernel = null,
378-
ChatHistory? messages = null,
379-
CancellationToken cancellationToken = default)
380-
{
381-
return this.InvokeStreamingAsync(threadId, options: null, arguments, kernel, messages, cancellationToken);
382-
}
383-
384-
/// <summary>
385-
/// Invokes the assistant on the specified thread with streaming response.
386-
/// </summary>
387-
/// <param name="threadId">The thread identifier.</param>
388-
/// <param name="options">Optional invocation options.</param>
389-
/// <param name="arguments">Optional arguments to pass to the agents's invocation, including any <see cref="PromptExecutionSettings"/>.</param>
390-
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use by the agent.</param>
391-
/// <param name="messages">Optional receiver of the completed messages that are generated.</param>
392-
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
393-
/// <returns>An asynchronous enumeration of messages.</returns>
394-
/// <remarks>
395-
/// The `arguments` parameter is not currently used by the agent, but is provided for future extensibility.
396-
/// </remarks>
397-
[Obsolete("Use InvokeStreamingAsync with AgentThread instead. This method will be removed after May 1st 2025.")]
398-
public IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(
399-
string threadId,
400-
AzureAIInvocationOptions? options,
401-
KernelArguments? arguments = null,
402-
Kernel? kernel = null,
403-
ChatHistory? messages = null,
404-
CancellationToken cancellationToken = default)
405-
{
406-
return ActivityExtensions.RunWithActivityAsync(
407-
() => ModelDiagnostics.StartAgentInvocationActivity(this.Id, this.GetDisplayName(), this.Description),
408-
() => InternalInvokeStreamingAsync(),
409-
cancellationToken);
410-
411-
IAsyncEnumerable<StreamingChatMessageContent> InternalInvokeStreamingAsync()
412-
{
413-
kernel ??= this.Kernel;
414-
return AgentThreadActions.InvokeStreamingAsync(this, this.Client, threadId, messages, options, this.Logger, kernel, arguments, cancellationToken);
415-
}
416-
}
417-
418280
/// <inheritdoc/>
419281
protected override IEnumerable<string> GetChannelKeys()
420282
{

0 commit comments

Comments
 (0)