ai-agents-for-beginners

🌍 Microsoft Agent Framework (.NET)을 ν™œμš©ν•œ AI μ—¬ν–‰ μ—μ΄μ „νŠΈ

πŸ“‹ μ‹œλ‚˜λ¦¬μ˜€ κ°œμš”

이 μ˜ˆμ œλŠ” Microsoft Agent Framework for .NET을 μ‚¬μš©ν•˜μ—¬ μ§€λŠ₯ν˜• μ—¬ν–‰ κ³„νš μ—μ΄μ „νŠΈλ₯Ό κ΅¬μΆ•ν•˜λŠ” 방법을 λ³΄μ—¬μ€λ‹ˆλ‹€. 이 μ—μ΄μ „νŠΈλŠ” μ „ μ„Έκ³„μ˜ 랜덀 λͺ©μ μ§€μ— λŒ€ν•΄ κ°œμΈν™”λœ 당일 μ—¬ν–‰ 일정을 μžλ™μœΌλ‘œ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.

μ£Όμš” κΈ°λŠ₯:

πŸ”§ 기술 μ•„ν‚€ν…μ²˜

핡심 기술

μ£Όμš” ꡬ성 μš”μ†Œ

  1. AIAgent: λŒ€ν™” 흐름을 κ΄€λ¦¬ν•˜λŠ” μ£Όμš” μ—μ΄μ „νŠΈ μ˜€μΌ€μŠ€νŠΈλ ˆμ΄ν„°
  2. μ»€μŠ€ν…€ 도ꡬ: μ—μ΄μ „νŠΈκ°€ μ‚¬μš©ν•  수 μžˆλŠ” GetRandomDestination() ν•¨μˆ˜
  3. μ±„νŒ… ν΄λΌμ΄μ–ΈνŠΈ: GitHub Models 기반 λŒ€ν™” μΈν„°νŽ˜μ΄μŠ€
  4. 슀트리밍 지원: μ‹€μ‹œκ°„ 응닡 생성 κΈ°λŠ₯

톡합 νŒ¨ν„΄

graph LR
    A[User Request] --> B[AI Agent]
    B --> C[GitHub Models API]
    B --> D[GetRandomDestination Tool]
    C --> E[Travel Itinerary]
    D --> E

πŸš€ μ‹œμž‘ν•˜κΈ°

사전 μ€€λΉ„ 사항

ν•„μˆ˜ ν™˜κ²½ λ³€μˆ˜

# zsh/bash
export GH_TOKEN=<your_github_token>
export GH_ENDPOINT=https://models.github.ai/inference
export GH_MODEL_ID=openai/gpt-5-mini
# PowerShell
$env:GH_TOKEN = "<your_github_token>"
$env:GH_ENDPOINT = "https://models.github.ai/inference"
$env:GH_MODEL_ID = "openai/gpt-5-mini"

μƒ˜ν”Œ μ½”λ“œ

μ½”λ“œ 예제λ₯Ό μ‹€ν–‰ν•˜λ €λ©΄,

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

λ˜λŠ” dotnet CLIλ₯Ό μ‚¬μš©ν•˜μ—¬:

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

전체 μ½”λ“œλŠ” 01-dotnet-agent-framework.csμ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

#!/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);
}

πŸŽ“ μ£Όμš” ν•™μŠ΅ λ‚΄μš©

  1. μ—μ΄μ „νŠΈ μ•„ν‚€ν…μ²˜: Microsoft Agent FrameworkλŠ” .NETμ—μ„œ AI μ—μ΄μ „νŠΈλ₯Ό κ΅¬μΆ•ν•˜κΈ° μœ„ν•œ κΉ”λ”ν•˜κ³  νƒ€μž… μ•ˆμ „ν•œ μ ‘κ·Ό 방식을 μ œκ³΅ν•©λ‹ˆλ‹€.
  2. 도ꡬ 톡합: [Description] μ†μ„±μœΌλ‘œ μž₯μ‹λœ ν•¨μˆ˜λŠ” μ—μ΄μ „νŠΈκ°€ μ‚¬μš©ν•  수 μžˆλŠ” λ„κ΅¬λ‘œ μ œκ³΅λ©λ‹ˆλ‹€.
  3. ꡬ성 관리: ν™˜κ²½ λ³€μˆ˜μ™€ λ³΄μ•ˆ 자격 증λͺ… μ²˜λ¦¬λŠ” .NET의 λͺ¨λ²” 사둀λ₯Ό λ”°λ¦…λ‹ˆλ‹€.
  4. OpenAI ν˜Έν™˜μ„±: GitHub Models 톡합은 OpenAI ν˜Έν™˜ APIλ₯Ό 톡해 μ›ν™œν•˜κ²Œ μž‘λ™ν•©λ‹ˆλ‹€.

πŸ”— μΆ”κ°€ 자료


λ©΄μ±… μ‘°ν•­:
이 λ¬Έμ„œλŠ” AI λ²ˆμ—­ μ„œλΉ„μŠ€ Co-op Translatorλ₯Ό μ‚¬μš©ν•˜μ—¬ λ²ˆμ—­λ˜μ—ˆμŠ΅λ‹ˆλ‹€. 정확성을 μœ„ν•΄ λ…Έλ ₯ν•˜κ³  μžˆμ§€λ§Œ, μžλ™ λ²ˆμ—­μ—λŠ” 였λ₯˜λ‚˜ 뢀정확성이 포함될 수 μžˆμŠ΅λ‹ˆλ‹€. 원본 λ¬Έμ„œλ₯Ό ν•΄λ‹Ή μ–Έμ–΄λ‘œ μž‘μ„±λœ μƒνƒœμ—μ„œ κΆŒμœ„ μžˆλŠ” 자료둜 κ°„μ£Όν•΄μ•Ό ν•©λ‹ˆλ‹€. μ€‘μš”ν•œ μ •λ³΄μ˜ 경우, 전문적인 인간 λ²ˆμ—­μ„ ꢌμž₯ν•©λ‹ˆλ‹€. 이 λ²ˆμ—­ μ‚¬μš©μœΌλ‘œ 인해 λ°œμƒν•˜λŠ” μ˜€ν•΄λ‚˜ 잘λͺ»λœ 해석에 λŒ€ν•΄ λ‹Ήμ‚¬λŠ” μ±…μž„μ„ μ§€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.