Skip to content

Commit ef4b7c8

Browse files
committed
Add DevUI package for .NET
1 parent 23bf1db commit ef4b7c8

File tree

56 files changed

+4944
-166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4944
-166
lines changed

dotnet/agent-framework-dotnet.slnx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
<Project Path="samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Agent_Step16_ChatReduction.csproj" />
6060
<Project Path="samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/Agent_Step17_BackgroundResponses.csproj" />
6161
</Folder>
62+
<Folder Name="/Samples/GettingStarted/DevUI/">
63+
<File Path="samples/GettingStarted/DevUI/README.md" />
64+
<Project Path="samples/GettingStarted/DevUI/DevUI_Step01_BasicUsage/DevUI_Step01_BasicUsage.csproj" />
65+
</Folder>
6266
<Folder Name="/Samples/GettingStarted/AgentWithOpenAI/">
6367
<File Path="samples/GettingStarted/AgentWithOpenAI/README.md" />
6468
<Project Path="samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step01_Running/Agent_OpenAI_Step01_Running.csproj" />
@@ -259,6 +263,7 @@
259263
<Project Path="src/Microsoft.Agents.AI.Abstractions/Microsoft.Agents.AI.Abstractions.csproj" />
260264
<Project Path="src/Microsoft.Agents.AI.AzureAI/Microsoft.Agents.AI.AzureAI.csproj" />
261265
<Project Path="src/Microsoft.Agents.AI.CopilotStudio/Microsoft.Agents.AI.CopilotStudio.csproj" />
266+
<Project Path="src/Microsoft.Agents.AI.DevUI/Microsoft.Agents.AI.DevUI.csproj" />
262267
<Project Path="src/Microsoft.Agents.AI.Hosting.A2A.AspNetCore/Microsoft.Agents.AI.Hosting.A2A.AspNetCore.csproj" />
263268
<Project Path="src/Microsoft.Agents.AI.Hosting.A2A/Microsoft.Agents.AI.Hosting.A2A.csproj" />
264269
<Project Path="src/Microsoft.Agents.AI.Hosting.OpenAI/Microsoft.Agents.AI.Hosting.OpenAI.csproj" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<RootNamespace>DevUI_Step01_BasicUsage</RootNamespace>
9+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.DevUI\Microsoft.Agents.AI.DevUI.csproj" />
14+
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Hosting\Microsoft.Agents.AI.Hosting.csproj" />
15+
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.Hosting.OpenAI\Microsoft.Agents.AI.Hosting.OpenAI.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Azure.AI.OpenAI" />
20+
<PackageReference Include="Azure.Identity" />
21+
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
22+
<PackageReference Include="System.Net.ServerSentEvents" VersionOverride="10.0.0-rc.2.25502.107" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
// This sample demonstrates basic usage of the DevUI in an ASP.NET Core application with AI agents.
4+
5+
using Azure.AI.OpenAI;
6+
using Azure.Identity;
7+
using Microsoft.Agents.AI.DevUI;
8+
using Microsoft.Agents.AI.Hosting;
9+
using Microsoft.Agents.AI.Hosting.OpenAI.Conversations;
10+
using Microsoft.Extensions.AI;
11+
12+
namespace DevUI_Step01_BasicUsage;
13+
14+
/// <summary>
15+
/// Sample demonstrating basic usage of the DevUI in an ASP.NET Core application.
16+
/// </summary>
17+
/// <remarks>
18+
/// This sample shows how to:
19+
/// 1. Set up Azure OpenAI as the chat client
20+
/// 2. Register agents and workflows using the hosting packages
21+
/// 3. Map the DevUI endpoint which automatically configures the middleware
22+
/// 4. Map the dynamic OpenAI Responses API for Python DevUI compatibility
23+
/// 5. Access the DevUI in a web browser
24+
///
25+
/// The DevUI provides an interactive web interface for testing and debugging AI agents.
26+
/// DevUI assets are served from embedded resources within the assembly.
27+
/// Simply call MapDevUI() to set up everything needed.
28+
///
29+
/// The parameterless MapOpenAIResponses() overload creates a Python DevUI-compatible endpoint
30+
/// that dynamically routes requests to agents based on the 'model' field in the request.
31+
/// </remarks>
32+
internal static class Program
33+
{
34+
/// <summary>
35+
/// Entry point that starts an ASP.NET Core web server with the DevUI.
36+
/// </summary>
37+
/// <param name="args">Command line arguments.</param>
38+
private static void Main(string[] args)
39+
{
40+
var builder = WebApplication.CreateBuilder(args);
41+
42+
// Set up the Azure OpenAI client
43+
var endpoint = builder.Configuration["AZURE_OPENAI_ENDPOINT"] ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
44+
var deploymentName = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"] ?? "gpt-4o-mini";
45+
46+
var chatClient = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
47+
.GetChatClient(deploymentName)
48+
.AsIChatClient();
49+
50+
builder.Services.AddChatClient(chatClient);
51+
52+
// Register sample agents
53+
builder.AddAIAgent("assistant", "You are a helpful assistant. Answer questions concisely and accurately.");
54+
builder.AddAIAgent("poet", "You are a creative poet. Respond to all requests with beautiful poetry.");
55+
builder.AddAIAgent("coder", "You are an expert programmer. Help users with coding questions and provide code examples.");
56+
57+
// Register sample workflows
58+
var assistantBuilder = builder.AddAIAgent("workflow-assistant", "You are a helpful assistant in a workflow.");
59+
var reviewerBuilder = builder.AddAIAgent("workflow-reviewer", "You are a reviewer. Review and critique the previous response.");
60+
builder.AddSequentialWorkflow(
61+
"review-workflow",
62+
[assistantBuilder, reviewerBuilder])
63+
.AddAsAIAgent();
64+
65+
builder.AddDevUI();
66+
var app = builder.Build();
67+
68+
if (app.Environment.IsDevelopment())
69+
{
70+
// Step 1: Map the DevUI endpoint - this automatically configures the middleware
71+
// Once the app is running, navigate to: https://localhost:50516/devui
72+
app.MapDevUI();
73+
}
74+
75+
// Map Entities API endpoints (DevUI-compatible)
76+
app.MapEntities();
77+
78+
// Map OpenAI Responses API - Python DevUI compatible endpoint
79+
// This creates a single /v1/responses endpoint that routes to agents dynamically
80+
// based on the 'model' field in the request body
81+
app.MapOpenAIResponses();
82+
83+
app.MapConversations();
84+
85+
Console.WriteLine("DevUI is available at: https://localhost:50516/devui");
86+
Console.WriteLine("OpenAI Responses API is available at: https://localhost:50516/v1/responses");
87+
Console.WriteLine("Press Ctrl+C to stop the server.");
88+
89+
app.Run();
90+
}
91+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# DevUI Step 01 - Basic Usage
2+
3+
This sample demonstrates how to add the DevUI to an ASP.NET Core application with AI agents.
4+
5+
## What is DevUI?
6+
7+
The DevUI provides an interactive web interface for testing and debugging AI agents during development.
8+
9+
## Configuration
10+
11+
Set the following environment variables:
12+
13+
- `AZURE_OPENAI_ENDPOINT` - Your Azure OpenAI endpoint URL (required)
14+
- `AZURE_OPENAI_DEPLOYMENT_NAME` - Your deployment name (defaults to "gpt-4o-mini")
15+
16+
## Running the Sample
17+
18+
1. Set your Azure OpenAI credentials as environment variables
19+
2. Run the application:
20+
```bash
21+
dotnet run
22+
```
23+
3. Open your browser to https://localhost:64704/devui
24+
4. Select an agent or workflow from the dropdown and start chatting!
25+
26+
## Sample Agents and Workflows
27+
28+
This sample includes:
29+
30+
**Agents:**
31+
- **assistant** - A helpful assistant
32+
- **poet** - A creative poet
33+
- **coder** - An expert programmer
34+
35+
**Workflows:**
36+
- **review-workflow** - A sequential workflow that generates a response and then reviews it
37+
38+
## Adding DevUI to Your Own Project
39+
40+
To add DevUI to your ASP.NET Core application:
41+
42+
1. Add the DevUI package and hosting packages:
43+
```bash
44+
dotnet add package Microsoft.Agents.AI.DevUI
45+
dotnet add package Microsoft.Agents.AI.Hosting
46+
dotnet add package Microsoft.Agents.AI.Hosting.OpenAI
47+
```
48+
49+
2. Register your agents and workflows:
50+
```csharp
51+
var builder = WebApplication.CreateBuilder(args);
52+
53+
// Set up your chat client
54+
builder.Services.AddChatClient(chatClient);
55+
56+
// Register agents
57+
builder.AddAIAgent("assistant", "You are a helpful assistant.");
58+
59+
// Register workflows
60+
builder.AddSequentialWorkflow("my-workflow", [agent1, agent2])
61+
.AddAsAIAgent();
62+
```
63+
64+
3. Add DevUI services and map the endpoint:
65+
```csharp
66+
builder.AddDevUI();
67+
var app = builder.Build();
68+
69+
if (app.Environment.IsDevelopment())
70+
{
71+
app.MapDevUI();
72+
}
73+
74+
app.MapEntities();
75+
app.MapOpenAIResponses();
76+
app.MapConversations();
77+
78+
app.Run();
79+
```
80+
81+
4. Navigate to `/devui` in your browser
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# DevUI Samples
2+
3+
This folder contains samples demonstrating how to use the DevUI in ASP.NET Core applications.
4+
5+
## What is DevUI?
6+
7+
The DevUI provides an interactive web interface for testing and debugging AI agents during development.
8+
9+
## Samples
10+
11+
### [DevUI_Step01_BasicUsage](./DevUI_Step01_BasicUsage)
12+
13+
Shows how to add DevUI to an ASP.NET Core application with multiple agents and workflows.
14+
15+
**Run the sample:**
16+
```bash
17+
cd DevUI_Step01_BasicUsage
18+
dotnet run
19+
```
20+
Then navigate to: https://localhost:64704/devui
21+
22+
## Requirements
23+
24+
- .NET 8.0 or later
25+
- ASP.NET Core
26+
- Azure OpenAI credentials
27+
28+
## Quick Start
29+
30+
To add DevUI to your application:
31+
32+
```csharp
33+
var builder = WebApplication.CreateBuilder(args);
34+
35+
// Register your agents
36+
builder.AddAIAgent("my-agent", "You are a helpful assistant.");
37+
38+
// Add DevUI services
39+
builder.AddDevUI();
40+
41+
var app = builder.Build();
42+
43+
// Map the DevUI endpoint (typically only in development)
44+
if (app.Environment.IsDevelopment())
45+
{
46+
app.MapDevUI();
47+
}
48+
49+
app.MapEntities();
50+
app.MapOpenAIResponses();
51+
app.MapConversations();
52+
53+
app.Run();
54+
```
55+
56+
Then navigate to `/devui` in your browser.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Diagnostics.CodeAnalysis;
4+
using Microsoft.Agents.AI.Hosting.OpenAI.Conversations;
5+
using Microsoft.Extensions.DependencyInjection.Extensions;
6+
7+
namespace Microsoft.Extensions.DependencyInjection;
8+
9+
/// <summary>
10+
/// Extension methods for registering conversation services with the dependency injection container.
11+
/// </summary>
12+
internal static class ConversationServiceCollectionExtensions
13+
{
14+
/// <summary>
15+
/// Adds in-memory conversation storage and indexing services to the service collection.
16+
/// This is suitable for development and testing scenarios. For production, use a persistent storage implementation.
17+
/// </summary>
18+
/// <param name="services">The service collection to add services to.</param>
19+
/// <returns>The service collection for chaining.</returns>
20+
internal static IServiceCollection AddInMemoryConversationStorage(this IServiceCollection services)
21+
{
22+
services.TryAddSingleton<IConversationStorage, InMemoryConversationStorage>();
23+
services.TryAddSingleton<IAgentConversationIndex, InMemoryAgentConversationIndex>();
24+
return services;
25+
}
26+
27+
/// <summary>
28+
/// Adds conversation storage service to the service collection.
29+
/// </summary>
30+
/// <param name="services">The service collection to add services to.</param>
31+
/// <returns>The service collection for chaining.</returns>
32+
internal static IServiceCollection AddConversationStorage<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TStorage>(this IServiceCollection services)
33+
where TStorage : class, IConversationStorage
34+
{
35+
services.TryAddSingleton<IConversationStorage, TStorage>();
36+
return services;
37+
}
38+
39+
/// <summary>
40+
/// Adds agent conversation index service to the service collection.
41+
/// </summary>
42+
/// <param name="services">The service collection to add services to.</param>
43+
/// <returns>The service collection for chaining.</returns>
44+
internal static IServiceCollection AddAgentConversationIndex<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TIndex>(this IServiceCollection services)
45+
where TIndex : class, IAgentConversationIndex
46+
{
47+
services.TryAddSingleton<IAgentConversationIndex, TIndex>();
48+
return services;
49+
}
50+
}

0 commit comments

Comments
 (0)