Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@

```c#
using System;
using Azure.AI.OpenAI;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;

var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")!;
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME")!;

var agent = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
var agent = new OpenAIClient(
new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"),
new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
.GetOpenAIResponseClient(deploymentName)
.CreateAIAgent(name: "HaikuBot", instructions: "You are an upbeat assistant that writes beautifully.");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ClientModel.Primitives;
using System.ComponentModel;
using System.Text.Json;
using AGUIDojoServer.AgenticUI;
using AGUIDojoServer.BackendToolRendering;
using AGUIDojoServer.PredictiveStateUpdates;
using AGUIDojoServer.SharedState;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
using ChatClient = OpenAI.Chat.ChatClient;

namespace AGUIDojoServer;

internal static class ChatClientAgentFactory
{
private static AzureOpenAIClient? s_azureOpenAIClient;
private static OpenAIClient? s_openAIClient;
private static string? s_deploymentName;

public static void Initialize(IConfiguration configuration)
{
string endpoint = configuration["AZURE_OPENAI_ENDPOINT"] ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
s_deploymentName = configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME is not set.");

s_azureOpenAIClient = new AzureOpenAIClient(
new Uri(endpoint),
new DefaultAzureCredential());
s_openAIClient = new OpenAIClient(
new BearerTokenPolicy(new DefaultAzureCredential(), "https://ai.azure.com/.default"),
new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });
}

public static ChatClientAgent CreateAgenticChat()
{
ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!);
ChatClient chatClient = s_openAIClient!.GetChatClient(s_deploymentName!);

return chatClient.AsIChatClient().CreateAIAgent(
name: "AgenticChat",
Expand All @@ -40,7 +41,7 @@ public static ChatClientAgent CreateAgenticChat()

public static ChatClientAgent CreateBackendToolRendering()
{
ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!);
ChatClient chatClient = s_openAIClient!.GetChatClient(s_deploymentName!);

return chatClient.AsIChatClient().CreateAIAgent(
name: "BackendToolRenderer",
Expand All @@ -54,7 +55,7 @@ public static ChatClientAgent CreateBackendToolRendering()

public static ChatClientAgent CreateHumanInTheLoop()
{
ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!);
ChatClient chatClient = s_openAIClient!.GetChatClient(s_deploymentName!);

return chatClient.AsIChatClient().CreateAIAgent(
name: "HumanInTheLoopAgent",
Expand All @@ -63,7 +64,7 @@ public static ChatClientAgent CreateHumanInTheLoop()

public static ChatClientAgent CreateToolBasedGenerativeUI()
{
ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!);
ChatClient chatClient = s_openAIClient!.GetChatClient(s_deploymentName!);

return chatClient.AsIChatClient().CreateAIAgent(
name: "ToolBasedGenerativeUIAgent",
Expand All @@ -72,7 +73,7 @@ public static ChatClientAgent CreateToolBasedGenerativeUI()

public static AIAgent CreateAgenticUI(JsonSerializerOptions options)
{
ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!);
ChatClient chatClient = s_openAIClient!.GetChatClient(s_deploymentName!);
var baseAgent = chatClient.AsIChatClient().CreateAIAgent(new ChatClientAgentOptions
{
Name = "AgenticUIAgent",
Expand Down Expand Up @@ -114,7 +115,7 @@ again until all the steps in current plan are completed.

public static AIAgent CreateSharedState(JsonSerializerOptions options)
{
ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!);
ChatClient chatClient = s_openAIClient!.GetChatClient(s_deploymentName!);

var baseAgent = chatClient.AsIChatClient().CreateAIAgent(
name: "SharedStateAgent",
Expand All @@ -125,7 +126,7 @@ public static AIAgent CreateSharedState(JsonSerializerOptions options)

public static AIAgent CreatePredictiveStateUpdates(JsonSerializerOptions options)
{
ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!);
ChatClient chatClient = s_openAIClient!.GetChatClient(s_deploymentName!);

var baseAgent = chatClient.AsIChatClient().CreateAIAgent(new ChatClientAgentOptions
{
Expand Down
9 changes: 5 additions & 4 deletions dotnet/samples/AGUIClientServer/AGUIServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ClientModel.Primitives;
using System.ComponentModel;
using AGUIServer;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
using Microsoft.Extensions.AI;
using OpenAI;
using OpenAI.Chat;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
Expand All @@ -19,9 +20,9 @@
string deploymentName = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME is not set.");

// Create the AI agent with tools
var agent = new AzureOpenAIClient(
new Uri(endpoint),
new DefaultAzureCredential())
var agent = new OpenAIClient(
new BearerTokenPolicy(new DefaultAzureCredential(), "https://ai.azure.com/.default"),
new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
.GetChatClient(deploymentName)
.CreateAIAgent(
name: "AGUIAssistant",
Expand Down
8 changes: 4 additions & 4 deletions dotnet/samples/AGUIWebChat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ The server (`Server/Program.cs`) creates a simple chat agent:

```csharp
// Create Azure OpenAI client
AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(
new Uri(endpoint),
new DefaultAzureCredential());
OpenAIClient openAIClient = new OpenAIClient(
new BearerTokenPolicy(new DefaultAzureCredential(), "https://ai.azure.com/.default"),
new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

ChatClient chatClient = azureOpenAIClient.GetChatClient(deploymentName);
ChatClient chatClient = openAIClient.GetChatClient(deploymentName);

// Create AI agent
ChatClientAgent agent = chatClient.AsIChatClient().CreateAIAgent(
Expand Down
11 changes: 6 additions & 5 deletions dotnet/samples/AGUIWebChat/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

// This sample demonstrates a basic AG-UI server hosting a chat agent for the Blazor web client.

using Azure.AI.OpenAI;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
using Microsoft.Extensions.AI;
using OpenAI;
using OpenAI.Chat;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
Expand All @@ -19,11 +20,11 @@
string deploymentName = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME is not set.");

// Create the AI agent
AzureOpenAIClient azureOpenAIClient = new(
new Uri(endpoint),
new DefaultAzureCredential());
OpenAIClient openAIClient = new(
new BearerTokenPolicy(new DefaultAzureCredential(), "https://ai.azure.com/.default"),
new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

ChatClient chatClient = azureOpenAIClient.GetChatClient(deploymentName);
ChatClient chatClient = openAIClient.GetChatClient(deploymentName);

ChatClientAgent agent = chatClient.AsIChatClient().CreateAIAgent(
name: "ChatAssistant",
Expand Down
11 changes: 6 additions & 5 deletions dotnet/samples/AzureFunctions/01_SingleAgent/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure;
using Azure.AI.OpenAI;
using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting.AzureFunctions;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using OpenAI;
using OpenAI.Chat;

// Get the Azure OpenAI endpoint and deployment name from environment variables.
Expand All @@ -17,9 +18,9 @@

// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
OpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new OpenAIClient(new ApiKeyCredential(azureOpenAiKey), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
: new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

// Set up an AI agent following the standard Microsoft Agent Framework pattern.
const string JokerName = "Joker";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure;
using Azure.AI.OpenAI;
using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting.AzureFunctions;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using OpenAI;
using OpenAI.Chat;

// Get the Azure OpenAI endpoint and deployment name from environment variables.
Expand All @@ -17,9 +18,9 @@

// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
OpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new OpenAIClient(new ApiKeyCredential(azureOpenAiKey), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
: new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

// Single agent used by the orchestration to demonstrate sequential calls on the same thread.
const string WriterName = "WriterAgent";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure;
using Azure.AI.OpenAI;
using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting.AzureFunctions;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using OpenAI;
using OpenAI.Chat;

// Get the Azure OpenAI endpoint and deployment name from environment variables.
Expand All @@ -17,9 +18,9 @@

// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
OpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new OpenAIClient(new ApiKeyCredential(azureOpenAiKey), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
: new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

// Two agents used by the orchestration to demonstrate concurrent execution.
const string PhysicistName = "PhysicistAgent";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure;
using Azure.AI.OpenAI;
using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting.AzureFunctions;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using OpenAI;
using OpenAI.Chat;

// Get the Azure OpenAI endpoint and deployment name from environment variables.
Expand All @@ -17,9 +18,9 @@

// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
OpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new OpenAIClient(new ApiKeyCredential(azureOpenAiKey), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
: new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

// Two agents used by the orchestration to demonstrate conditional logic.
const string SpamDetectionName = "SpamDetectionAgent";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure;
using Azure.AI.OpenAI;
using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting.AzureFunctions;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using OpenAI;
using OpenAI.Chat;

// Get the Azure OpenAI endpoint and deployment name from environment variables.
Expand All @@ -17,9 +18,9 @@

// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
OpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new OpenAIClient(new ApiKeyCredential(azureOpenAiKey), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
: new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

// Single agent used by the orchestration to demonstrate human-in-the-loop workflow.
const string WriterName = "WriterAgent";
Expand Down
11 changes: 6 additions & 5 deletions dotnet/samples/AzureFunctions/06_LongRunningTools/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.

using Azure;
using Azure.AI.OpenAI;
using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.Identity;
using LongRunningTools;
using Microsoft.Agents.AI;
Expand All @@ -11,6 +11,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenAI;
using OpenAI.Chat;

// Get the Azure OpenAI endpoint and deployment name from environment variables.
Expand All @@ -21,9 +22,9 @@

// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
OpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new OpenAIClient(new ApiKeyCredential(azureOpenAiKey), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
: new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

// Agent used by the orchestration to write content.
const string WriterAgentName = "Writer";
Expand Down
11 changes: 6 additions & 5 deletions dotnet/samples/AzureFunctions/07_AgentAsMcpTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
// generate a remote MCP endpoint for the app at /runtime/webhooks/mcp with a agent-specific
// query tool name.

using Azure;
using Azure.AI.OpenAI;
using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.DurableTask;
using Microsoft.Agents.AI.Hosting.AzureFunctions;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
using OpenAI;
using OpenAI.Chat;

// Get the Azure OpenAI endpoint and deployment name from environment variables.
Expand All @@ -23,9 +24,9 @@

// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
OpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
? new OpenAIClient(new ApiKeyCredential(azureOpenAiKey), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
: new OpenAIClient(new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"), new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") });

// Define three AI agents we are going to use in this application.
AIAgent agent1 = client.GetChatClient(deploymentName).CreateAIAgent("You are good at telling jokes.", "Joker");
Expand Down
Loading
Loading