.Net: Add missing providers to tool samples (#211)

* Add missing providers to tool samples

* Add Options ctor and simplify samples a bit

* Remove basic
This commit is contained in:
Roger Barreto
2025-07-24 10:31:24 +01:00
committed by GitHub
Unverified
parent 517955ce66
commit 62be887947
6 changed files with 81 additions and 79 deletions
@@ -33,7 +33,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
IChatClient chatClient = base.GetChatClient(provider);
// Define the agent
AIAgent agent = new ChatClientAgent(chatClient, ParrotInstructions, ParrotName);
AIAgent agent = new ChatClientAgent(chatClient, name: ParrotName, instructions: ParrotInstructions);
// Invoke the agent and output the text result.
Console.WriteLine(await agent.RunAsync("Fortune favors the bold."));
@@ -52,11 +52,7 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
public async Task RunWithoutThread(ChatClientProviders provider)
{
// Define the options for the chat client agent.
var agentOptions = new ChatClientAgentOptions
{
Name = ParrotName,
Instructions = ParrotInstructions,
};
var agentOptions = new ChatClientAgentOptions(name: ParrotName, instructions: ParrotInstructions);
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
@@ -149,11 +145,8 @@ public sealed class Step01_ChatClientAgent_Running(ITestOutputHelper output) : A
public async Task RunStreamingWithThread(ChatClientProviders provider)
{
// Define the options for the chat client agent.
var agentOptions = new ChatClientAgentOptions
var agentOptions = new ChatClientAgentOptions(name: ParrotName, instructions: ParrotInstructions)
{
Name = ParrotName,
Instructions = ParrotInstructions,
// Get chat options based on the store type, if needed.
ChatOptions = base.GetChatOptions(provider),
};
@@ -14,49 +14,27 @@ public sealed class Step02_ChatClientAgent_UsingFunctionTools(ITestOutputHelper
{
[Theory]
[InlineData(ChatClientProviders.AzureOpenAI)]
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
[InlineData(ChatClientProviders.OpenAIAssistant)]
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
public async Task RunningWithToolsBasic(ChatClientProviders provider)
{
// Creating a MenuTools instance to be used by the agent.
var menuTools = new MenuTools();
// Get the chat client to use for the agent.
var chatClient = base.GetChatClient(provider);
// Define the agent and add the GetSpecials tool.
var agent = new ChatClientAgent(
chatClient,
instructions: "Answer questions about the menu.",
tools: [AIFunctionFactory.Create(menuTools.GetSpecials)]);
// Respond to user input, invoking functions where appropriate.
Console.WriteLine(await agent.RunAsync("What is the special soup and its price?"));
}
[Theory]
[InlineData(ChatClientProviders.AzureOpenAI)]
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
[InlineData(ChatClientProviders.OpenAIResponses)]
public async Task RunningWithTools(ChatClientProviders provider)
{
// Creating a MenuTools instance to be used by the agent.
var menuTools = new MenuTools();
// Define the options for the chat client agent.
var agentOptions = new ChatClientAgentOptions
{
Name = "Host",
Instructions = "Answer questions about the menu.",
var agentOptions = new ChatClientAgentOptions(
name: "Host",
instructions: "Answer questions about the menu",
tools: [
AIFunctionFactory.Create(menuTools.GetMenu),
AIFunctionFactory.Create(menuTools.GetSpecials),
AIFunctionFactory.Create(menuTools.GetItemPrice)
]);
// Provide the tools that are available to the agent
ChatOptions = new()
{
Tools = [
AIFunctionFactory.Create(menuTools.GetMenu),
AIFunctionFactory.Create(menuTools.GetSpecials),
AIFunctionFactory.Create(menuTools.GetItemPrice)
]
},
};
// 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 = base.GetChatClient(provider, agentOptions);
@@ -79,32 +57,34 @@ public sealed class Step02_ChatClientAgent_UsingFunctionTools(ITestOutputHelper
var response = await agent.RunAsync(input, thread);
this.WriteResponseOutput(response);
}
// Clean up the server-side agent after use when applicable (depending on the provider).
await base.AgentCleanUpAsync(provider, agent, thread);
}
[Theory]
[InlineData(ChatClientProviders.AzureOpenAI)]
[InlineData(ChatClientProviders.AzureAIAgentsPersistent)]
[InlineData(ChatClientProviders.OpenAIAssistant)]
[InlineData(ChatClientProviders.OpenAIChatCompletion)]
[InlineData(ChatClientProviders.OpenAIResponses)]
public async Task StreamingRunWithTools(ChatClientProviders provider)
{
// Creating a MenuTools instance to be used by the agent.
var menuTools = new MenuTools();
// Define the options for the chat client agent.
var agentOptions = new ChatClientAgentOptions
{
Name = "Host",
Instructions = "Answer questions about the menu.",
var agentOptions = new ChatClientAgentOptions(
name: "Host",
instructions: "Answer questions about the menu",
tools: [
AIFunctionFactory.Create(menuTools.GetMenu),
AIFunctionFactory.Create(menuTools.GetSpecials),
AIFunctionFactory.Create(menuTools.GetItemPrice)
]);
// Provide the tools that are available to the agent
ChatOptions = new()
{
Tools = [
AIFunctionFactory.Create(menuTools.GetMenu),
AIFunctionFactory.Create(menuTools.GetSpecials),
AIFunctionFactory.Create(menuTools.GetItemPrice)
]
},
};
// 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 = base.GetChatClient(provider, agentOptions);
@@ -129,6 +109,9 @@ public sealed class Step02_ChatClientAgent_UsingFunctionTools(ITestOutputHelper
this.WriteAgentOutput(update);
}
}
// Clean up the server-side agent after use when applicable (depending on the provider).
await base.AgentCleanUpAsync(provider, agent, thread);
}
private sealed class MenuTools
@@ -23,12 +23,11 @@ public sealed class Step03_ChatClientAgent_UsingCodeInterpreterTools(ITestOutput
{
var codeInterpreterTool = new NewHostedCodeInterpreterTool();
codeInterpreterTool.FileIds.Add(await UploadFileAsync("Resources/groceries.txt", provider));
var agentOptions = new ChatClientAgentOptions
{
Name = "HelpfulAssistant",
Instructions = "You are a helpful assistant.",
ChatOptions = new() { Tools = [codeInterpreterTool] }
};
var agentOptions = new ChatClientAgentOptions(
name: "HelpfulAssistant",
instructions: "You are a helpful assistant.",
tools: [codeInterpreterTool]);
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
@@ -19,14 +19,9 @@ public sealed class Step04_ChatClientAgent_DependencyInjection(ITestOutputHelper
// 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),
};
var agentOptions = new ChatClientAgentOptions(
name: "Parrot",
instructions: "Repeat the user message in the voice of a pirate and then end with a parrot sound.");
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
@@ -33,12 +33,8 @@ public sealed class Step05_ChatClientAgent_Telemetry(ITestOutputHelper output) :
.AddConsoleExporter()
.Build();
// Define agent
var agentOptions = new ChatClientAgentOptions
{
Name = "TelemetryAgent",
Instructions = "You are a helpful assistant.",
};
// Define agent options
var agentOptions = new ChatClientAgentOptions(name: "TelemetryAgent", instructions: "You are a helpful assistant.");
// Create the server-side agent Id when applicable (depending on the provider).
agentOptions.Id = await base.AgentCreateAsync(provider, agentOptions);
@@ -1,5 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
namespace Microsoft.Extensions.AI.Agents;
/// <summary>
@@ -12,6 +14,40 @@ namespace Microsoft.Extensions.AI.Agents;
/// </remarks>
public class ChatClientAgentOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="ChatClientAgentOptions"/> class.
/// </summary>
public ChatClientAgentOptions()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ChatClientAgentOptions"/> class with the specified parameters.
/// </summary>
/// <remarks>If <paramref name="tools"/> is provided, a new <see cref="ChatOptions"/> instance is created
/// with the specified instructions and tools.</remarks>
/// <param name="instructions">The instructions or guidelines for the chat client agent. Can be <see langword="null"/> if not specified.</param>
/// <param name="name">The name of the chat client agent. Can be <see langword="null"/> if not specified.</param>
/// <param name="description">The description of the chat client agent. Can be <see langword="null"/> if not specified.</param>
/// <param name="tools">A list of <see cref="AITool"/> instances available to the chat client agent. Can be <see langword="null"/> if no
/// tools are specified.</param>
public ChatClientAgentOptions(string? instructions, string? name = null, string? description = null, IList<AITool>? tools = null)
{
this.Name = name;
this.Instructions = instructions;
this.Description = description;
if (tools is not null)
{
(this.ChatOptions ??= new()).Tools = tools;
}
if (instructions is not null)
{
(this.ChatOptions ??= new()).Instructions = instructions;
}
}
/// <summary>
/// Gets or sets the agent id.
/// </summary>