mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.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:
committed by
GitHub
Unverified
parent
544d2ba48f
commit
cbb05e210f
@@ -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.");
|
||||
|
||||
Reference in New Issue
Block a user