Skip to content

Commit ea5d8a9

Browse files
MaryanneNjeriMaryanne Gichohi
andauthored
Add .NET sample chat agent app (#1147)
* Add .NET sample chat agent app * Update ChatAgent submodule with new example * Add .NET sample app * Update folder * Suppress warning * Address PR comment --------- Co-authored-by: Maryanne Gichohi <mgichohi@microsoft.com>
1 parent f96ad90 commit ea5d8a9

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<NoWarn>$(NoWarn);OPENAI001</NoWarn>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Azure.AI.Projects" Version="2.0.0" />
13+
<PackageReference Include="Azure.Identity" Version="1.20.0" />
14+
<PackageReference Include="Microsoft.Agents.AI" Version="1.0.0" />
15+
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0" />
16+
<PackageReference Include="Microsoft.Agents.AI.Declarative" Version="1.0.0-rc4" />
17+
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.4.1" />
18+
<PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="8.5.0" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Azure.Core;
2+
using Azure.Identity;
3+
using Microsoft.Agents.AI;
4+
using Microsoft.Extensions.AI;
5+
using Azure.AI.Projects;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
8+
9+
TokenCredential credential = new AzureCliCredential();
10+
11+
// Load configuration from Azure App Configuration
12+
IConfiguration configuration = new ConfigurationBuilder()
13+
.AddAzureAppConfiguration(options =>
14+
{
15+
Uri endpoint = new(Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT") ??
16+
throw new InvalidOperationException("The environment variable 'AZURE_APPCONFIGURATION_ENDPOINT' is not set or is empty"));
17+
options.Connect(endpoint, credential)
18+
.Select("ChatAgent:*")
19+
}).Build();
20+
21+
var endpoint = configuration["ChatAgent:ProjectEndpoint"];
22+
var deploymentName = configuration["ChatAgent:DeploymentName"];
23+
24+
IChatClient chatClient = new AIProjectClient(
25+
new Uri(endpoint), credential)
26+
.GetProjectOpenAIClient()
27+
.GetProjectResponsesClient()
28+
.AsIChatClient();
29+
30+
var agentSpec = configuration["ChatAgent:Spec"];
31+
32+
var agentFactory = new ChatClientPromptAgentFactory(chatClient);
33+
34+
AIAgent agent = await agentFactory.CreateFromYamlAsync(agentSpec);
35+
36+
while (true)
37+
{
38+
Console.WriteLine("How can I help? (type 'quit' to exit)");
39+
40+
Console.Write("User: ");
41+
42+
var userInput = Console.ReadLine();
43+
44+
if (userInput?.Trim().ToLower() == "quit")
45+
{
46+
break;
47+
}
48+
49+
var response = await agent.RunAsync(userInput);
50+
51+
Console.WriteLine($"Agent response: {response}");
52+
Console.WriteLine("Press enter to continue...");
53+
Console.ReadLine();
54+
}
55+
56+
Console.WriteLine("Goodbye!");
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Azure App Configuration - AI Agent chat application
2+
3+
This sample demonstrates using Azure App Configuration to load agent YAML specifications that define AI agent behavior, prompts, and model configurations for a chat application.
4+
5+
## Features
6+
- Integrates with Azure AI Agent Framework to create a conversational AI agent
7+
- Loads agent YAML specifications from Azure App Configuration.
8+
9+
## Prerequisites
10+
11+
- .NET 10 SDK
12+
- An Azure subscription with:
13+
- An Azure App Configuration store
14+
- An Azure AI project with a deployed gpt-5 model.
15+
- User has **App Configuration Reader** role assigned for the Azure App Configuration resource.
16+
- User has **Azure AI User** role assigned for the Azure AI project.
17+
18+
## Setup
19+
20+
1. Clone the repository and navigate to the `examples\DotNetCore\ChatAgent` directory:
21+
```bash
22+
git clone https://github.com/Azure/AppConfiguration.git
23+
cd examples\DotNetCore\ChatAgent
24+
```
25+
26+
1. Install the required packages:
27+
28+
```bash
29+
dotnet restore
30+
```
31+
32+
1. Add the following key-values to your Azure App Configuration store.
33+
34+
| Key | Value |
35+
|-----|-------|
36+
| ChatAgent:Spec | _See YAML below_ |
37+
| ChatAgent:ProjectEndpoint | _Your Azure AI project endpoint_ |
38+
| ChatAgent:DeploymentName| gpt-5 |
39+
40+
**YAML specification for _ChatAgent:Spec_**
41+
```yaml
42+
kind: Prompt
43+
name: ChatAgent
44+
description: Agent example with web search
45+
instructions: You are a helpful assistant with access to web search.
46+
model:
47+
id: gpt-5
48+
connection:
49+
kind: remote
50+
tools:
51+
- kind: webSearch
52+
name: WebSearchTool
53+
description: Search the web for live information.
54+
```
55+
56+
1. Set the required environment variable:
57+
58+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
59+
60+
```cmd
61+
setx AZURE_APPCONFIGURATION_ENDPOINT "<endpoint-of-your-app-configuration-store>"
62+
```
63+
64+
If you use PowerShell, run the following command:
65+
```powershell
66+
$Env:AZURE_APPCONFIGURATION_ENDPOINT="<endpoint-of-your-app-configuration-store>"
67+
```
68+
69+
If you use macOS or Linux run the following command:
70+
```bash
71+
export AZURE_APPCONFIGURATION_ENDPOINT='<endpoint-of-your-app-configuration-store>'
72+
```
73+
74+
## Run the Application
75+
76+
1. Start the console application:
77+
78+
```cmd
79+
dotnet run
80+
```
81+
82+
1. Type the message "What is the weather in Seattle today?" when prompted with "How can I help?" and then press the Enter key
83+
84+
```Output
85+
How can I help? (type 'quit' to exit)
86+
User: What is the weather in Seattle today ?
87+
Agent response: Seattle weather for today (Thursday, April 9, 2026):
88+
89+
- Current conditions (as of ~10:48 AM PDT): 55°F, sunny. Wind N 6 mph (gusts 7), humidity 55%, pressure 30.05 in. ([wunderground.com](https://www.wunderground.com/weather/us/wa/seattle))
90+
- Today’s forecast: Mostly sunny and mild. High around 64–65°F; tonight’s low near 43–44°F. Very low chance of precipitation and light winds. ([wunderground.com](https://www.wunderground.com/weather/us/wa/seattle))
91+
92+
Want the hour‑by‑hour forecast or weekend outlook?
93+
Press enter to continue...
94+
```

0 commit comments

Comments
 (0)