ΠΠ²Π°Ρ ΠΏΡΠΈΠΌΠ΅Ρ ΠΏΠΎΠΊΠ°Π·ΡΡΠ΅ ΠΊΠ°ΠΊΠΎ Π΄Π° Π½Π°ΠΏΡΠ°Π²ΠΈΡΠ΅ ΠΈΠ½ΡΠ΅Π»ΠΈΠ³Π΅Π½ΡΠ½ΠΎΠ³ Π°Π³Π΅Π½ΡΠ° Π·Π° ΠΏΠ»Π°Π½ΠΈΡΠ°ΡΠ΅ ΠΏΡΡΠΎΠ²Π°ΡΠ° ΠΊΠΎΡΠΈΡΡΠ΅ΡΠΈ Microsoft Agent Framework Π·Π° .NET. ΠΠ³Π΅Π½Π°Ρ ΠΌΠΎΠΆΠ΅ Π°ΡΡΠΎΠΌΠ°ΡΡΠΊΠΈ Π³Π΅Π½Π΅ΡΠΈΡΠ°ΡΠΈ ΠΏΠ΅ΡΡΠΎΠ½Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π΅ ΠΏΠ»Π°Π½ΠΎΠ²Π΅ ΡΠ΅Π΄Π½ΠΎΠ΄Π½Π΅Π²Π½ΠΈΡ ΠΈΠ·Π»Π΅ΡΠ° Π·Π° Π½Π°ΡΡΠΌΠΈΡΠ½Π΅ Π΄Π΅ΡΡΠΈΠ½Π°ΡΠΈΡΠ΅ ΡΠΈΡΠΎΠΌ ΡΠ²Π΅ΡΠ°.
GetRandomDestination() Π΄ΠΎΡΡΡΠΏΠ½Π° Π°Π³Π΅Π½ΡΡ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);
}
[Description] Π°ΡΡΠΈΠ±ΡΡΠΈΠΌΠ° ΠΏΠΎΡΡΠ°ΡΡ Π΄ΠΎΡΡΡΠΏΠ½ΠΈ Π°Π»Π°ΡΠΈ Π·Π° Π°Π³Π΅Π½ΡΠ°ΠΠ΄ΡΠΈΡΠ°ΡΠ΅ ΠΎΠ΄ ΠΎΠ΄Π³ΠΎΠ²ΠΎΡΠ½ΠΎΡΡΠΈ:
ΠΠ²Π°Ρ Π΄ΠΎΠΊΡΠΌΠ΅Π½Ρ ΡΠ΅ ΠΏΡΠ΅Π²Π΅Π΄Π΅Π½ ΠΊΠΎΡΠΈΡΡΠ΅ΡΠ΅ΠΌ ΡΡΠ»ΡΠ³Π΅ Π·Π° ΠΏΡΠ΅Π²ΠΎΡΠ΅ΡΠ΅ ΠΏΠΎΠΌΠΎΡΡ Π²Π΅ΡΡΠ°ΡΠΊΠ΅ ΠΈΠ½ΡΠ΅Π»ΠΈΠ³Π΅Π½ΡΠΈΡΠ΅ Co-op Translator. ΠΠ°ΠΊΠΎ ΡΠ΅ ΡΡΡΠ΄ΠΈΠΌΠΎ Π΄Π° ΠΎΠ±Π΅Π·Π±Π΅Π΄ΠΈΠΌΠΎ ΡΠ°ΡΠ½ΠΎΡΡ, ΠΌΠΎΠ»ΠΈΠΌΠΎ Π²Π°Ρ Π΄Π° ΠΈΠΌΠ°ΡΠ΅ Ρ Π²ΠΈΠ΄Ρ Π΄Π° Π°ΡΡΠΎΠΌΠ°ΡΡΠΊΠΈ ΠΏΡΠ΅Π²ΠΎΠ΄ΠΈ ΠΌΠΎΠ³Ρ ΡΠ°Π΄ΡΠΆΠ°ΡΠΈ Π³ΡΠ΅ΡΠΊΠ΅ ΠΈΠ»ΠΈ Π½Π΅ΡΠ°ΡΠ½ΠΎΡΡΠΈ. ΠΡΠΈΠ³ΠΈΠ½Π°Π»Π½ΠΈ Π΄ΠΎΠΊΡΠΌΠ΅Π½Ρ Π½Π° ΡΠ΅Π³ΠΎΠ²ΠΎΠΌ ΠΈΠ·Π²ΠΎΡΠ½ΠΎΠΌ ΡΠ΅Π·ΠΈΠΊΡ ΡΡΠ΅Π±Π° ΡΠΌΠ°ΡΡΠ°ΡΠΈ Π°ΡΡΠΎΡΠΈΡΠ°ΡΠΈΠ²Π½ΠΈΠΌ ΠΈΠ·Π²ΠΎΡΠΎΠΌ. ΠΠ° ΠΊΡΠΈΡΠΈΡΠ½Π΅ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡΠ΅ ΠΏΡΠ΅ΠΏΠΎΡΡΡΡΡΠ΅ ΡΠ΅ ΠΏΡΠΎΡΠ΅ΡΠΈΠΎΠ½Π°Π»Π½ΠΈ ΠΏΡΠ΅Π²ΠΎΠ΄ ΠΎΠ΄ ΡΡΡΠ°Π½Π΅ ΡΡΠ΄ΠΈ. ΠΠ΅ ΠΏΡΠ΅ΡΠ·ΠΈΠΌΠ°ΠΌΠΎ ΠΎΠ΄Π³ΠΎΠ²ΠΎΡΠ½ΠΎΡΡ Π·Π° Π±ΠΈΠ»ΠΎ ΠΊΠ°ΠΊΠ²Π° ΠΏΠΎΠ³ΡΠ΅ΡΠ½Π° ΡΡΠΌΠ°ΡΠ΅ΡΠ° ΠΈΠ»ΠΈ Π½Π΅ΡΠΏΠΎΡΠ°Π·ΡΠΌΠ΅ ΠΊΠΎΡΠΈ ΠΌΠΎΠ³Ρ Π½Π°ΡΡΠ°ΡΠΈ ΡΡΠ»Π΅Π΄ ΠΊΠΎΡΠΈΡΡΠ΅ΡΠ° ΠΎΠ²ΠΎΠ³ ΠΏΡΠ΅Π²ΠΎΠ΄Π°.