diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx index 320b2e27a9..b3a1fd81f3 100644 --- a/dotnet/agent-framework-dotnet.slnx +++ b/dotnet/agent-framework-dotnet.slnx @@ -176,6 +176,7 @@ + @@ -409,7 +410,6 @@ - @@ -417,6 +417,7 @@ + @@ -428,8 +429,8 @@ - + @@ -439,8 +440,8 @@ - + @@ -454,13 +455,13 @@ - + diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/FoundryAgents_Step19_OpenAPITools.csproj b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/FoundryAgents_Step19_OpenAPITools.csproj new file mode 100644 index 0000000000..77b76acfa0 --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/FoundryAgents_Step19_OpenAPITools.csproj @@ -0,0 +1,22 @@ + + + + Exe + net10.0 + + enable + enable + $(NoWarn);CA1812;CS8321 + + + + + + + + + + + + + \ No newline at end of file diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/Program.cs b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/Program.cs new file mode 100644 index 0000000000..8d17bb0f91 --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/Program.cs @@ -0,0 +1,116 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to use OpenAPI Tools with AI Agents. + +using Azure.AI.Projects; +using Azure.AI.Projects.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI; +using OpenAI.Responses; + +// Warning: DefaultAzureCredential is intended for simplicity in development. For production scenarios, consider using a more specific credential. +string endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set."); +string deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; + +const string AgentInstructions = "You are a helpful assistant that can use the countries API to retrieve information about countries by their currency code."; + +// A simple OpenAPI specification for the REST Countries API +const string CountriesOpenApiSpec = """ +{ + "openapi": "3.1.0", + "info": { + "title": "REST Countries API", + "description": "Retrieve information about countries by currency code", + "version": "v3.1" + }, + "servers": [ + { + "url": "https://restcountries.com/v3.1" + } + ], + "paths": { + "/currency/{currency}": { + "get": { + "description": "Get countries that use a specific currency code (e.g., USD, EUR, GBP)", + "operationId": "GetCountriesByCurrency", + "parameters": [ + { + "name": "currency", + "in": "path", + "description": "Currency code (e.g., USD, EUR, GBP)", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Successful response with list of countries", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "type": "object" + } + } + } + } + }, + "404": { + "description": "No countries found for the currency" + } + } + } + } + } +} +"""; + +// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents. +AIProjectClient aiProjectClient = new(new Uri(endpoint), new DefaultAzureCredential()); + +// Create the OpenAPI function definition +var openApiFunction = new OpenAPIFunctionDefinition( + "get_countries", + BinaryData.FromString(CountriesOpenApiSpec), + new OpenAPIAnonymousAuthenticationDetails()) +{ + Description = "Retrieve information about countries by currency code" +}; + +AIAgent agent = await CreateAgentWithMEAI(); +// AIAgent agent = await CreateAgentWithNativeSDK(); + +// Run the agent with a question about countries +Console.WriteLine(await agent.RunAsync("What countries use the Euro (EUR) as their currency? Please list them.")); + +// Cleanup by deleting the agent +await aiProjectClient.Agents.DeleteAgentAsync(agent.Name); + +// --- Agent Creation Options --- + +// Option 1 - Using AsAITool wrapping for OpenApiTool (MEAI + AgentFramework) +async Task CreateAgentWithMEAI() +{ + return await aiProjectClient.CreateAIAgentAsync( + model: deploymentName, + name: "OpenAPIToolsAgent-MEAI", + instructions: AgentInstructions, + tools: [((ResponseTool)AgentTool.CreateOpenApiTool(openApiFunction)).AsAITool()]); +} + +// Option 2 - Using PromptAgentDefinition with AgentTool.CreateOpenApiTool (Native SDK) +async Task CreateAgentWithNativeSDK() +{ + return await aiProjectClient.CreateAIAgentAsync( + name: "OpenAPIToolsAgent-NATIVE", + creationOptions: new AgentVersionCreationOptions( + new PromptAgentDefinition(model: deploymentName) + { + Instructions = AgentInstructions, + Tools = { (ResponseTool)AgentTool.CreateOpenApiTool(openApiFunction) } + }) + ); +} diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/README.md b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/README.md new file mode 100644 index 0000000000..e7a2f2cb52 --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/README.md @@ -0,0 +1,47 @@ +# Using OpenAPI Tools with AI Agents + +This sample demonstrates how to use OpenAPI tools with AI agents. OpenAPI tools allow agents to call external REST APIs defined by OpenAPI specifications. + +## What this sample demonstrates + +- Creating agents with OpenAPI tool capabilities +- Using AgentTool.CreateOpenApiTool with an embedded OpenAPI specification +- Anonymous authentication for public APIs +- Running an agent that can call external REST APIs +- Managing agent lifecycle (creation and deletion) + +## Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 10 SDK or later +- Azure Foundry service endpoint and deployment configured +- Azure CLI installed and authenticated (for Azure credential authentication) + +**Note**: This demo uses `DefaultAzureCredential` for authentication, which supports multiple authentication methods including Azure CLI, managed identity, and more. Make sure you're logged in with `az login` and have access to the Azure Foundry resource. For more information, see the [Azure Identity documentation](https://learn.microsoft.com/dotnet/api/azure.identity.defaultazurecredential). + +Set the following environment variables: + +```powershell +$env:AZURE_FOUNDRY_PROJECT_ENDPOINT="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project" # Replace with your Azure Foundry resource endpoint +$env:AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini +``` + +## Run the sample + +Navigate to the FoundryAgents sample directory and run: + +```powershell +cd dotnet/samples/GettingStarted/FoundryAgents +dotnet run --project .\FoundryAgents_Step19_OpenAPITools +``` + +## Expected behavior + +The sample will: + +1. Create an agent with an OpenAPI tool configured to call the REST Countries API +2. Ask the agent: "What countries use the Euro (EUR) as their currency?" +3. The agent will use the OpenAPI tool to call the REST Countries API +4. Display the response containing the list of countries that use EUR +5. Clean up resources by deleting the agent \ No newline at end of file