mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Declarative Agents (#1301)
* AgentFactory abstractions and ChatClient implementation * Add a getitng started sample * Update to latest M.B.OM * Add some additional samples * Work in progress * Merge latest from main * Start to add support for using different kinds of connections * Remove IsSupported * Remove IsSupported * Refactor code to create clients to support DI * Add some unit tests * Update based on the latest code review feedback * Add support for OOB tools when using persistent agent sdk * Fix sample naming * Fix error based on latest MEAI * Update M.B.OM package to latest * Update to the latest M.B.OM release * Remove some obsolete helper methods * Update to the latest M.B.OM version * Fix broken unit test * Update MCP sample * Bump to latest M.B.OM release * Update to latest M.B.OM release * Update to latest M.B.OM release * Switch to using ExternalModel * Update to latest M.B.OM * Resolve merge conflicts * All tests pass * All tests pass * Start to clean up the code * Start to clean up the code * More clean up * More clean up * More clean up * Fix apiType checks * Run dotnet format * Fix typo * Address code review feedback * Add all properties for MCP tool * Address code review feedback * Address code review feedback * Fix merge * Undo warnings * Undo test change * More copilot feedback * Make class sealed * Address additional core review feedback --------- Co-authored-by: Mark Wallace <markwallace@microsoft.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
105dc82c39
commit
aaa91954c5
+25
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.AI.OpenAI" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.Json" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.PowerFx" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Declarative\Microsoft.Agents.AI.Declarative.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,54 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
// This sample shows how to create an agent from a YAML based declarative representation.
|
||||
|
||||
using Azure.AI.OpenAI;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Extensions.AI;
|
||||
|
||||
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
|
||||
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
|
||||
|
||||
// Create the chat client
|
||||
IChatClient chatClient = new AzureOpenAIClient(
|
||||
new Uri(endpoint),
|
||||
new AzureCliCredential())
|
||||
.GetChatClient(deploymentName)
|
||||
.AsIChatClient();
|
||||
|
||||
// Define the agent using a YAML definition.
|
||||
var text =
|
||||
"""
|
||||
kind: Prompt
|
||||
name: Assistant
|
||||
description: Helpful assistant
|
||||
instructions: You are a helpful assistant. You answer questions in the language specified by the user. You return your answers in a JSON format.
|
||||
model:
|
||||
options:
|
||||
temperature: 0.9
|
||||
topP: 0.95
|
||||
outputSchema:
|
||||
properties:
|
||||
language:
|
||||
type: string
|
||||
required: true
|
||||
description: The language of the answer.
|
||||
answer:
|
||||
type: string
|
||||
required: true
|
||||
description: The answer text.
|
||||
""";
|
||||
|
||||
// Create the agent from the YAML definition.
|
||||
var agentFactory = new ChatClientAgentFactory(chatClient);
|
||||
var agent = await agentFactory.CreateFromYamlAsync(text);
|
||||
|
||||
// Invoke the agent and output the text result.
|
||||
Console.WriteLine(await agent!.RunAsync("Tell me a joke about a pirate in English."));
|
||||
|
||||
// Invoke the agent with streaming support.
|
||||
await foreach (var update in agent!.RunStreamingAsync("Tell me a joke about a pirate in French."))
|
||||
{
|
||||
Console.WriteLine(update);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="Azure.AI.Agents.Persistent" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.Json" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.PowerFx" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Declarative.AzureAI\Microsoft.Agents.AI.Declarative.AzureAI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,64 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
// This sample shows how to load an AI agent from a YAML file and process a prompt using Azure OpenAI as the backend.
|
||||
|
||||
using System.ComponentModel;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Extensions.AI;
|
||||
|
||||
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
|
||||
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
|
||||
|
||||
// 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);
|
||||
|
||||
// TODO: Remove this workaround when the agent framework supports environment variable substitution in YAML files.
|
||||
text = text.Replace("=Env.AZURE_OPENAI_DEPLOYMENT_NAME", deploymentName, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
var endpointUri = new Uri(endpoint);
|
||||
var tokenCredential = new AzureCliCredential();
|
||||
|
||||
// Create the agent from the YAML definition.
|
||||
var agentFactory = new AggregatorAgentFactory(
|
||||
[
|
||||
new OpenAIChatAgentFactory(endpointUri, tokenCredential),
|
||||
new OpenAIResponseAgentFactory(endpointUri, tokenCredential),
|
||||
new OpenAIAssistantAgentFactory(endpointUri, tokenCredential)
|
||||
]);
|
||||
var agent = await agentFactory.CreateFromYamlAsync(text);
|
||||
|
||||
// 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 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));
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.AI.OpenAI" />
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.Json" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.PowerFx" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Declarative\Microsoft.Agents.AI.Declarative.csproj" />
|
||||
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
// This sample shows how to load an AI agent from a YAML file and process a prompt using Azure OpenAI as the backend.
|
||||
|
||||
using System.ComponentModel;
|
||||
using Azure.AI.OpenAI;
|
||||
using Azure.Identity;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Extensions.AI;
|
||||
|
||||
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
|
||||
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
|
||||
|
||||
// Create the chat client
|
||||
IChatClient chatClient = new AzureOpenAIClient(
|
||||
new Uri(endpoint),
|
||||
new AzureCliCredential())
|
||||
.GetChatClient(deploymentName)
|
||||
.AsIChatClient();
|
||||
|
||||
// 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);
|
||||
|
||||
// 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 ChatClientAgentFactory(chatClient, [AIFunctionFactory.Create(GetWeather, "GetWeather")]);
|
||||
var agent = await agentFactory.CreateFromYamlAsync(text);
|
||||
|
||||
// Invoke the agent and output the text result.
|
||||
Console.WriteLine(await agent!.RunAsync(prompt));
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="Azure.AI.Agents.Persistent" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.Json" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.PowerFx" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Declarative.AzureAI\Microsoft.Agents.AI.Declarative.AzureAI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,57 @@
|
||||
// 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;
|
||||
|
||||
var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
|
||||
var model = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_MODEL_ID") ?? "gpt-4.1-mini";
|
||||
|
||||
// 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);
|
||||
|
||||
// TODO: Remove this workaround when the agent framework supports environment variable substitution in YAML files.
|
||||
text = text.Replace("=Env.AZURE_FOUNDRY_PROJECT_ENDPOINT", endpoint, StringComparison.OrdinalIgnoreCase);
|
||||
text = text.Replace("=Env.AZURE_FOUNDRY_PROJECT_MODEL_ID", model, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
// 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());
|
||||
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));
|
||||
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.Identity" />
|
||||
<PackageReference Include="Azure.AI.Agents.Persistent" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.Json" />
|
||||
<PackageReference Include="Microsoft.Bot.ObjectModel.PowerFx" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Declarative.AzureAI\Microsoft.Agents.AI.Declarative.AzureAI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,61 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
// This sample shows how to load an AI agent from a YAML file and process a prompt using OpenAI as the backend.
|
||||
|
||||
using System.ComponentModel;
|
||||
using Microsoft.Agents.AI;
|
||||
using Microsoft.Extensions.AI;
|
||||
|
||||
var apiKey = Environment.GetEnvironmentVariable("OPENAI_APIKEY") ?? throw new InvalidOperationException("OPENAI_APIKEY is not set.");
|
||||
var model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini";
|
||||
|
||||
// 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);
|
||||
|
||||
// TODO: Remove this workaround when the agent framework supports environment variable substitution in YAML files.
|
||||
text = text.Replace("=Env.OPENAI_APIKEY", apiKey, StringComparison.OrdinalIgnoreCase);
|
||||
text = text.Replace("=Env.OPENAI_MODEL", model, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
// Create the agent from the YAML definition.
|
||||
var agentFactory = new AggregatorAgentFactory(
|
||||
[
|
||||
new OpenAIChatAgentFactory(),
|
||||
new OpenAIResponseAgentFactory(),
|
||||
new OpenAIAssistantAgentFactory()
|
||||
]);
|
||||
var agent = await agentFactory.CreateFromYamlAsync(text);
|
||||
|
||||
// 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 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));
|
||||
Reference in New Issue
Block a user