Files
2026-07-13 12:59:43 +08:00

79 lines
2.7 KiB
C#
Executable File

#!/usr/bin/dotnet run
#:package Microsoft.Extensions.AI@9.9.1
#:package Microsoft.Agents.AI.OpenAI@1.0.0-preview.251001.3
#:package Microsoft.Agents.AI@1.0.0-preview.251001.3
#:package Azure.AI.OpenAI@2.1.0
#:package Azure.Identity@1.13.1
#:package DotNetEnv@3.1.1
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;
using Azure.AI.OpenAI;
using Azure.Identity;
using DotNetEnv;
// Load environment variables from .env file
Env.Load("../../../.env");
// Azure OpenAI with the Responses API (stable v1 endpoint). Sign in with `az login`.
var azureEndpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deployment = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT") ?? "gpt-4o-mini";
var azureClient = new AzureOpenAIClient(new Uri(azureEndpoint), new AzureCliCredential());
// Define agent configuration
const string AGENT_NAME = "TravelPlanAgent";
const string AGENT_INSTRUCTIONS = @"You are an planner agent.
Your job is to decide which agents to run based on the user's request.
Below are the available agents specialised in different tasks:
- FlightBooking: For booking flights and providing flight information
- HotelBooking: For booking hotels and providing hotel information
- CarRental: For booking cars and providing car rental information
- ActivitiesBooking: For booking activities and providing activity information
- DestinationInfo: For providing information about destinations
- DefaultAgent: For handling general request";
// Configure agent with structured output
ChatClientAgentOptions agentOptions = new(name: AGENT_NAME, instructions: AGENT_INSTRUCTIONS)
{
ChatOptions = new()
{
ResponseFormat = ChatResponseFormatJson.ForJsonSchema(
schema: AIJsonUtilities.CreateJsonSchema(typeof(TravelPlan)),
schemaName: "TravelPlan",
schemaDescription: "Travel Plan with main_task and subtasks")
}
};
// Create AI agent
AIAgent agent = azureClient
.GetOpenAIResponseClient(deployment)
.CreateAIAgent(agentOptions);
// Execute planning request
Console.WriteLine(await agent.RunAsync("Create a travel plan for a family of 4, with 2 kids, from Singapore to Melbourne"));
// Define data models for structured output
public class Plan
{
[JsonPropertyName("assigned_agent")]
public string? Assigned_agent { get; set; }
[JsonPropertyName("task_details")]
public string? Task_details { get; set; }
}
public class TravelPlan
{
[JsonPropertyName("main_task")]
public string? Main_task { get; set; }
[JsonPropertyName("subtasks")]
public IList<Plan>? Subtasks { get; set; }
}