ai-agents-for-beginners

🌍 AI Travel Agent with Microsoft Agent Framework (.NET)

πŸ“‹ Scenario Overview

αž§αž‘αžΆαž αžšαžŽαŸαž“αŸαŸ‡αž”αž„αŸ’αž αžΆαž‰αž–αžΈαžšαž”αŸ€αž”αž”αž„αŸ’αž€αžΎαžαž—αŸ’αž“αžΆαž€αŸ‹αž„αžΆαžšαž’αŸ’αžœαžΎαžŠαŸ†αžŽαžΎαžšαžŠαŸ‚αž›αž˜αžΆαž“αž”αž‰αŸ’αž‰αžΆαžŸαž·αž”αŸ’αž”αž“αž·αž˜αŸ’αž˜αž·αžαžŠαŸ„αž™αž”αŸ’αžšαžΎ Microsoft Agent Framework αžŸαž˜αŸ’αžšαžΆαž”αŸ‹ .NETαŸ” αž—αŸ’αž“αžΆαž€αŸ‹αž„αžΆαžšαž“αŸαŸ‡αž’αžΆαž…αž”αž„αŸ’αž€αžΎαžβ€‹αž€αž˜αŸ’αž˜αžœαž·αž’αžΈαžŠαŸ†αžŽαžΎαžšαž€αž˜αŸ’αžŸαžΆαž“αŸ’αžβ€‹αž”αŸ’αžšαž…αžΆαŸ†αžαŸ’αž„αŸƒαžŠαŸ‚αž›αž•αŸ’αž‘αžΆαž›αŸ‹αžαŸ’αž›αž½αž“αžŸαž˜αŸ’αžšαžΆαž”αŸ‹αž€αž“αŸ’αž›αŸ‚αž„αž‘αŸαžŸαž…αžšαžŽαŸαž…αŸƒαžŠαž“αŸ’αž™αž‡αžΆαž…αŸ’αžšαžΎαž“αž“αŸ…αž‡αž»αŸ†αžœαž·αž‰αž–αž·αž—αž–αž›αŸ„αž€αž”αžΆαž“αžŠαŸ„αž™αžŸαŸ’αžœαŸαž™αžŠαŸƒαŸ”

Key Capabilities:

πŸ”§ Technical Architecture

Core Technologies

Key Components

  1. AIAgent: The main agent orchestrator that handles conversation flow
  2. Custom Tools: GetRandomDestination() function available to the agent
  3. Chat Client: GitHub Models-backed conversation interface
  4. Streaming Support: Real-time response generation capabilities

Integration Pattern

graph LR
    A[αžŸαŸ†αžŽαžΎαž’αŸ’αž“αž€αž”αŸ’αžšαžΎ] --> B[αž—αŸ’αž“αžΆαž€αŸ‹αž„αžΆαžš AI]
    B --> C[API αž˜αŸ‰αžΌαžŒαŸ‚αž› GitHub]
    B --> D[αž§αž”αž€αžšαžŽαŸ GetRandomDestination]
    C --> E[αž•αŸ‚αž“αž€αžΆαžšαž’αŸ’αžœαžΎαžŠαŸ†αžŽαžΎαžš]
    D --> E

πŸš€ Getting Started

Prerequisites

Required Environment Variables

# zsh/αž”αžΆαžŸ
export GH_TOKEN=<your_github_token>
export GH_ENDPOINT=https://models.github.ai/inference
export GH_MODEL_ID=openai/gpt-5-mini
# αž–αžΆαžœαžΎαžšαžŸαŸ‚αž›
$env:GH_TOKEN = "<your_github_token>"
$env:GH_ENDPOINT = "https://models.github.ai/inference"
$env:GH_MODEL_ID = "openai/gpt-5-mini"

Sample Code

To run the code example,

# zsh/bash
chmod +x ./01-dotnet-agent-framework.cs
./01-dotnet-agent-framework.cs

Or using the dotnet CLI:

dotnet run ./01-dotnet-agent-framework.cs

See 01-dotnet-agent-framework.cs for the complete code.

#!/usr/bin/dotnet run

#:package Microsoft.Extensions.AI@9.*
#:package Microsoft.Agents.AI.OpenAI@1.*-*

using System.ClientModel;
using System.ComponentModel;

using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;

using OpenAI;

// Tool Function: Random Destination Generator
// This static method will be available to the agent as a callable tool
// The [Description] attribute helps the AI understand when to use this function
// This demonstrates how to create custom tools for AI agents
[Description("Provides a random vacation destination.")]
static string GetRandomDestination()
{
    // List of popular vacation destinations around the world
    // The agent will randomly select from these options
    var destinations = new List<string>
    {
        "Paris, France",
        "Tokyo, Japan",
        "New York City, USA",
        "Sydney, Australia",
        "Rome, Italy",
        "Barcelona, Spain",
        "Cape Town, South Africa",
        "Rio de Janeiro, Brazil",
        "Bangkok, Thailand",
        "Vancouver, Canada"
    };

    // Generate random index and return selected destination
    // Uses System.Random for simple random selection
    var random = new Random();
    int index = random.Next(destinations.Count);
    return destinations[index];
}

