Skip to content

Commit 7107e07

Browse files
committed
Update 02-dotnet-agent-framework
- Also updated 01-dotnet-agent-framework for consistency
1 parent b3fd4be commit 7107e07

File tree

6 files changed

+215
-191
lines changed

6 files changed

+215
-191
lines changed

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

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

3-
#:package Microsoft.Extensions.AI@9.*
3+
#:package Microsoft.Extensions.AI@10.*
44
#:package Microsoft.Agents.AI.OpenAI@1.*-*
55

66
using System.ClientModel;
@@ -63,8 +63,8 @@ static string GetRandomDestination()
6363
var openAIClient = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions);
6464

6565
// 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
66+
// Initialize complete agent pipeline: OpenAI client → Chat client → AI agent
67+
// Configure agent with name, instructions, and available tools
6868
// The agent can now plan trips using the GetRandomDestination function
6969
AIAgent agent = openAIClient
7070
.GetChatClient(github_model_id)

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 📋 Scenario Overview
44

5-
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.
5+
This example 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

77
### Key Capabilities:
88

@@ -164,4 +164,18 @@ await foreach (var update in agent.RunStreamingAsync("Plan me a day trip"))
164164
await Task.Delay(10);
165165
Console.Write(update);
166166
}
167-
```
167+
```
168+
169+
## 🎓 Key Takeaways
170+
171+
1. **Agent Architecture**: The Microsoft Agent Framework provides a clean, type-safe approach to building AI agents in .NET
172+
2. **Tool Integration**: Functions decorated with `[Description]` attributes become available tools for the agent
173+
3. **Configuration Management**: Environment variables and secure credential handling follow .NET best practices
174+
4. **OpenAI Compatibility**: GitHub Models integration works seamlessly through OpenAI-compatible APIs
175+
176+
## 🔗 Additional Resources
177+
178+
- [Microsoft Agent Framework Documentation](https://learn.microsoft.com/agent-framework)
179+
- [GitHub Models Marketplace](https://github.com/marketplace?type=models)
180+
- [Microsoft.Extensions.AI](https://learn.microsoft.com/dotnet/ai/microsoft-extensions-ai)
181+
- [.NET Single File Apps](https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app)

02-explore-agentic-frameworks/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,12 @@ The answer is yes, you can integrate your existing Azure ecosystem tools directl
739739

740740
For AutoGen and Semantic Kernel, you can also integrate with Azure services, but it may require you to call the Azure services from your code. Another way to integrate is to use the Azure SDKs to interact with Azure services from your agents. Additionally, like was mentioned, you can use Azure AI Agent Service as an orchestrator for your agents built in AutoGen or Semantic Kernel which would give easy access to the Azure ecosystem.
741741

742-
### Got More Questions about AI Agent Frameworks?
742+
## Sample Codes
743+
744+
- Python: [Agent Framework](./code_samples/02-python-agent-framework.ipynb)
745+
- .NET: [Agent Framework](./code_samples/02-dotnet-agent-framework.md)
746+
747+
## Got More Questions about AI Agent Frameworks?
743748

744749
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.
745750
Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,126 @@
11
#!/usr/bin/dotnet run
2-
#:package Microsoft.Extensions.AI@9.9.1
3-
#:package Microsoft.Agents.AI.OpenAI@1.0.0-preview.251001.3
4-
#:package Microsoft.Agents.AI@1.0.0-preview.251001.3
5-
#:package DotNetEnv@3.1.1
62

7-
// 📚 Import Essential Namespaces for Agent Development
8-
using System;
9-
using System.ComponentModel;
3+
#:package Microsoft.Extensions.AI@10.*
4+
#:package Microsoft.Agents.AI.OpenAI@1.*-*
5+
106
using System.ClientModel;
11-
using Microsoft.Extensions.AI;
7+
using System.ComponentModel;
8+
129
using Microsoft.Agents.AI;
10+
using Microsoft.Extensions.AI;
11+
1312
using OpenAI;
14-
using DotNetEnv;
15-
16-
// 🔧 Load Environment Configuration from .env File
17-
// Load configuration from a .env file located 3 directories up from current location
18-
// This loads: GITHUB_ENDPOINT, GITHUB_TOKEN, GITHUB_MODEL_ID
19-
// Ensures secure handling of API credentials and configuration settings
20-
Env.Load("../../../.env");
21-
22-
// 🎲 Tool Function: Random Destination Generator (.NET Implementation)
23-
// This static method demonstrates how to create tools for agents in .NET
24-
// The [Description] attribute helps the AI understand when and how to use this function
25-
// This showcases the .NET approach to extending agent capabilities with custom tools
13+
14+
// 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
2618
[Description("Provides a random vacation destination.")]
2719
static string GetRandomDestination()
2820
{
29-
// Curated list of popular vacation destinations worldwide
30-
// The agent will randomly select from these options when users need destination suggestions
21+
// List of popular vacation destinations around the world
22+
// The agent will randomly select from these options
3123
var destinations = new List<string>
3224
{
33-
"Paris, France", // European cultural capital
34-
"Tokyo, Japan", // Asian modern metropolis
35-
"New York City, USA", // American urban experience
36-
"Sydney, Australia", // Oceanic coastal beauty
37-
"Rome, Italy", // Historical European city
38-
"Barcelona, Spain", // Mediterranean cultural hub
39-
"Cape Town, South Africa", // African scenic destination
40-
"Rio de Janeiro, Brazil", // South American beach city
41-
"Bangkok, Thailand", // Southeast Asian cultural center
42-
"Vancouver, Canada" // North American natural beauty
25+
"Paris, France",
26+
"Tokyo, Japan",
27+
"New York City, USA",
28+
"Sydney, Australia",
29+
"Rome, Italy",
30+
"Barcelona, Spain",
31+
"Cape Town, South Africa",
32+
"Rio de Janeiro, Brazil",
33+
"Bangkok, Thailand",
34+
"Vancouver, Canada"
4335
};
4436

45-
// Generate random index using System.Random and return selected destination
46-
// This demonstrates simple random selection in .NET
37+
// Generate random index and return selected destination
38+
// Uses System.Random for simple random selection
4739
var random = new Random();
4840
int index = random.Next(destinations.Count);
4941
return destinations[index];
5042
}
5143

52-
// 🔑 Extract and Validate Configuration from Environment Variables
53-
// Retrieve GitHub Models API endpoint - throws exception if not configured
54-
var github_endpoint = Environment.GetEnvironmentVariable("GITHUB_ENDPOINT") ?? throw new InvalidOperationException("GITHUB_ENDPOINT is not set.");
55-
// Get the AI model ID - defaults to gpt-4o-mini if not specified
56-
var github_model_id = Environment.GetEnvironmentVariable("GITHUB_MODEL_ID") ?? "gpt-4o-mini";
57-
// Retrieve GitHub authentication token - throws exception if missing
58-
var github_token = Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? throw new InvalidOperationException("GITHUB_TOKEN is not set.");
59-
60-
// ⚙️ Configure OpenAI Client Options for GitHub Models Integration
61-
// Create configuration options to redirect OpenAI client calls to GitHub Models endpoint
62-
// This enables using OpenAI-compatible APIs with GitHub's model inference service
44+
// Extract configuration from environment variables
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.");
51+
52+
// 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
6355
var openAIOptions = new OpenAIClientOptions()
6456
{
65-
Endpoint = new Uri(github_endpoint) // Set custom endpoint for GitHub Models API
57+
Endpoint = new Uri(github_endpoint)
6658
};
6759

68-
// 🔌 Initialize OpenAI Client with GitHub Models Configuration
69-
// Create OpenAI client instance using GitHub token for authentication
60+
// Initialize OpenAI Client with GitHub Models Configuration
61+
// Create OpenAI client using GitHub token for authentication
7062
// Configure it to use GitHub Models endpoint instead of OpenAI directly
71-
// This client will handle all communication with the AI model
7263
var openAIClient = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions);
7364

74-
// 🤖 Define Agent Identity and Comprehensive Instructions
65+
// Define Agent Identity and Comprehensive Instructions
7566
// Agent name for identification and logging purposes
76-
const string AGENT_NAME = "TravelAgent";
67+
var AGENT_NAME = "TravelAgent";
7768

7869
// Detailed instructions that define the agent's personality, capabilities, and behavior
7970
// This system prompt shapes how the agent responds and interacts with users
80-
const string AGENT_INSTRUCTIONS = @"You are a helpful AI Agent that can help plan vacations for customers.
71+
var AGENT_INSTRUCTIONS = """
72+
You are a helpful AI Agent that can help plan vacations for customers.
8173
8274
Important: When users specify a destination, always plan for that location. Only suggest random destinations when the user hasn't specified a preference.
8375
8476
When the conversation begins, introduce yourself with this message:
85-
""Hello! I'm your TravelAgent assistant. I can help plan vacations and suggest interesting destinations for you. Here are some things you can ask me:
77+
"Hello! I'm your TravelAgent assistant. I can help plan vacations and suggest interesting destinations for you. Here are some things you can ask me:
8678
1. Plan a day trip to a specific location
8779
2. Suggest a random vacation destination
8880
3. Find destinations with specific features (beaches, mountains, historical sites, etc.)
8981
4. Plan an alternative trip if you don't like my first suggestion
9082
91-
What kind of trip would you like me to help you plan today?""
83+
What kind of trip would you like me to help you plan today?"
9284
93-
Always prioritize user preferences. If they mention a specific destination like ""Bali"" or ""Paris,"" focus your planning on that location rather than suggesting alternatives.
94-
";
85+
Always prioritize user preferences. If they mention a specific destination like "Bali" or "Paris," focus your planning on that location rather than suggesting alternatives.
86+
""";
9587

96-
// 🤖 Create AI Agent with Advanced Travel Planning Capabilities
88+
// Create AI Agent with Advanced Travel Planning Capabilities
9789
// Initialize complete agent pipeline: OpenAI client → Chat client → AI agent
9890
// Configure agent with name, detailed instructions, and available tools
9991
// This demonstrates the .NET agent creation pattern with full configuration
100-
AIAgent agent = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions)
101-
.GetChatClient(github_model_id) // Get chat client for the specified AI model
92+
AIAgent agent = openAIClient
93+
.GetChatClient(github_model_id)
10294
.CreateAIAgent(
103-
name: AGENT_NAME, // Set agent identifier for logging and tracking
104-
instructions: AGENT_INSTRUCTIONS, // Comprehensive behavior and personality instructions
105-
tools: [AIFunctionFactory.Create((Func<string>)GetRandomDestination)] // Register tool functions
95+
name: AGENT_NAME,
96+
instructions: AGENT_INSTRUCTIONS,
97+
tools: [AIFunctionFactory.Create(GetRandomDestination)]
10698
);
10799

108-
// 🧵 Create New Conversation Thread for Context Management
100+
// Create New Conversation Thread for Context Management
109101
// Initialize a new conversation thread to maintain context across multiple interactions
110102
// Threads enable the agent to remember previous exchanges and maintain conversational state
111103
// This is essential for multi-turn conversations and contextual understanding
112104
AgentThread thread = agent.GetNewThread();
113105

114-
// 🚀 Execute Agent: First Travel Planning Request
106+
// Execute Agent: First Travel Planning Request
115107
// Run the agent with an initial request that will likely trigger the random destination tool
116108
// The agent will analyze the request, use the GetRandomDestination tool, and create an itinerary
117109
// Using the thread parameter maintains conversation context for subsequent interactions
118-
Console.WriteLine(await agent.RunAsync("Plan me a day trip", thread));
110+
await foreach (var update in agent.RunStreamingAsync("Plan me a day trip", thread))
111+
{
112+
await Task.Delay(10);
113+
Console.Write(update);
114+
}
119115

120-
// 🔄 Execute Agent: Follow-up Request with Context Awareness
116+
Console.WriteLine();
117+
118+
// Execute Agent: Follow-up Request with Context Awareness
121119
// Demonstrate contextual conversation by referencing the previous response
122120
// The agent remembers the previous destination suggestion and will provide an alternative
123121
// This showcases the power of conversation threads and contextual understanding in .NET agents
124-
Console.WriteLine(await agent.RunAsync("I don't like that destination. Plan me another vacation.", thread));
122+
await foreach (var update in agent.RunStreamingAsync("I don't like that destination. Plan me another vacation.", thread))
123+
{
124+
await Task.Delay(10);
125+
Console.Write(update);
126+
}

0 commit comments

Comments
 (0)