Skip to content

Instantly share code, notes, and snippets.

@lucamilan
Created May 27, 2026 07:58
Show Gist options
  • Select an option

  • Save lucamilan/5fb772b72f367a0b1215a18c5ba2da6b to your computer and use it in GitHub Desktop.

Select an option

Save lucamilan/5fb772b72f367a0b1215a18c5ba2da6b to your computer and use it in GitHub Desktop.
Agent loop con Microsoft Agent Framework + meteo reale (Open-Meteo)
// =============================================================================
// agent.cs — Agent loop con Microsoft Agent Framework + meteo fake
// -----------------------------------------------------------------------------
// AVVIO (PowerShell):
//
// 1) Autenticazione Azure (usata da AzureCliCredential):
// az login --tenant "<tenant-id-o-dominio>"
//
// 2) Endpoint della risorsa Azure OpenAI (solo per la sessione corrente):
// $env:AZURE_OPENAI_ENDPOINT = "https://<nome-risorsa>.openai.azure.com/"
//
// 3) Esecuzione (l'argomento dopo -- è la domanda passata all'agente):
// dotnet run agent.cs -- "Che tempo fa a Piacenza?"
//
// Note: richiede .NET 10+ (app file-based). Il nome del deployment in
// GetChatClient("gpt-4o-mini") deve corrispondere a quello su Azure OpenAI.
// =============================================================================
#:package Microsoft.Agents.AI@1.5.0
#:package Microsoft.Extensions.AI.OpenAI@9.*-*
#:package Azure.AI.OpenAI@2.*
#:package Azure.Identity@1.*
using System.ComponentModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
string question = args.Length > 0 ? args[0] : "Che tempo fa a Piacenza?";
[Description("Restituisce il meteo corrente per una città")]
static string GetWeather([Description("Nome della città")] string city)
=> $"A {city} ci sono 22°C e cielo sereno";
AIAgent agent = new AzureOpenAIClient(
new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")!),
new AzureCliCredential())
.GetChatClient("gpt-4o-mini")
.AsIChatClient()
.AsAIAgent(
instructions: "Sei un assistente conciso.",
tools: [AIFunctionFactory.Create(GetWeather)]);
AgentResponse response = await agent.RunAsync(question);
Console.WriteLine(response.Text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment