diff --git a/dotnet/Directory.Packages.props b/dotnet/Directory.Packages.props index 2e705607f7..5f1f297c92 100644 --- a/dotnet/Directory.Packages.props +++ b/dotnet/Directory.Packages.props @@ -72,6 +72,8 @@ + + diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx index 16abdc9479..1722d656c4 100644 --- a/dotnet/agent-framework-dotnet.slnx +++ b/dotnet/agent-framework-dotnet.slnx @@ -30,6 +30,15 @@ + + + + + + + + + diff --git a/dotnet/samples/GettingStarted/Providers/AIAgent_With_AzureAIAgentsPersistent.cs b/dotnet/samples/GettingStarted/Providers/AIAgent_With_AzureAIAgentsPersistent.cs deleted file mode 100644 index cf3d2bcae4..0000000000 --- a/dotnet/samples/GettingStarted/Providers/AIAgent_With_AzureAIAgentsPersistent.cs +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using Azure.AI.Agents.Persistent; -using Azure.Identity; -using Microsoft.Extensions.AI.Agents; -using Microsoft.Shared.Samples; - -namespace Providers; - -/// -/// Shows how to use with Azure AI Persistent Agents. -/// -/// -/// Running "az login" command in terminal is required for authentication with Azure AI service. -/// -public sealed class AIAgent_With_AzureAIAgentsPersistent(ITestOutputHelper output) : AgentSample(output) -{ - private const string JokerName = "Joker"; - private const string JokerInstructions = "You are good at telling jokes."; - - [Fact] - public async Task GetWithAzureAIAgentsPersistent() - { - // Get a client to create server side agents with. - var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential()); - - // Create a service side persistent agent. - var persistentAgent = await persistentAgentsClient.Administration.CreateAgentAsync( - model: TestConfiguration.AzureAI.DeploymentName!, - name: JokerName, - instructions: JokerInstructions); - - // Get a server side agent. - AIAgent agent = await persistentAgentsClient.GetAIAgentAsync(persistentAgent.Value.Id); - - // Start a new thread for the agent conversation. - AgentThread thread = agent.GetNewThread(); - - // Respond to user input - await RunAgentAsync("Tell me a joke about a pirate."); - await RunAgentAsync("Now add some emojis to the joke."); - - // Local function to run agent and display the conversation messages for the thread. - async Task RunAgentAsync(string input) - { - Console.WriteLine( - $""" - User: {input} - Assistant: - {await agent.RunAsync(input, thread)} - - """); - } - - // Cleanup - await persistentAgentsClient.Threads.DeleteThreadAsync(thread.ConversationId); - await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id); - } - - [Fact] - public async Task CreateWithAzureAIAgentsPersistent() - { - // Get a client to create server side agents with. - var persistentAgentsClient = new PersistentAgentsClient(TestConfiguration.AzureAI.Endpoint, new AzureCliCredential()); - - // Create a server side persistent agent. - AIAgent agent = await persistentAgentsClient.CreateAIAgentAsync( - model: TestConfiguration.AzureAI.DeploymentName!, - name: JokerName, - instructions: JokerInstructions); - - // Start a new thread for the agent conversation. - AgentThread thread = agent.GetNewThread(); - - // Respond to user input - await RunAgentAsync("Tell me a joke about a pirate."); - await RunAgentAsync("Now add some emojis to the joke."); - - // Local function to run agent and display the conversation messages for the thread. - async Task RunAgentAsync(string input) - { - Console.WriteLine( - $""" - User: {input} - Assistant: - {await agent.RunAsync(input, thread)} - - """); - } - - // Cleanup - await persistentAgentsClient.Threads.DeleteThreadAsync(thread.ConversationId); - await persistentAgentsClient.Administration.DeleteAgentAsync(agent.Id); - } -} diff --git a/dotnet/samples/GettingStarted/Providers/AIAgent_With_AzureOpenAIChatCompletion.cs b/dotnet/samples/GettingStarted/Providers/AIAgent_With_AzureOpenAIChatCompletion.cs deleted file mode 100644 index e2216359ad..0000000000 --- a/dotnet/samples/GettingStarted/Providers/AIAgent_With_AzureOpenAIChatCompletion.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using System.ClientModel; -using Azure.AI.OpenAI; -using Azure.Identity; -using Microsoft.Extensions.AI.Agents; -using Microsoft.Shared.Samples; -using OpenAI; - -namespace Providers; - -/// -/// End-to-end sample showing how to use with Azure OpenAI Chat Completion. -/// -public sealed class AIAgent_With_AzureOpenAIChatCompletion(ITestOutputHelper output) : AgentSample(output) -{ - private const string JokerName = "Joker"; - private const string JokerInstructions = "You are good at telling jokes."; - - [Fact] - public async Task RunWithChatCompletion() - { - // Get the OpenAI client to use for the agent. - var openAIClient = (TestConfiguration.AzureOpenAI.ApiKey is null) - // Use Azure CLI credentials if API key is not provided. - ? new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new AzureCliCredential()) - : new AzureOpenAIClient(TestConfiguration.AzureOpenAI.Endpoint, new ApiKeyCredential(TestConfiguration.AzureOpenAI.ApiKey)); - - // Create the agent - AIAgent agent = openAIClient.GetChatClient(TestConfiguration.AzureOpenAI.DeploymentName).CreateAIAgent(JokerInstructions, JokerName); - - // Start a new thread for the agent conversation. - AgentThread thread = agent.GetNewThread(); - - // Respond to user input - await RunAgentAsync("Tell me a joke about a pirate."); - await RunAgentAsync("Now add some emojis to the joke."); - - // Local function to invoke agent and display the conversation messages for the thread. - async Task RunAgentAsync(string input) - { - Console.WriteLine( - $""" - User: {input} - Assistant: - {await agent.RunAsync(input, thread)} - - """); - } - } -} diff --git a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs b/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs deleted file mode 100644 index dd01ed6382..0000000000 --- a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIClient.cs +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using Microsoft.Extensions.AI.Agents; -using Microsoft.Shared.Samples; -using OpenAI; -using OpenAI.Chat; - -namespace Providers; - -/// -/// End-to-end sample showing how to use with OpenAI Chat Completion and Responses. -/// -public sealed class AIAgent_With_OpenAIClient(ITestOutputHelper output) : AgentSample(output) -{ - private const string JokerName = "Joker"; - private const string JokerInstructions = "You are good at telling jokes."; - - [Fact] - public async Task RunWithChatCompletion() - { - // Create the agent using the OpenAI ChatClient. - AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey) - .GetChatClient(TestConfiguration.OpenAI.ChatModelId) - .CreateAIAgent(JokerInstructions, JokerName); - - // Start a new thread for the agent conversation. - AgentThread thread = agent.GetNewThread(); - - // Respond to user input. - await RunAgentAsync("Tell me a joke about a pirate."); - await RunAgentAsync("Now add some emojis to the joke."); - - // Local function to invoke agent and display the conversation messages for the thread. - async Task RunAgentAsync(string input) - { - Console.WriteLine( - $""" - User: {input} - Assistant: - {await agent.RunAsync(input, thread)} - - """); - } - } - - [Fact] - public async Task RunWithChatCompletionReturnChatCompletion() - { - // Get the agent directly from OpenAIClient. - var agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey) - .GetChatClient(TestConfiguration.OpenAI.ChatModelId) - .CreateAIAgent(JokerInstructions, JokerName); - - // Start a new thread for the agent conversation. - AgentThread thread = agent.GetNewThread(); - - // Respond to user input. - await RunAgentAsync("Tell me a joke about a pirate."); - await RunAgentAsync("Now add some emojis to the joke."); - - // Local function to invoke agent and display the conversation messages for the thread. - async Task RunAgentAsync(string input) - { - Console.WriteLine($"User: {input}"); - - var response = await agent.RunAsync(input, thread); - var chatCompletion = response.AsChatCompletion(); - - Console.WriteLine( - $""" - Assistant: - {chatCompletion.Content.Last().Text} - - """); - } - } - - [Fact] - public async Task RunWithChatCompletionWithOpenAIChatMessage() - { - // Get the agent directly from OpenAIClient. - var agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey) - .GetChatClient(TestConfiguration.OpenAI.ChatModelId) - .CreateAIAgent(JokerInstructions, JokerName); - - // Start a new thread for the agent conversation. - AgentThread thread = agent.GetNewThread(); - - // Respond to user input. - await RunAgentAsync("Tell me a joke about a pirate."); - await RunAgentAsync("Now add some emojis to the joke."); - - // Local function to invoke agent and display the conversation messages for the thread. - async Task RunAgentAsync(string input) - { - Console.WriteLine($"User: {input}"); - - // Use the OpenAI.Chat message types directly - var chatMessage = new UserChatMessage(input); - var chatCompletion = await agent.RunAsync(chatMessage, thread); - - Console.WriteLine( - $""" - Assistant: - {chatCompletion.Content.Last().Text} - - """); - } - } -} diff --git a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIResponseClient.cs b/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIResponseClient.cs deleted file mode 100644 index a8da098ef6..0000000000 --- a/dotnet/samples/GettingStarted/Providers/AIAgent_With_OpenAIResponseClient.cs +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using Microsoft.Extensions.AI; -using Microsoft.Extensions.AI.Agents; -using Microsoft.Shared.Samples; -using OpenAI; - -namespace Providers; - -/// -/// End-to-end sample showing how to use with OpenAI Chat Completion. -/// -public sealed class AIAgent_With_OpenAIResponseClient(ITestOutputHelper output) : AgentSample(output) -{ - private const string JokerName = "Joker"; - private const string JokerInstructions = "You are good at telling jokes."; - - /// - /// This will use the conversation id to reference the thread state on the server side. - /// - [Fact] - public async Task RunWithResponses() - { - // Get the agent directly from OpenAIClient. - AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey) - .GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId) - .CreateAIAgent(JokerInstructions, JokerName); - - // Start a new thread for the agent conversation based on the type. - AgentThread thread = agent.GetNewThread(); - - // Respond to user input. - await RunAgentAsync("Tell me a joke about a pirate."); - await RunAgentAsync("Now add some emojis to the joke."); - - // Local function to invoke agent and display the conversation messages for the thread. - async Task RunAgentAsync(string input) - { - Console.WriteLine( - $""" - User: {input} - Assistant: - {await agent.RunAsync(input, thread)} - - """); - } - } - - /// - /// This will use in-memory messages to store the thread state. - /// - [Fact] - public async Task RunWithResponsesAndStoreOutputDisabled() - { - // Get the agent directly from OpenAIClient. - AIAgent agent = new OpenAIClient(TestConfiguration.OpenAI.ApiKey) - .GetOpenAIResponseClient(TestConfiguration.OpenAI.ChatModelId) - .CreateAIAgent(options: new() - { - Name = JokerName, - Instructions = JokerInstructions, - ChatOptions = new ChatOptions().WithResponseStoredOutputDisabled() - }); - - // Start a new thread for the agent conversation based on the type. - AgentThread thread = agent.GetNewThread(); - - // Respond to user input. - await RunAgentAsync("Tell me a joke about a pirate."); - await RunAgentAsync("Now add some emojis to the joke."); - - // Local function to invoke agent and display the conversation messages for the thread. - async Task RunAgentAsync(string input) - { - Console.WriteLine( - $""" - User: {input} - Assistant: - {await agent.RunAsync(input, thread)} - - """); - } - } -} diff --git a/dotnet/samples/GettingStartedSteps/README.md b/dotnet/samples/GettingStartedSteps/README.md index cdce13ca5a..dec1438789 100644 --- a/dotnet/samples/GettingStartedSteps/README.md +++ b/dotnet/samples/GettingStartedSteps/README.md @@ -7,7 +7,7 @@ While the functionality can be used with any agent type, these samples use Azure and use ChatCompletion as the type of service. For other samples that demonstrate how to create and configure each type of agent that come with the agent framework, -see the [Agent setup](../AgentSetup/README.md) samples. +see the [How to create an AIAgent for each provider](../HowToCreateAnAIAgentByProvider/README.md) samples. ## Getting started steps prerequisites diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/AIAgent_With_AzureFoundry.csproj b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/AIAgent_With_AzureFoundry.csproj new file mode 100644 index 0000000000..294ec1455e --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/AIAgent_With_AzureFoundry.csproj @@ -0,0 +1,22 @@ + + + + Exe + net9.0 + 12 + + enable + disable + + + + + + + + + + + + + diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/Program.cs b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/Program.cs new file mode 100644 index 0000000000..774e5cd9a2 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/Program.cs @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to create and use a simple AI agent with Azure Foundry Agents as the backend. + +using System; +using Azure.AI.Agents.Persistent; +using Azure.Identity; +using Microsoft.Extensions.AI.Agents; + +var endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set."); +var deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; + +const string JokerName = "Joker"; +const string JokerInstructions = "You are good at telling jokes."; + +// Get a client to create/retrieve server side agents with. +var persistentAgentsClient = new PersistentAgentsClient(endpoint, new AzureCliCredential()); + +// You can create a server side persistent agent with the Azure.AI.Agents.Persistent SDK. +var agentMetadata = await persistentAgentsClient.Administration.CreateAgentAsync( + model: deploymentName, + name: JokerName, + instructions: JokerInstructions); + +// You can retrieve an already created server side persistent agent as an AIAgent. +AIAgent agent1 = await persistentAgentsClient.GetAIAgentAsync(agentMetadata.Value.Id); + +// You can also create a server side persistent agent and return it as an AIAgent directly. +AIAgent agent2 = await persistentAgentsClient.CreateAIAgentAsync( + model: deploymentName, + name: JokerName, + instructions: JokerInstructions); + +// You can then invoke the agent like any other AIAgent. +AgentThread thread = agent1.GetNewThread(); +Console.WriteLine(await agent1.RunAsync("Tell me a joke about a pirate.", thread)); + +// Cleanup for sample purposes. +await persistentAgentsClient.Threads.DeleteThreadAsync(thread.ConversationId); +await persistentAgentsClient.Administration.DeleteAgentAsync(agent1.Id); +await persistentAgentsClient.Administration.DeleteAgentAsync(agent2.Id); diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/README.md b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/README.md new file mode 100644 index 0000000000..df0854ba2f --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureFoundry/README.md @@ -0,0 +1,16 @@ +# Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 8.0 SDK or later +- Azure Foundry service endpoint and deployment configured +- Azure CLI installed and authenticated (for Azure credential authentication) + +**Note**: This demo uses Azure CLI credentials for authentication. Make sure you're logged in with `az login` and have access to the Azure Foundry resource. For more information, see the [Azure CLI documentation](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively). + +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 +``` diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/AIAgent_With_AzureOpenAIChatCompletion.csproj b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/AIAgent_With_AzureOpenAIChatCompletion.csproj new file mode 100644 index 0000000000..15185f869a --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/AIAgent_With_AzureOpenAIChatCompletion.csproj @@ -0,0 +1,23 @@ + + + + Exe + net9.0 + 12 + + enable + disable + + + + + + + + + + + + + + diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/Program.cs b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/Program.cs new file mode 100644 index 0000000000..27ab7dd865 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/Program.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to create and use a simple AI agent with Azure OpenAI Chat Completion as the backend. + +using System; +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Extensions.AI.Agents; +using OpenAI; + +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"; + +const string JokerName = "Joker"; +const string JokerInstructions = "You are good at telling jokes."; + +AIAgent agent = new AzureOpenAIClient( + new Uri(endpoint), + new AzureCliCredential()) + .GetChatClient(deploymentName) + .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/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/README.md b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/README.md new file mode 100644 index 0000000000..1278eb59e5 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIChatCompletion/README.md @@ -0,0 +1,16 @@ +# Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 8.0 SDK or later +- Azure OpenAI service endpoint and deployment configured +- Azure CLI installed and authenticated (for Azure credential authentication) + +**Note**: This demo uses Azure CLI credentials for authentication. Make sure you're logged in with `az login` and have access to the Azure OpenAI resource. For more information, see the [Azure CLI documentation](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively). + +Set the following environment variables: + +```powershell +$env:AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/" # Replace with your Azure OpenAI resource endpoint +$env:AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini +``` diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/AIAgent_With_AzureOpenAIResponses.csproj b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/AIAgent_With_AzureOpenAIResponses.csproj new file mode 100644 index 0000000000..15185f869a --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/AIAgent_With_AzureOpenAIResponses.csproj @@ -0,0 +1,23 @@ + + + + Exe + net9.0 + 12 + + enable + disable + + + + + + + + + + + + + + diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/Program.cs b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/Program.cs new file mode 100644 index 0000000000..311b421cd8 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/Program.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to create and use a simple AI agent with Azure OpenAI Responses as the backend. + +#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. + +using System; +using Azure.AI.OpenAI; +using Azure.Identity; +using Microsoft.Extensions.AI.Agents; +using OpenAI; + +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"; + +const string JokerName = "Joker"; +const string JokerInstructions = "You are good at telling jokes."; + +AIAgent agent = new AzureOpenAIClient( + new Uri(endpoint), + new AzureCliCredential()) + .GetOpenAIResponseClient(deploymentName) + .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/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/README.md b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/README.md new file mode 100644 index 0000000000..1278eb59e5 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_AzureOpenAIResponses/README.md @@ -0,0 +1,16 @@ +# Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 8.0 SDK or later +- Azure OpenAI service endpoint and deployment configured +- Azure CLI installed and authenticated (for Azure credential authentication) + +**Note**: This demo uses Azure CLI credentials for authentication. Make sure you're logged in with `az login` and have access to the Azure OpenAI resource. For more information, see the [Azure CLI documentation](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively). + +Set the following environment variables: + +```powershell +$env:AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/" # Replace with your Azure OpenAI resource endpoint +$env:AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini +``` diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/AIAgent_With_ONNX.csproj b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/AIAgent_With_ONNX.csproj new file mode 100644 index 0000000000..599d3f6698 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/AIAgent_With_ONNX.csproj @@ -0,0 +1,20 @@ + + + + Exe + net9.0 + 12 + + enable + disable + + + + + + + + + + + diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/Program.cs b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/Program.cs new file mode 100644 index 0000000000..94994dd92d --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/Program.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to create and use a simple AI agent with ONNX as the backend. + +using System; +using Microsoft.Extensions.AI.Agents; +using Microsoft.ML.OnnxRuntimeGenAI; + +// E.g. C:\repos\Phi-4-mini-instruct-onnx\cpu_and_mobile\cpu-int4-rtn-block-32-acc-level-4 +var modelPath = Environment.GetEnvironmentVariable("ONNX_MODEL_PATH") ?? throw new InvalidOperationException("ONNX_MODEL_PATH is not set."); + +const string JokerName = "Joker"; +const string JokerInstructions = "You are good at telling jokes."; + +// Get a chat client for ONNX and use it to construct an AIAgent. +using OnnxRuntimeGenAIChatClient chatClient = new(modelPath); +AIAgent agent = new ChatClientAgent(chatClient, 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/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/README.md b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/README.md new file mode 100644 index 0000000000..7bfacf30e9 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_ONNX/README.md @@ -0,0 +1,12 @@ +# Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 8.0 SDK or later +- An ONNX model downloaded to your machine + +Set the following environment variables: + +```powershell +$env:ONNX_MODEL_PATH="C:\repos\Phi-4-mini-instruct-onnx\cpu_and_mobile\cpu-int4-rtn-block-32-acc-level-4" # Replace with your model path +``` diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/AIAgent_With_OpenAIChatCompletion.csproj b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/AIAgent_With_OpenAIChatCompletion.csproj new file mode 100644 index 0000000000..6f38c09c64 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/AIAgent_With_OpenAIChatCompletion.csproj @@ -0,0 +1,17 @@ + + + + Exe + net9.0 + 12 + + enable + disable + + + + + + + + diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/Program.cs b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/Program.cs new file mode 100644 index 0000000000..1427433d5c --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/Program.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to create and use a simple AI agent with OpenAI Chat Completion as the backend. + +using System; +using Microsoft.Extensions.AI.Agents; +using OpenAI; + +var apiKey = Environment.GetEnvironmentVariable("OPENAI_APIKEY") ?? throw new InvalidOperationException("OPENAI_APIKEY is not set."); +var modelName = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; + +const string JokerName = "Joker"; +const string JokerInstructions = "You are good at telling jokes."; + +AIAgent agent = new OpenAIClient( + apiKey) + .GetChatClient(modelName) + .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/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/README.md b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/README.md new file mode 100644 index 0000000000..80b63e7cd0 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIChatCompletion/README.md @@ -0,0 +1,13 @@ +# Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 8.0 SDK or later +- OpenAI api key + +Set the following environment variables: + +```powershell +$env:OPENAI_APIKEY="*****" # Replace with your OpenAI api key +$env:OPENAI_MODEL="gpt-4o-mini" # Optional, defaults to gpt-4o-mini +``` diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/AIAgent_With_OpenAIResponses.csproj b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/AIAgent_With_OpenAIResponses.csproj new file mode 100644 index 0000000000..6f38c09c64 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/AIAgent_With_OpenAIResponses.csproj @@ -0,0 +1,17 @@ + + + + Exe + net9.0 + 12 + + enable + disable + + + + + + + + diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/Program.cs b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/Program.cs new file mode 100644 index 0000000000..bd919ade8f --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/Program.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to create and use a simple AI agent with OpenAI Responses as the backend. + +#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. + +using System; +using Microsoft.Extensions.AI.Agents; +using OpenAI; + +var apiKey = Environment.GetEnvironmentVariable("OPENAI_APIKEY") ?? throw new InvalidOperationException("OPENAI_APIKEY is not set."); +var modelName = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-4o-mini"; + +const string JokerName = "Joker"; +const string JokerInstructions = "You are good at telling jokes."; + +AIAgent agent = new OpenAIClient( + apiKey) + .GetOpenAIResponseClient(modelName) + .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/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/README.md b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/README.md new file mode 100644 index 0000000000..80b63e7cd0 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/AIAgent_With_OpenAIResponses/README.md @@ -0,0 +1,13 @@ +# Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 8.0 SDK or later +- OpenAI api key + +Set the following environment variables: + +```powershell +$env:OPENAI_APIKEY="*****" # Replace with your OpenAI api key +$env:OPENAI_MODEL="gpt-4o-mini" # Optional, defaults to gpt-4o-mini +``` diff --git a/dotnet/samples/HowToCreateAnAIAgentByProvider/README.md b/dotnet/samples/HowToCreateAnAIAgentByProvider/README.md new file mode 100644 index 0000000000..7c10bb1cd5 --- /dev/null +++ b/dotnet/samples/HowToCreateAnAIAgentByProvider/README.md @@ -0,0 +1,55 @@ +# Creating an AIAgent instance for various providers + +These samples show how to create an AIAgent instance using various providers. +This is not an exhaustive list, but shows a variety of the more popular options. + +For other samples that demonstrate how to use AIAgent instances, +see the [Getting Started Steps](../GettingStartedSteps/README.md) samples. + +## Prerequisites + +See the README.md for each sample for the prerequisites for that sample. +## Samples + +|Sample|Description| +|---|---| +|[Creating an AIAgent with AzureFoundry](./AIAgent_With_AzureFoundry/)|This sample demonstrates how to create an Azure Foundry agent and expose it as an AIAgent| +|[Creating an AIAgent with Azure OpenAI ChatCompletion](./AIAgent_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](./AIAgent_With_AzureOpenAIResponses/)|This sample demonstrates how to create an AIAgent using Azure OpenAI Responses as the underlying inference service| +|[Creating an AIAgent with ONNX](./AIAgent_With_ONNX/)|This sample demonstrates how to create an AIAgent using ONNX as the underlying inference service| +|[Creating an AIAgent with OpenAI ChatCompletion](./AIAgent_With_OpenAIChatCompletion/)|This sample demonstrates how to create an AIAgent using OpenAI ChatCompletion as the underlying inference service| +|[Creating an AIAgent with OpenAI Responses](./AIAgent_With_OpenAIResponses/)|This sample demonstrates how to create an AIAgent using OpenAI Responses as the underlying inference service| + +## Running the samples from the console + +To run the samples, navigate to the desired sample directory, e.g. + +```powershell +cd AIAgent_With_AzureOpenAIChatCompletion +``` + +Set the required environment variables as documented in the sample readme. +If the variables are not set, you will be prompted for the values when running the samples. +Execute the following command to build the sample: + +```powershell +dotnet build +``` + +Execute the following command to run the sample: + +```powershell +dotnet run --no-build +``` + +Or just build and run in one step: + +```powershell +dotnet run +``` + +## Running the samples from Visual Studio + +Open the solution in Visual Studio and set the desired sample project as the startup project. Then, run the project using the built-in debugger or by pressing `F5`. + +You will be prompted for any required environment variables if they are not already set. diff --git a/dotnet/samples/README.md b/dotnet/samples/README.md index 0d9d0340b5..abe2298128 100644 --- a/dotnet/samples/README.md +++ b/dotnet/samples/README.md @@ -16,7 +16,7 @@ The samples are subdivided into the following categories: - [Getting Started Steps](./GettingStartedSteps/README.md): Basic steps to get started with the agent framework. These samples demonstrate the fundamental concepts and functionalities of the agent framework when using the `ChatClientAgent` and can be used with any underlying service that the `ChatClientAgent` supports. -- [Agent setup](./AgentSetup/README.md): Samples that demonstrate how to create and configure each type of agent that come with the agent framework. +- [How to create an AIAgent for each provider](./HowToCreateAnAIAgentByProvider/README.md): Shows how to create an AIAgent instance for a selection of providers. - [Agent specific features](./AgentSpecificFeatures/README.md): Samples that showcase features specific to each type of agent. ## Prerequisites