Files
Mark Wallace f74131ef61 .NET: Add FoundryAgentFactory which uses AgentClient (#2123)
* Add FoundryAgentFactory which uses AgentClient

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/FoundryAgentFactory.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/FoundryAgentFactory.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/FoundryAgentFactory.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/FoundryAgentFactory.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/FunctionToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/FileSearchToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/CodeInterpreterToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/PromptAgentExtensions.cs

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

* Code review feedback

* Add sample showing use of FoundryAgentFactory

* Add sample showing use of FoundryAgentFactory

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/CodeInterpreterToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/FunctionToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/McpServerToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/CodeInterpreterToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/FileSearchToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/WebSearchToolExtensions.cs

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

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/Extensions/PromptAgentExtensions.cs

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

* Fix issue to get FoundryAgentFactory sample working

* Run dotnet format

* Undo changes made by dotnet format

* Update dotnet/src/Microsoft.Agents.AI.Declarative.AzureAI/FoundryAgentFactory.cs

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

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 14:15:15 +00:00

62 lines
2.3 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
// This sample shows how to load an AI agent from a YAML file and process a prompt using Foundry Agents as the backend.
using System.ComponentModel;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
// Read command-line arguments
if (args.Length < 2)
{
Console.WriteLine("Usage: DeclarativeAgents <yaml-file-path> <prompt>");
Console.WriteLine(" <yaml-file-path>: The path to the YAML file containing the agent definition");
Console.WriteLine(" <prompt>: The prompt to send to the agent");
return;
}
var yamlFilePath = args[0];
var prompt = args[1];
// Verify the YAML file exists
if (!File.Exists(yamlFilePath))
{
Console.WriteLine($"Error: File not found: {yamlFilePath}");
return;
}
// Read the YAML content from the file
var text = await File.ReadAllTextAsync(yamlFilePath);
// Set up configuration with the Azure Foundry project endpoint
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["AZURE_FOUNDRY_PROJECT_ENDPOINT"] = endpoint
})
.Build();
// Example function tool that can be used by the agent.
[Description("Get the weather for a given location.")]
static string GetWeather(
[Description("The city and state, e.g. San Francisco, CA")] string location,
[Description("The unit of temperature. Possible values are 'celsius' and 'fahrenheit'.")] string unit)
=> $"The weather in {location} is cloudy with a high of {(unit.Equals("celsius", StringComparison.Ordinal) ? "15°C" : "59°F")}.";
// Create the agent from the YAML definition.
var agentFactory = new FoundryPersistentAgentFactory(new AzureCliCredential(), configuration);
var agent = await agentFactory.CreateFromYamlAsync(text);
// Create agent run options
var options = new ChatClientAgentRunOptions(new()
{
Tools = [AIFunctionFactory.Create(GetWeather, name: nameof(GetWeather))]
});
// Invoke the agent and output the text result.
Console.WriteLine(await agent!.RunAsync(prompt, options: options));