Skip to content

Commit dee0b1a

Browse files
authored
Merge pull request #951 from MicrosoftDocs/fix-middleware-docs-002
Add C# runtime-context samples and migrate middleware docs to AIProjectClient
2 parents 377eb7d + 22fe11e commit dee0b1a

9 files changed

Lines changed: 352 additions & 65 deletions

File tree

agent-framework/agents/middleware/agent-vs-run-scope.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ using System.Linq;
3333
using System.Runtime.CompilerServices;
3434
using System.Threading;
3535
using System.Threading.Tasks;
36-
using Azure.AI.OpenAI;
36+
using Azure.AI.Projects;
3737
using Azure.Identity;
3838
using Microsoft.Agents.AI;
3939
using Microsoft.Extensions.AI;
@@ -65,11 +65,12 @@ async IAsyncEnumerable<AgentResponseUpdate> SecurityStreamingMiddleware(
6565
}
6666
}
6767

68-
AIAgent baseAgent = new AzureOpenAIClient(
69-
new Uri("https://<myresource>.openai.azure.com"),
70-
new AzureCliCredential())
71-
.GetChatClient("gpt-4o-mini")
72-
.AsAIAgent(instructions: "You are a helpful assistant.");
68+
AIAgent baseAgent = new AIProjectClient(
69+
new Uri("<your-foundry-project-endpoint>"),
70+
new DefaultAzureCredential())
71+
.AsAIAgent(
72+
model: "gpt-4o-mini",
73+
instructions: "You are a helpful assistant.");
7374

7475
// Register middleware at the agent level
7576
var agentWithMiddleware = baseAgent
@@ -80,6 +81,9 @@ var agentWithMiddleware = baseAgent
8081
Console.WriteLine(await agentWithMiddleware.RunAsync("What's the weather in Paris?"));
8182
```
8283

84+
> [!WARNING]
85+
> `DefaultAzureCredential` is convenient for development but requires careful consideration in production. In production, consider using a specific credential (e.g., `ManagedIdentityCredential`) to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
86+
8387
### Run-level middleware
8488

8589
Run-level middleware uses the same builder pattern, applied inline for a specific invocation:

agent-framework/agents/middleware/chat-middleware.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using System.Collections.Generic;
2323
using System.Linq;
2424
using System.Threading;
2525
using System.Threading.Tasks;
26-
using Azure.AI.OpenAI;
26+
using Azure.AI.Projects;
2727
using Azure.Identity;
2828
using Microsoft.Agents.AI;
2929
using Microsoft.Extensions.AI;
@@ -48,18 +48,23 @@ async Task<ChatResponse> LoggingChatMiddleware(
4848
}
4949

5050
// Register IChatClient middleware using the client factory
51-
var agent = new AzureOpenAIClient(
52-
new Uri("https://<myresource>.openai.azure.com"),
53-
new AzureCliCredential())
54-
.GetChatClient("gpt-4o-mini")
55-
.AsAIAgent("You are a helpful assistant.", clientFactory: (chatClient) => chatClient
56-
.AsBuilder()
57-
.Use(getResponseFunc: LoggingChatMiddleware, getStreamingResponseFunc: null)
58-
.Build());
51+
var agent = new AIProjectClient(
52+
new Uri("<your-foundry-project-endpoint>"),
53+
new DefaultAzureCredential())
54+
.AsAIAgent(
55+
model: "gpt-4o-mini",
56+
instructions: "You are a helpful assistant.",
57+
clientFactory: (chatClient) => chatClient
58+
.AsBuilder()
59+
.Use(getResponseFunc: LoggingChatMiddleware, getStreamingResponseFunc: null)
60+
.Build());
5961

6062
Console.WriteLine(await agent.RunAsync("Hello, how are you?"));
6163
```
6264

