.NET: Fix error for ChatClientAgent DI ambiguity (#299)

* Fix error for ChatClientAgent DI ambiguity

* Using AIAgent abstraction for DI samples

* Update dotnet/samples/GettingStarted/Steps/Step04_ChatClientAgent_DependencyInjection.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix warnings

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Roger Barreto
2025-08-05 10:08:45 +01:00
committed by GitHub
Unverified
parent 544d2ba48f
commit cbb05e210f
2 changed files with 15 additions and 8 deletions
+5 -5
View File
@@ -65,7 +65,7 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
/// <remarks>
/// Ideally for faster execution and potential cost savings, server-side agents should be reused.
/// </remarks>
protected Task AgentCleanUpAsync(ChatClientProviders provider, ChatClientAgent agent, AgentThread? thread = null, CancellationToken cancellationToken = default)
protected Task AgentCleanUpAsync(ChatClientProviders provider, AIAgent agent, AgentThread? thread = null, CancellationToken cancellationToken = default)
{
return provider switch
{
@@ -157,9 +157,9 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
#region Private AgentCleanUp
private async Task AzureAIAgentsPersistentAgentCleanUpAsync(ChatClientAgent agent, AgentThread? thread, CancellationToken cancellationToken)
private async Task AzureAIAgentsPersistentAgentCleanUpAsync(AIAgent agent, AgentThread? thread, CancellationToken cancellationToken)
{
var persistentAgentsClient = agent.ChatClient.GetService<PersistentAgentsClient>() ??
var persistentAgentsClient = (agent as ChatClientAgent)?.ChatClient.GetService<PersistentAgentsClient>() ??
throw new InvalidOperationException("The provided chat client is not a Persistent Agents Chat Client");
await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id, cancellationToken);
@@ -171,9 +171,9 @@ public class AgentSample(ITestOutputHelper output) : BaseSample(output)
}
}
private async Task OpenAIAssistantCleanUpAgentAsync(ChatClientAgent agent, AgentThread? thread, CancellationToken cancellationToken)
private async Task OpenAIAssistantCleanUpAgentAsync(AIAgent agent, AgentThread? thread, CancellationToken cancellationToken)
{
var assistantClient = agent.ChatClient
var assistantClient = (agent as ChatClientAgent)?.ChatClient
.GetService<AssistantClient>()
?? throw new InvalidOperationException("The provided chat client is not an OpenAI Assistant Chat Client");
@@ -3,6 +3,7 @@
using Microsoft.Extensions.AI;
using Microsoft.Extensions.AI.Agents;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Steps;
@@ -23,6 +24,8 @@ public sealed class Step04_ChatClientAgent_DependencyInjection(ITestOutputHelper
name: "Parrot",
instructions: "Repeat the user message in the voice of a pirate and then end with a parrot sound.");
services.AddLogging();
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
@@ -30,18 +33,22 @@ public sealed class Step04_ChatClientAgent_DependencyInjection(ITestOutputHelper
services.AddChatClient((sp) => base.GetChatClient(provider, sp.GetRequiredService<ChatClientAgentOptions>()));
services.AddSingleton<ChatClientAgent>();
services.AddSingleton<AIAgent>((sp)
=> new ChatClientAgent(
chatClient: sp.GetRequiredService<IChatClient>(),
options: sp.GetRequiredService<ChatClientAgentOptions>(),
loggerFactory: sp.GetRequiredService<ILoggerFactory>()));
// Build the service provider.
using var serviceProvider = services.BuildServiceProvider();
// Get the agent from the service provider.
var agent = serviceProvider.GetRequiredService<ChatClientAgent>();
var agent = serviceProvider.GetRequiredService<AIAgent>();
// Create the chat history thread to capture the agent interaction.
var thread = agent.GetNewThread();
Console.WriteLine($"Using chat client for provider: {agent.ChatClient.GetService<ChatClientMetadata>()!.ProviderName}, for agent: {agent.Name}");
Console.WriteLine($"Using chat client for provider: {provider}");
// Respond to user input, invoking functions where appropriate.
await RunAgentAsync("Tell me a joke about a pirate.");