// Extract configuration from environment variables
// Retrieve the GitHub Models API endpoint, defaults to https://models.github.ai/inference if not specified
// Retrieve the model ID, defaults to openai/gpt-5-mini if not specified
// Retrieve the GitHub token for authentication, throws exception if not specified
var github_endpoint = Environment.GetEnvironmentVariable("GH_ENDPOINT") ?? "https://models.github.ai/inference";
var github_model_id = Environment.GetEnvironmentVariable("GH_MODEL_ID") ?? "openai/gpt-5-mini";
var github_token = Environment.GetEnvironmentVariable("GH_TOKEN") ?? throw new InvalidOperationException("GH_TOKEN is not set.");

// Configure OpenAI Client Options
// Create configuration options to point to GitHub Models endpoint
// This redirects OpenAI client calls to GitHub's model inference service
var openAIOptions = new OpenAIClientOptions()
{
    Endpoint = new Uri(github_endpoint)
};

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

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

// Execute Agent: Plan a Day Trip
// Run the agent with streaming enabled for real-time response display
// Shows the agent's thinking and response as it generates the content
// Provides better user experience with immediate feedback
await foreach (var update in agent.RunStreamingAsync("Plan me a day trip"))
{
    await Task.Delay(10);
    Console.Write(update);
}

πŸŽ“ Key Takeaways

  1. Agent Architecture: The Microsoft Agent Framework provides a clean, type-safe approach to building AI agents in .NET
  2. Tool Integration: Functions decorated with [Description] attributes become available tools for the agent
  3. Configuration Management: Environment variables and secure credential handling follow .NET best practices
  4. OpenAI Compatibility: GitHub Models integration works seamlessly through OpenAI-compatible APIs

πŸ”— Additional Resources


Disclaimer: αž―αž€αžŸαžΆαžšαž“αŸαŸ‡αžαŸ’αžšαžΌαžœαž”αžΆαž“αž”αž€αž”αŸ’αžšαŸ‚αžŠαŸ„αž™αž”αŸ’αžšαžΎαžŸαŸαžœαžΆαž€αž˜αŸ’αž˜αž”αž€αž”αŸ’αžšαŸ‚αžŠαŸ„αž™αž”αž‰αŸ’αž‰αžΆαžŸαž·αž”αŸ’αž”αž“αž·αž˜αŸ’αž˜αž·αž Co-op TranslatorαŸ” αž‘αŸ„αŸ‡αž™αžΎαž„αžαž·αžαžαŸ†αžŸαž˜αŸ’αžšαžΆαž”αŸ‹αž—αžΆαž–αžαŸ’αžšαžΉαž˜αžαŸ’αžšαžΌαžœ αžŸαžΌαž˜αž™αž€αž…αž·αžαŸ’αžαž‘αž»αž€αžŠαžΆαž€αŸ‹αžαžΆ αž€αžΆαžšαž”αž€αž”αŸ’αžšαŸ‚αžŠαŸ„αž™αžŸαŸ’αžœαŸαž™αž”αŸ’αžšαžœαžαŸ’αžαž·αž’αžΆαž…αž˜αžΆαž“αž€αŸ†αž αž»αžŸαž¬αž˜αž·αž“αžαŸ’αžšαžΉαž˜αžαŸ’αžšαžΌαžœαŸ” αž―αž€αžŸαžΆαžšαžŠαžΎαž˜αž“αŸ…αž€αŸ’αž“αž»αž„αž—αžΆαžŸαžΆαž˜αžΌαž›αžŠαŸ’αž‹αžΆαž“αžšαž”αžŸαŸ‹αžœαžΆαž‚αž½αžšαžαŸ’αžšαžΌαžœαž”αžΆαž“αž…αžΆαžαŸ‹αž‘αž»αž€αžαžΆαž‡αžΆαž”αŸ’αžšαž—αž–αž•αŸ’αž›αžΌαžœαž€αžΆαžšαžŠαŸ‚αž›αž˜αžΆαž“αžŸαž»αž–αž›αž—αžΆαž–αŸ” αžŸαž˜αŸ’αžšαžΆαž”αŸ‹αž–αŸαžαŸŒαž˜αžΆαž“αžŸαŸ†αžαžΆαž“αŸ‹αŸ— αž™αžΎαž„αž•αŸ’αžαž›αŸ‹αž’αž“αž»αžŸαžΆαžŸαž“αŸαž²αŸ’αž™αž”αŸ’αžšαžΎαž€αžΆαžšαž”αž€αž”αŸ’αžšαŸ‚αžŠαŸ„αž™αž’αŸ’αž“αž€αž”αž€αž”αŸ’αžšαŸ‚αžœαž·αž‡αŸ’αž‡αžΆαž‡αžΈαžœαŸˆαŸ” αž™αžΎαž„αž˜αž·αž“αž‘αž‘αž½αž›αžαž»αžŸαžαŸ’αžšαžΌαžœαž…αŸ†αž–αŸ„αŸ‡αž€αžΆαžšαž™αž›αŸ‹αž…αŸ’αžšαž‘αŸ† αž¬αž€αžΆαžšαž”αž€αžŸαŸαž…αž€αŸ’αžαžΈαžαž»αžŸαžŽαžΆαž˜αž½αž™ αžŠαŸ‚αž›αž€αžΎαžαž˜αžΆαž“αž–αžΈαž€αžΆαžšαž”αŸ’αžšαžΎαž”αŸ’αžšαžΆαžŸαŸ‹αž€αžΆαžšαž”αž€αž”αŸ’αžšαŸ‚αž“αŸαŸ‡αž‘αžΎαž™αŸ”