Skip to content

Commit 230e69c

Browse files
committed
Add missing code snippet
1 parent d8717a3 commit 230e69c

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

03-agentic-design-patterns/code_samples/03-dotnet-agent-framework.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,130 @@ dotnet run ./03-dotnet-agent-framework.cs
161161
See [`03-dotnet-agent-framework.cs`](./03-dotnet-agent-framework.cs) for the complete code.
162162

163163
```csharp
164-
164+
#!/usr/bin/dotnet run
165+
166+
#:package Microsoft.Extensions.AI@10.*
167+
#:package Microsoft.Agents.AI.OpenAI@1.*-*
168+
169+
using System.ClientModel;
170+
using System.ComponentModel;
171+
172+
using Microsoft.Agents.AI;
173+
using Microsoft.Extensions.AI;
174+
175+
using OpenAI;
176+
177+
// Tool Function: Random Destination Generator
178+
// This static method will be available to the agent as a callable tool
179+
// The [Description] attribute helps the AI understand when to use this function
180+
// This demonstrates how to create custom tools for AI agents
181+
[Description("Provides a random vacation destination.")]
182+
static string GetRandomDestination()
183+
{
184+
// List of popular vacation destinations around the world
185+
// The agent will randomly select from these options
186+
var destinations = new List<string>
187+
{
188+
"Paris, France",
189+
"Tokyo, Japan",
190+
"New York City, USA",
191+
"Sydney, Australia",
192+
"Rome, Italy",
193+
"Barcelona, Spain",
194+
"Cape Town, South Africa",
195+
"Rio de Janeiro, Brazil",
196+
"Bangkok, Thailand",
197+
"Vancouver, Canada"
198+
};
199+
200+
// Generate random index and return selected destination
201+
// Uses System.Random for simple random selection
202+
var random = new Random();
203+
int index = random.Next(destinations.Count);
204+
return destinations[index];
205+
}
206+
207+
// Extract configuration from environment variables
208+
// Retrieve the GitHub Models API endpoint, defaults to https://models.github.ai/inference if not specified
209+
// Retrieve the model ID, defaults to openai/gpt-5-mini if not specified
210+
// Retrieve the GitHub token for authentication, throws exception if not specified
211+
var github_endpoint = Environment.GetEnvironmentVariable("GH_ENDPOINT") ?? "https://models.github.ai/inference";
212+
var github_model_id = Environment.GetEnvironmentVariable("GH_MODEL_ID") ?? "openai/gpt-5-mini";
213+
var github_token = Environment.GetEnvironmentVariable("GH_TOKEN") ?? throw new InvalidOperationException("GH_TOKEN is not set.");
214+
215+
// Configure OpenAI Client Options
216+
// Create configuration options to point to GitHub Models endpoint
217+
// This redirects OpenAI client calls to GitHub's model inference service
218+
var openAIOptions = new OpenAIClientOptions()
219+
{
220+
Endpoint = new Uri(github_endpoint)
221+
};
222+
223+
// Initialize OpenAI Client with GitHub Models Configuration
224+
// Create OpenAI client using GitHub token for authentication
225+
// Configure it to use GitHub Models endpoint instead of OpenAI directly
226+
var openAIClient = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions);
227+
228+
// Define Agent Identity and Comprehensive Instructions
229+
// Agent name for identification and logging purposes
230+
var AGENT_NAME = "TravelAgent";
231+
232+
// Detailed instructions that define the agent's personality, capabilities, and behavior
233+
// This system prompt shapes how the agent responds and interacts with users
234+
var AGENT_INSTRUCTIONS = """
235+
You are a helpful AI Agent that can help plan vacations for customers.
236+
237+
Important: When users specify a destination, always plan for that location. Only suggest random destinations when the user hasn't specified a preference.
238+
239+
When the conversation begins, introduce yourself with this message:
240+
"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:
241+
1. Plan a day trip to a specific location
242+
2. Suggest a random vacation destination
243+
3. Find destinations with specific features (beaches, mountains, historical sites, etc.)
244+
4. Plan an alternative trip if you don't like my first suggestion
245+
246+
What kind of trip would you like me to help you plan today?"
247+
248+
Always prioritize user preferences. If they mention a specific destination like "Bali" or "Paris," focus your planning on that location rather than suggesting alternatives.
249+
""";
250+
251+
// Create AI Agent with Advanced Travel Planning Capabilities
252+
// Initialize complete agent pipeline: OpenAI client → Chat client → AI agent
253+
// Configure agent with name, detailed instructions, and available tools
254+
// This demonstrates the .NET agent creation pattern with full configuration
255+
AIAgent agent = openAIClient
256+
.GetChatClient(github_model_id)
257+
.CreateAIAgent(
258+
name: AGENT_NAME,
259+
instructions: AGENT_INSTRUCTIONS,
260+
tools: [AIFunctionFactory.Create(GetRandomDestination)]
261+
);
262+
263+
// Create New Conversation Thread for Context Management
264+
// Initialize a new conversation thread to maintain context across multiple interactions
265+
// Threads enable the agent to remember previous exchanges and maintain conversational state
266+
// This is essential for multi-turn conversations and contextual understanding
267+
AgentThread thread = agent.GetNewThread();
268+
269+
// Execute Agent: First Travel Planning Request
270+
// Run the agent with an initial request that will likely trigger the random destination tool
271+
// The agent will analyze the request, use the GetRandomDestination tool, and create an itinerary
272+
// Using the thread parameter maintains conversation context for subsequent interactions
273+
await foreach (var update in agent.RunStreamingAsync("Plan me a day trip", thread))
274+
{
275+
await Task.Delay(10);
276+
Console.Write(update);
277+
}
278+
279+
Console.WriteLine();
280+
281+
// Execute Agent: Follow-up Request with Context Awareness
282+
// Demonstrate contextual conversation by referencing the previous response
283+
// The agent remembers the previous destination suggestion and will provide an alternative
284+
// This showcases the power of conversation threads and contextual understanding in .NET agents
285+
await foreach (var update in agent.RunStreamingAsync("I don't like that destination. Plan me another vacation.", thread))
286+
{
287+
await Task.Delay(10);
288+
Console.Write(update);
289+
}
165290
```

0 commit comments

Comments
 (0)