65+
> [!WARNING]
66+
> `DefaultAzureCredential` is convenient for development but requires careful consideration in production. In production, consider using a specific credential (e.g., `ManagedIdentityCredential`) to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
67+
6368
> [!NOTE]
6469
> For more information about `IChatClient` middleware, see [Custom IChatClient middleware](/dotnet/ai/microsoft-extensions-ai#custom-ichatclient-middleware).
6570

agent-framework/agents/middleware/defining-middleware.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,27 @@ First, create a basic agent with a function tool.
2626
```csharp
2727
using System;
2828
using System.ComponentModel;
29-
using Azure.AI.OpenAI;
29+
using Azure.AI.Projects;
3030
using Azure.Identity;
3131
using Microsoft.Agents.AI;
3232
using Microsoft.Extensions.AI;
33-
using OpenAI;
3433

3534
[Description("The current datetime offset.")]
3635
static string GetDateTime()
3736
=> DateTimeOffset.Now.ToString();
3837

39-
AIAgent baseAgent = new AzureOpenAIClient(
40-
new Uri("https://<myresource>.openai.azure.com"),
41-
new AzureCliCredential())
42-
.GetChatClient("gpt-4o-mini")
38+
AIAgent baseAgent = new AIProjectClient(
39+
new Uri("<your-foundry-project-endpoint>"),
40+
new DefaultAzureCredential())
4341
.AsAIAgent(
42+
model: "gpt-4o-mini",
4443
instructions: "You are an AI assistant that helps people find information.",
4544
tools: [AIFunctionFactory.Create(GetDateTime, name: nameof(GetDateTime))]);
4645
```
4746

47+
> [!WARNING]
48+
> `DefaultAzureCredential` is convenient for development but requires careful consideration in production. In production, consider using a specific credential (e.g., `ManagedIdentityCredential`) to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
49+
4850
## Step 2: Create Your Agent Run Middleware
4951

5052
Next, create a function that will get invoked for each agent run.
@@ -178,9 +180,12 @@ To add middleware to your <xref:Microsoft.Extensions.AI.IChatClient>, you can us
178180
After adding the middleware, you can use the `IChatClient` with your agent as usual.
179181

180182
```csharp
181-
var chatClient = new AzureOpenAIClient(new Uri("https://<myresource>.openai.azure.com"), new AzureCliCredential())
182-
.GetChatClient("gpt-4o-mini")
183-
.AsIChatClient();
183+
var chatClient = new AIProjectClient(
184+
new Uri("<your-foundry-project-endpoint>"),
185+
new DefaultAzureCredential())
186+
.GetProjectOpenAIClient()
187+
.GetResponsesClient()
188+
.AsIChatClient("gpt-4o-mini");
184189

185190
var middlewareEnabledChatClient = chatClient
186191
.AsBuilder()
@@ -194,12 +199,16 @@ var agent = new ChatClientAgent(middlewareEnabledChatClient, instructions: "You
194199
an agent via one of the helper methods on SDK clients.
195200

196201
```csharp
197-
var agent = new AzureOpenAIClient(new Uri("https://<myresource>.openai.azure.com"), new AzureCliCredential())
198-
.GetChatClient("gpt-4o-mini")
199-
.AsAIAgent("You are a helpful assistant.", clientFactory: (chatClient) => chatClient
200-
.AsBuilder()
201-
.Use(getResponseFunc: CustomChatClientMiddleware, getStreamingResponseFunc: null)
202-
.Build());
202+
var agent = new AIProjectClient(
203+
new Uri("<your-foundry-project-endpoint>"),
204+
new DefaultAzureCredential())
205+
.AsAIAgent(
206+
model: "gpt-4o-mini",
207+
instructions: "You are a helpful assistant.",
208+
clientFactory: (chatClient) => chatClient
209+
.AsBuilder()
210+
.Use(getResponseFunc: CustomChatClientMiddleware, getStreamingResponseFunc: null)
211+
.Build());
203212
```
204213

205214
::: zone-end

agent-framework/agents/middleware/exception-handling.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using System;
2222
using System.Collections.Generic;
2323
using System.Threading;
2424
using System.Threading.Tasks;
25-
using Azure.AI.OpenAI;
25+
using Azure.AI.Projects;
2626
using Azure.Identity;
2727
using Microsoft.Agents.AI;
2828
using Microsoft.Extensions.AI;
@@ -54,11 +54,12 @@ async Task<AgentResponse> ExceptionHandlingMiddleware(
5454
}
5555
}
5656

57-
AIAgent agent = new AzureOpenAIClient(
58-
new Uri("https://<myresource>.openai.azure.com"),
59-
new AzureCliCredential())
60-
.GetChatClient("gpt-4o-mini")
61-
.AsAIAgent(instructions: "You are a helpful assistant.");
57+
AIAgent agent = new AIProjectClient(
58+
new Uri("<your-foundry-project-endpoint>"),
59+
new DefaultAzureCredential())
60+
.AsAIAgent(
61+
model: "gpt-4o-mini",
62+
instructions: "You are a helpful assistant.");
6263

6364
var safeAgent = agent
6465
.AsBuilder()
@@ -68,6 +69,9 @@ var safeAgent = agent
6869
Console.WriteLine(await safeAgent.RunAsync("Get user statistics"));
6970
```
7071

72+
> [!WARNING]
73+
> `DefaultAzureCredential` is convenient for development but requires careful consideration in production. In production, consider using a specific credential (e.g., `ManagedIdentityCredential`) to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
74+
7175
:::zone-end
7276

7377
:::zone pivot="programming-language-python"

agent-framework/agents/middleware/index.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ var middlewareEnabledAgent = originalAgent
4343
`IChatClient` middleware can be registered on an `IChatClient` before it is used with a `ChatClientAgent`, by using the chat client builder pattern.
4444

4545
```csharp
46-
var chatClient = new AzureOpenAIClient(new Uri("https://<myresource>.openai.azure.com"), new DefaultAzureCredential())
47-
.GetChatClient(deploymentName)
48-
.AsIChatClient();
46+
var chatClient = new AIProjectClient(
47+
new Uri("<your-foundry-project-endpoint>"),
48+
new DefaultAzureCredential())
49+
.GetProjectOpenAIClient()
50+
.GetResponsesClient()
51+
.AsIChatClient(deploymentName);
4952

5053
var middlewareEnabledChatClient = chatClient
5154
.AsBuilder()
@@ -62,12 +65,16 @@ var agent = new ChatClientAgent(middlewareEnabledChatClient, instructions: "You
6265
an agent via one of the helper methods on SDK clients.
6366

6467
```csharp
65-
var agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
66-
.GetChatClient(deploymentName)
67-
.AsAIAgent("You are a helpful assistant.", clientFactory: (chatClient) => chatClient
68-
.AsBuilder()
69-
.Use(getResponseFunc: CustomChatClientMiddleware, getStreamingResponseFunc: null)
70-
.Build());
68+
var agent = new AIProjectClient(
69+
new Uri("<your-foundry-project-endpoint>"),
70+
new DefaultAzureCredential())
71+
.AsAIAgent(
72+
model: deploymentName,
73+
instructions: "You are a helpful assistant.",
74+
clientFactory: (chatClient) => chatClient
75+
.AsBuilder()
76+
.Use(getResponseFunc: CustomChatClientMiddleware, getStreamingResponseFunc: null)
77+
.Build());
7178
```
7279

7380
## Agent Run Middleware

agent-framework/agents/middleware/result-overrides.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using System.Collections.Generic;
2323
using System.Linq;
2424
using System.Threading;
2525
using System.Threading.Tasks;
26-
using Azure.AI.OpenAI;
26+
using Azure.AI.Projects;
2727
using Azure.Identity;
2828
using Microsoft.Agents.AI;
2929
using Microsoft.Extensions.AI;
@@ -52,11 +52,12 @@ async Task<AgentResponse> ResultOverrideMiddleware(
5252
return new AgentResponse(modifiedMessages);
5353
}
5454

55-
AIAgent agent = new AzureOpenAIClient(
56-
new Uri("https://<myresource>.openai.azure.com"),
57-
new AzureCliCredential())
58-
.GetChatClient("gpt-4o-mini")
59-
.AsAIAgent(instructions: "You are a helpful weather assistant.");
55+
AIAgent agent = new AIProjectClient(
56+
new Uri("<your-foundry-project-endpoint>"),
57+
new DefaultAzureCredential())
58+
.AsAIAgent(
59+
model: "gpt-4o-mini",
60+
instructions: "You are a helpful weather assistant.");
6061

6162
var agentWithOverride = agent
6263
.AsBuilder()
@@ -66,6 +67,9 @@ var agentWithOverride = agent
6667
Console.WriteLine(await agentWithOverride.RunAsync("What's the weather in Seattle?"));
6768
```
6869

70+
> [!WARNING]
71+
> `DefaultAzureCredential` is convenient for development but requires careful consideration in production. In production, consider using a specific credential (e.g., `ManagedIdentityCredential`) to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
72+
6973
:::zone-end
7074

7175
:::zone pivot="programming-language-python"

0 commit comments

Comments
 (0)