Skip to content

Commit d407444

Browse files
authored
Merge pull request #403 from microsoft/docs/01-intro-to-ai-agents
Update 01-dotnet-agent-framework
2 parents 85c8867 + 34060c5 commit d407444

File tree

4 files changed

+132
-44
lines changed

4 files changed

+132
-44
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
description: 'Cherrypick a file deleted from the given commit between branches using Git.'
3+
mode: 'agent'
4+
---
5+
6+
# Cherry-picking a File Between Branches Using Git
7+
8+
Your goal is to cherry-pick a specific file deleted from a given commit between branches in a Git repository. Use the Git command line interface to accomplish this task.
9+
10+
## Inputs
11+
12+
- **Commit Hash**: $COMMIT - The hash of the commit from which to cherry-pick the file.
13+
- **File Path**: $FILE_PATH - The path of the file to be cherry-picked.
14+
15+
If you don't have the commit hash or file path, please ask the user for these details.
16+
17+
## Instructions
18+
19+
1. **Identify the Commit**: Determine the commit hash from which you want to cherry-pick the file.
20+
2. **Cherry-pick the File**: Use the `git checkout` command to cherry-pick the specific file from the identified commit. If the file doesn't exist in the given commit, find the closest commit where the file exists.
21+
3. **Commit the Changes**: Ask the user to stage and commit the changes to finalize the cherry-pick.

01-intro-to-ai-agents/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,14 @@ Agentic Frameworks allow developers to implement agentic patterns through code.
101101

102102
In this course, we will explore the research-driven AutoGen framework and the production-ready Agent framework from Semantic Kernel.
103103

104-
### Got More Questions about AI Agents?
104+
## Sample Codes
105105

