diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx index 5449d630c3..920da0589e 100644 --- a/dotnet/agent-framework-dotnet.slnx +++ b/dotnet/agent-framework-dotnet.slnx @@ -24,7 +24,8 @@ - + + diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundry/Agent_With_AzureFoundry.csproj b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryAgent/Agent_With_AzureFoundryAgent.csproj similarity index 100% rename from dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundry/Agent_With_AzureFoundry.csproj rename to dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryAgent/Agent_With_AzureFoundryAgent.csproj diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundry/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryAgent/Program.cs similarity index 100% rename from dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundry/Program.cs rename to dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryAgent/Program.cs diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundry/README.md b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryAgent/README.md similarity index 100% rename from dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundry/README.md rename to dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryAgent/README.md diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Agent_With_AzureFoundryModel.csproj b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Agent_With_AzureFoundryModel.csproj new file mode 100644 index 0000000000..cd545ddb48 --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Agent_With_AzureFoundryModel.csproj @@ -0,0 +1,19 @@ + + + + Exe + net9.0 + + enable + enable + + + + + + + + + + + diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Program.cs b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Program.cs new file mode 100644 index 0000000000..a79b8058b2 --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/Program.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to use the OpenAI SDK to create and use a simple AI agent with any model hosted in Azure AI Foundry. +// You could use models from Microsoft, OpenAI, DeepSeek, Hugging Face, Meta, xAI or any other model you have deployed in your Azure AI Foundry resource. +// Note: Ensure that you pick a model that suits your needs. For example, if you want to use function calling, ensure that the model you pick supports function calling. + +using System.ClientModel; +using System.ClientModel.Primitives; +using Azure.Identity; +using Microsoft.Agents.AI; +using OpenAI; + +var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_OPENAI_ENDPOINT is not set."); +var apiKey = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_OPENAI_APIKEY"); +var model = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_MODEL_DEPLOYMENT") ?? "Phi-4-mini-instruct"; + +const string JokerName = "Joker"; +const string JokerInstructions = "You are good at telling jokes."; + +// Since we are using the OpenAI Client SDK, we need to override the default endpoint to point to Azure Foundry. +var clientOptions = new OpenAIClientOptions() { Endpoint = new Uri(endpoint) }; + +// Create the OpenAI client with either an API key or Azure CLI credential. +OpenAIClient client = string.IsNullOrWhiteSpace(apiKey) + ? new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), clientOptions) + : new OpenAIClient(new ApiKeyCredential(apiKey), clientOptions); + +AIAgent agent = client + .GetChatClient(model) + .CreateAIAgent(JokerInstructions, JokerName); + +// Invoke the agent and output the text result. +Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.")); diff --git a/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/README.md b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/README.md new file mode 100644 index 0000000000..9147bda1da --- /dev/null +++ b/dotnet/samples/GettingStarted/AgentProviders/Agent_With_AzureFoundryModel/README.md @@ -0,0 +1,34 @@ +## Overview + +This sample shows how to use the OpenAI SDK to create and use a simple AI agent with any model hosted in Azure AI Foundry. + +You could use models from Microsoft, OpenAI, DeepSeek, Hugging Face, Meta, xAI or any other model you have deployed in Azure AI Foundry. + +**Note**: Ensure that you pick a model that suits your needs. For example, if you want to use function calling, ensure that the model you pick supports function calling. + +## Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 8.0 SDK or later +- Azure AI Foundry resource +- A model deployment in your Azure AI Foundry resource. This example defaults to using the `Phi-4-mini-instruct` model, +so if you want to use a different model, ensure that you set your `AZURE_FOUNDRY_MODEL_DEPLOYMENT` environment +variable to the name of your deployed model. +- An API key or role based authentication to access the Azure AI Foundry resource + +See [here](https://learn.microsoft.com/en-us/azure/ai-foundry/quickstarts/get-started-code?tabs=csharp) for more info on setting up these prerequisites + +Set the following environment variables: + +```powershell +# Replace with your Azure AI Foundry resource endpoint +# Ensure that you have the "/openai/v1/" path in the URL, since this is required when using the OpenAI SDK to access Azure Foundry models. +$env:AZURE_FOUNDRY_OPENAI_ENDPOINT="https://ai-foundry-.services.ai.azure.com/openai/v1/" + +# Optional, defaults to using Azure CLI for authentication if not provided +$env:AZURE_FOUNDRY_OPENAI_APIKEY="************" + +# Optional, defaults to Phi-4-mini-instruct +$env:AZURE_FOUNDRY_MODEL_DEPLOYMENT="Phi-4-mini-instruct" +``` diff --git a/dotnet/samples/GettingStarted/AgentProviders/README.md b/dotnet/samples/GettingStarted/AgentProviders/README.md index 515c7a8f81..4e84cd4f08 100644 --- a/dotnet/samples/GettingStarted/AgentProviders/README.md +++ b/dotnet/samples/GettingStarted/AgentProviders/README.md @@ -15,7 +15,8 @@ See the README.md for each sample for the prerequisites for that sample. |Sample|Description| |---|---| |[Creating an AIAgent with A2A](./Agent_With_A2A/)|This sample demonstrates how to create AIAgent for an existing A2A agent.| -|[Creating an AIAgent with AzureFoundry](./Agent_With_AzureFoundry/)|This sample demonstrates how to create an Azure Foundry agent and expose it as an AIAgent| +|[Creating an AIAgent with AzureFoundry Agent](./Agent_With_AzureFoundryAgent/)|This sample demonstrates how to create an Azure Foundry agent and expose it as an AIAgent| +|[Creating an AIAgent with AzureFoundry Model](./Agent_With_AzureFoundryModel/)|This sample demonstrates how to use any model deployed to Azure Foundry to create an AIAgent| |[Creating an AIAgent with Azure OpenAI ChatCompletion](./Agent_With_AzureOpenAIChatCompletion/)|This sample demonstrates how to create an AIAgent using Azure OpenAI ChatCompletion as the underlying inference service| |[Creating an AIAgent with Azure OpenAI Responses](./Agent_With_AzureOpenAIResponses/)|This sample demonstrates how to create an AIAgent using Azure OpenAI Responses as the underlying inference service| |[Creating an AIAgent with a custom implementation](./Agent_With_CustomImplementation/)|This sample demonstrates how to create an AIAgent with a custom implementation|