.Net: Introduce Dependency Injection Samples (#165)

* DI WIP

* Update dependency injection examples and Agent Creation update

* Rollback override for GettingStarted as Azure.AI.OpenAI package currentl does not support OpenAI 2.2.0 GA version

* Dropping ct

* Update dotnet/samples/GettingStarted/AgentSample.cs

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

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Roger Barreto
2025-07-11 14:27:07 +01:00
committed by GitHub
Unverified
parent 715769e649
commit eca0eb9cd0
6 changed files with 183 additions and 71 deletions
@@ -20,10 +20,11 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
/// a unique interaction with no conversation history between them.
/// </summary>
[Theory]
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
[InlineData(ChatClientProviders.AzureOpenAI)]
[InlineData(ChatClientProviders.OpenAIAssistant)]
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
[InlineData(ChatClientProviders.OpenAIResponses)]
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
public async Task RunWithoutThread(ChatClientProviders provider)
{
// Define the options for the chat client agent.
@@ -33,8 +34,11 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
Instructions = ParrotInstructions,
};
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
// Get the chat client to use for the agent.
using var chatClient = await base.GetChatClientAsync(provider, agentOptions);
using var chatClient = base.GetChatClient(provider, agentOptions);
// Define the agent
var agent = new ChatClientAgent(chatClient, agentOptions);
@@ -53,7 +57,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
this.WriteResponseOutput(response);
}
// Clean up the agent after use when applicable.
// Clean up the server-side agent after use when applicable (depending on the provider).
await base.AgentCleanUpAsync(provider, agent);
}
@@ -61,8 +65,9 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
/// Demonstrate the usage of <see cref="ChatClientAgent"/> where a conversation history is maintained.
/// </summary>
[Theory]
[InlineData(ChatClientProviders.AzureOpenAI)]
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
[InlineData(ChatClientProviders.AzureOpenAI)]
[InlineData(ChatClientProviders.OpenAIAssistant)]
[InlineData(ChatClientProviders.OpenAIResponses_InMemoryMessageThread)]
[InlineData(ChatClientProviders.OpenAIResponses_ConversationIdThread)]
public async Task RunWithThread(ChatClientProviders provider)
@@ -77,8 +82,11 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
ChatOptions = base.GetChatOptions(provider),
};
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
// Get the chat client to use for the agent.
using var chatClient = await base.GetChatClientAsync(provider, agentOptions);
using var chatClient = base.GetChatClient(provider, agentOptions);
// Define the agent
var agent = new ChatClientAgent(chatClient, agentOptions);
@@ -100,7 +108,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
this.WriteResponseOutput(response);
}
// Clean up the agent and thread after use when applicable.
// Clean up the server-side agent and thread after use when applicable (depending on the provider).
await base.AgentCleanUpAsync(provider, agent, thread);
}
@@ -111,6 +119,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
[Theory]
[InlineData(ChatClientProviders.AzureOpenAI)]
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
[InlineData(ChatClientProviders.OpenAIAssistant)]
[InlineData(ChatClientProviders.OpenAIResponses_InMemoryMessageThread)]
[InlineData(ChatClientProviders.OpenAIResponses_ConversationIdThread)]
public async Task RunStreamingWithThread(ChatClientProviders provider)
@@ -125,8 +134,11 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
ChatOptions = base.GetChatOptions(provider),
};
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
// Get the chat client to use for the agent.
using var chatClient = await base.GetChatClientAsync(provider, agentOptions);
using var chatClient = base.GetChatClient(provider, agentOptions);
// Define the agent
var agent = new ChatClientAgent(chatClient, agentOptions);
@@ -149,7 +161,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
}
}
// Clean up the agent and thread after use when applicable.
// Clean up the server-side agent and thread after use when applicable (depending on the provider).
await base.AgentCleanUpAsync(provider, agent, thread);
}
}
@@ -38,7 +38,7 @@ public sealed class Step02_ChatClientAgent_UsingFunctionTools(ITestOutputHelper
};
// Get the chat client to use for the agent.
using var chatClient = await base.GetChatClientAsync(provider, agentOptions);
using var chatClient = base.GetChatClient(provider, agentOptions);
// Define the agent
var agent = new ChatClientAgent(chatClient, agentOptions);
@@ -86,7 +86,7 @@ public sealed class Step02_ChatClientAgent_UsingFunctionTools(ITestOutputHelper
};
// Get the chat client to use for the agent.
using var chatClient = await base.GetChatClientAsync(provider, agentOptions);
using var chatClient = base.GetChatClient(provider, agentOptions);
// Define the agent
var agent = new ChatClientAgent(chatClient, agentOptions);
@@ -17,13 +17,12 @@ namespace Steps;
public sealed class Step03_ChatClientAgent_UsingCodeInterpreterTools(ITestOutputHelper output) : AgentSample(output)
{
[Theory]
[InlineData(ChatClientProviders.OpenAIAssistant)]
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
[InlineData(ChatClientProviders.OpenAIAssistant)]
public async Task RunningWithFileReferenceAsync(ChatClientProviders provider)
{
var codeInterpreterTool = new NewHostedCodeInterpreterTool();
codeInterpreterTool.FileIds.Add(await UploadFileAsync("Resources/groceries.txt", provider));
var agentOptions = new ChatClientAgentOptions
{
Name = "HelpfulAssistant",
@@ -31,7 +30,10 @@ public sealed class Step03_ChatClientAgent_UsingCodeInterpreterTools(ITestOutput
ChatOptions = new() { Tools = [codeInterpreterTool] }
};
using var chatClient = await base.GetChatClientAsync(provider, agentOptions);
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
using var chatClient = base.GetChatClient(provider, agentOptions);
ChatClientAgent agent = new(chatClient, agentOptions);
@@ -61,6 +63,9 @@ public sealed class Step03_ChatClientAgent_UsingCodeInterpreterTools(ITestOutput
Console.WriteLine("Code interpreter Output:");
Console.WriteLine(codeInterpreterOutput.ToString());
// Clean up the server-side agent after use when applicable (depending on the provider).
await base.AgentCleanUpAsync(provider, agent, thread);
}
#region private
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.AI;
using Microsoft.Extensions.AI.Agents;
using Microsoft.Extensions.DependencyInjection;
namespace Steps;
public sealed class Step04_ChatClientAgent_DependencyInjection(ITestOutputHelper output) : AgentSample(output)
{
[Theory]
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
[InlineData(ChatClientProviders.AzureOpenAI)]
[InlineData(ChatClientProviders.OpenAIAssistant)]
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
[InlineData(ChatClientProviders.OpenAIResponses)]
public async Task RunningWithServiceCollection(ChatClientProviders provider)
{
// Adding multiple chat clients to the service collection.
var services = new ServiceCollection();
var agentOptions = new ChatClientAgentOptions
{
Name = "Parrot",
Instructions = "Repeat the user message in the voice of a pirate and then end with a parrot sound.",
// Get chat options based on the store type, if needed.
ChatOptions = base.GetChatOptions(provider),
};
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
services.AddSingleton(agentOptions);
services.AddChatClient((sp) => base.GetChatClient(provider, sp.GetRequiredService<ChatClientAgentOptions>()));
services.AddSingleton<ChatClientAgent>();
// Build the service provider.
using var serviceProvider = services.BuildServiceProvider();
// Get the agent from the service provider.
var agent = serviceProvider.GetRequiredService<ChatClientAgent>();
// 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}");
// Respond to user input, invoking functions where appropriate.
await RunAgentAsync("Tell me a joke about a pirate.");
await RunAgentAsync("Now add some emojis to the joke.");
async Task RunAgentAsync(string input)
{
this.WriteUserMessage(input);
var response = await agent.RunAsync(input, thread);
this.WriteResponseOutput(response);
}
// Clean up the agent and thread after use when applicable (depending on the provider).
await base.AgentCleanUpAsync(provider, agent, thread);
}
}