106-
Join the [Azure AI Foundry Discord](https://aka.ms/ai-agents/discord) to meet with other learners, attend office hours and get your AI Agents questions answered.
106+
- Python: [Agent Framework](./code_samples/01-python-agent-framework.ipynb)
107+
- .NET: [Agent Framework](./code_samples/01-dotnet-agent-framework.md)
107108

109+
## Got More Questions about AI Agents?
108110

111+
Join the [Azure AI Foundry Discord](https://aka.ms/ai-agents/discord) to meet with other learners, attend office hours and get your AI Agents questions answered.
109112

110113
## Previous Lesson
111114

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
#!/usr/bin/dotnet run
22

3-
#:package Microsoft.Extensions.AI@9.9.1
4-
#:package Microsoft.Agents.AI.OpenAI@1.0.0-preview.251001.3
5-
#:package Microsoft.Agents.AI@1.0.0-preview.251001.3
6-
#:package DotNetEnv@3.1.1
3+
#:package Microsoft.Extensions.AI@9.*
4+
#:package Microsoft.Agents.AI.OpenAI@1.*-*
75

8-
using System;
9-
using System.ComponentModel;
106
using System.ClientModel;
11-
using System.Collections.Generic;
12-
using Microsoft.Extensions.AI;
7+
using System.ComponentModel;
8+
139
using Microsoft.Agents.AI;
14-
using OpenAI;
15-
using DotNetEnv;
10+
using Microsoft.Extensions.AI;
1611

17-
// Load environment variables from .env file (3 directories up)
18-
Env.Load("../../../.env");
12+
using OpenAI;
1913

2014
// Tool Function: Random Destination Generator
15+
// This static method will be available to the agent as a callable tool
16+
// The [Description] attribute helps the AI understand when to use this function
17+
// This demonstrates how to create custom tools for AI agents
2118
[Description("Provides a random vacation destination.")]
2219
static string GetRandomDestination()
2320
{
21+
// List of popular vacation destinations around the world
22+
// The agent will randomly select from these options
2423
var destinations = new List<string>
2524
{
2625
"Paris, France",
@@ -34,38 +33,52 @@ static string GetRandomDestination()
3433
"Bangkok, Thailand",
3534
"Vancouver, Canada"
3635
};
36+
37+
// Generate random index and return selected destination
38+
// Uses System.Random for simple random selection
3739
var random = new Random();
3840
int index = random.Next(destinations.Count);
3941
return destinations[index];
4042
}
4143

4244
// Extract configuration from environment variables
43-
var github_endpoint = Environment.GetEnvironmentVariable("GITHUB_ENDPOINT") ?? throw new InvalidOperationException("GITHUB_ENDPOINT is not set.");
44-
var github_model_id = Environment.GetEnvironmentVariable("GITHUB_MODEL_ID") ?? "gpt-4o-mini";
45-
var github_token = Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? throw new InvalidOperationException("GITHUB_TOKEN is not set.");
45+
// Retrieve the GitHub Models API endpoint, defaults to https://models.github.ai/inference if not specified
46+
// Retrieve the model ID, defaults to openai/gpt-5-mini if not specified
47+
// Retrieve the GitHub token for authentication, throws exception if not specified
48+
var github_endpoint = Environment.GetEnvironmentVariable("GH_ENDPOINT") ?? "https://models.github.ai/inference";
49+
var github_model_id = Environment.GetEnvironmentVariable("GH_MODEL_ID") ?? "openai/gpt-5-mini";
50+
var github_token = Environment.GetEnvironmentVariable("GH_TOKEN") ?? throw new InvalidOperationException("GH_TOKEN is not set.");
4651

4752
// Configure OpenAI Client Options
53+
// Create configuration options to point to GitHub Models endpoint
54+
// This redirects OpenAI client calls to GitHub's model inference service
4855
var openAIOptions = new OpenAIClientOptions()
4956
{
5057
Endpoint = new Uri(github_endpoint)
5158
};
5259

5360
// Initialize OpenAI Client with GitHub Models Configuration
61+
// Create OpenAI client using GitHub token for authentication
62+
// Configure it to use GitHub Models endpoint instead of OpenAI directly
5463
var openAIClient = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions);
5564

5665
// Create AI Agent with Travel Planning Capabilities
66+
// Initialize OpenAI client, get chat client for specified model, and create AI agent
67+
// Configure agent with travel planning instructions and random destination tool
68+
// The agent can now plan trips using the GetRandomDestination function
5769
AIAgent agent = openAIClient
5870
.GetChatClient(github_model_id)
5971
.CreateAIAgent(
6072
instructions: "You are a helpful AI Agent that can help plan vacations for customers at random destinations",
6173
tools: [AIFunctionFactory.Create(GetRandomDestination)]
6274
);
6375

64-
// Execute Agent: Plan a Day Trip (Non-Streaming)
65-
Console.WriteLine(await agent.RunAsync("Plan me a day trip"));
66-
67-
// Execute Agent: Plan a Day Trip (Streaming Response)
76+
// Execute Agent: Plan a Day Trip
77+
// Run the agent with streaming enabled for real-time response display
78+
// Shows the agent's thinking and response as it generates the content
79+
// Provides better user experience with immediate feedback
6880
await foreach (var update in agent.RunStreamingAsync("Plan me a day trip"))
6981
{
82+
await Task.Delay(10);
7083
Console.Write(update);
7184
}

01-intro-to-ai-agents/code_samples/01-dotnet-agent-framework.md

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
This notebook demonstrates how to build an intelligent travel planning agent using the Microsoft Agent Framework for .NET. The agent can automatically generate personalized day-trip itineraries for random destinations around the world.
66

7-
**Key Capabilities:**
7+
### Key Capabilities:
8+
89
- 🎲 **Random Destination Selection**: Uses a custom tool to pick vacation spots
910
- 🗺️ **Intelligent Trip Planning**: Creates detailed day-by-day itineraries
1011
- 🔄 **Real-time Streaming**: Supports both immediate and streaming responses
@@ -13,18 +14,21 @@ This notebook demonstrates how to build an intelligent travel planning agent usi
1314
## 🔧 Technical Architecture
1415

1516
### Core Technologies
17+
1618
- **Microsoft Agent Framework**: Latest .NET implementation for AI agent development
1719
- **GitHub Models Integration**: Uses GitHub's AI model inference service
1820
- **OpenAI API Compatibility**: Leverages OpenAI client libraries with custom endpoints
1921
- **Secure Configuration**: Environment-based API key management
2022

2123
### Key Components
24+
2225
1. **AIAgent**: The main agent orchestrator that handles conversation flow
2326
2. **Custom Tools**: `GetRandomDestination()` function available to the agent
2427
3. **Chat Client**: GitHub Models-backed conversation interface
2528
4. **Streaming Support**: Real-time response generation capabilities
2629

2730
### Integration Pattern
31+
2832
```mermaid
2933
graph LR
3034
A[User Request] --> B[AI Agent]
@@ -36,35 +40,68 @@ graph LR
3640

3741
## 🚀 Getting Started
3842

39-
**Prerequisites:**
40-
- .NET 10.0 or higher
41-
- GitHub Models API access token
42-
- Environment variables configured in `.env` file
43+
### Prerequisites
44+
45+
- [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.0) or higher
46+
- [GitHub Models API access token](https://docs.github.com/github-models/github-models-at-scale/using-your-own-api-keys-in-github-models)
4347

44-
**Required Environment Variables:**
45-
```env
46-
GITHUB_TOKEN=your_github_token
47-
GITHUB_ENDPOINT=https://models.inference.ai.azure.com
48-
GITHUB_MODEL_ID=gpt-4o-mini
48+
### Required Environment Variables
49+
50+
```bash
51+
# zsh/bash
52+
export GH_TOKEN=<your_github_token>
53+
export GH_ENDPOINT=https://models.github.ai/inference
54+
export GH_MODEL_ID=openai/gpt-5-mini
4955
```
5056

51-
Run the code sample below in sequence to see the travel agent in action!
57+
```powershell
58+
# PowerShell
59+
$env:GH_TOKEN = "<your_github_token>"
60+
$env:GH_ENDPOINT = "https://models.github.ai/inference"
61+
$env:GH_MODEL_ID = "openai/gpt-5-mini"
62+
```
63+
64+
### Sample Code
5265

53-
---
66+
To run the code example,
5467

55-
## .NET Single File App: AI Travel Agent Example
68+
```bash
69+
# zsh/bash
70+
chmod +x ./01-dotnet-agent-framework.cs
71+
./01-dotnet-agent-framework.cs
72+
```
5673

57-
See `01-dotnet-agent-framework.cs` for the complete runnable code sample.
74+
Or using the dotnet CLI:
5875

5976
```bash
60-
dotnet run 01-dotnet-agent-framework.cs
77+
dotnet run ./01-dotnet-agent-framework.cs
6178
```
6279

63-
### Sample Code
80+
See [`01-dotnet-agent-framework.cs`](./01-dotnet-agent-framework.cs) for the complete code.
6481

6582
```csharp
83+
#!/usr/bin/dotnet run
84+
85+
#:package Microsoft.Extensions.AI@9.*
86+
#:package Microsoft.Agents.AI.OpenAI@1.*-*
87+
88+
using System.ClientModel;
89+
using System.ComponentModel;
90+
91+
using Microsoft.Agents.AI;
92+
using Microsoft.Extensions.AI;
93+
94+
using OpenAI;
95+
96+
// Tool Function: Random Destination Generator
97+
// This static method will be available to the agent as a callable tool
98+
// The [Description] attribute helps the AI understand when to use this function
99+
// This demonstrates how to create custom tools for AI agents
100+
[Description("Provides a random vacation destination.")]
66101
static string GetRandomDestination()
67102
{
103+
// List of popular vacation destinations around the world
104+
// The agent will randomly select from these options
68105
var destinations = new List<string>
69106
{
70107
"Paris, France",
@@ -78,39 +115,53 @@ static string GetRandomDestination()
78115
"Bangkok, Thailand",
79116
"Vancouver, Canada"
80117
};
118+
119+
// Generate random index and return selected destination
120+
// Uses System.Random for simple random selection
81121
var random = new Random();
82122
int index = random.Next(destinations.Count);
83123
return destinations[index];
84124
}
85125

86126
// Extract configuration from environment variables
87-
var github_endpoint = Environment.GetEnvironmentVariable("GITHUB_ENDPOINT") ?? throw new InvalidOperationException("GITHUB_ENDPOINT is not set.");
88-
var github_model_id = Environment.GetEnvironmentVariable("GITHUB_MODEL_ID") ?? "gpt-4o-mini";
89-
var github_token = Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? throw new InvalidOperationException("GITHUB_TOKEN is not set.");
127+
// Retrieve the GitHub Models API endpoint, defaults to https://models.github.ai/inference if not specified
128+
// Retrieve the model ID, defaults to openai/gpt-5-mini if not specified
129+
// Retrieve the GitHub token for authentication, throws exception if not specified
130+
var github_endpoint = Environment.GetEnvironmentVariable("GH_ENDPOINT") ?? "https://models.github.ai/inference";
131+
var github_model_id = Environment.GetEnvironmentVariable("GH_MODEL_ID") ?? "openai/gpt-5-mini";
132+
var github_token = Environment.GetEnvironmentVariable("GH_TOKEN") ?? throw new InvalidOperationException("GH_TOKEN is not set.");
90133

91134
// Configure OpenAI Client Options
135+
// Create configuration options to point to GitHub Models endpoint
136+
// This redirects OpenAI client calls to GitHub's model inference service
92137
var openAIOptions = new OpenAIClientOptions()
93138
{
94139
Endpoint = new Uri(github_endpoint)
95140
};
96141

97142
// Initialize OpenAI Client with GitHub Models Configuration
143+
// Create OpenAI client using GitHub token for authentication
144+
// Configure it to use GitHub Models endpoint instead of OpenAI directly
98145
var openAIClient = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions);
99146

100147
// Create AI Agent with Travel Planning Capabilities
148+
// Initialize OpenAI client, get chat client for specified model, and create AI agent
149+
// Configure agent with travel planning instructions and random destination tool
150+
// The agent can now plan trips using the GetRandomDestination function
101151
AIAgent agent = openAIClient
102152
.GetChatClient(github_model_id)
103153
.CreateAIAgent(
104154
instructions: "You are a helpful AI Agent that can help plan vacations for customers at random destinations",
105155
tools: [AIFunctionFactory.Create(GetRandomDestination)]
106156
);
107157

108-
// Execute Agent: Plan a Day Trip (Non-Streaming)
109-
Console.WriteLine(await agent.RunAsync("Plan me a day trip"));
110-
111-
// Execute Agent: Plan a Day Trip (Streaming Response)
158+
// Execute Agent: Plan a Day Trip
159+
// Run the agent with streaming enabled for real-time response display
160+
// Shows the agent's thinking and response as it generates the content
161+
// Provides better user experience with immediate feedback
112162
await foreach (var update in agent.RunStreamingAsync("Plan me a day trip"))
113163
{
164+
await Task.Delay(10);
114165
Console.Write(update);
115166
}
116167
```

0 commit comments

Comments
 (0)