mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
73 lines
3.2 KiB
C#
73 lines
3.2 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample shows how to use Microsoft Fabric Tool with AI Agents.
|
|
|
|
using Azure.AI.Projects;
|
|
using Azure.AI.Projects.OpenAI;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
using OpenAI.Responses;
|
|
|
|
string endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
|
|
string deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
|
|
string fabricConnectionId = Environment.GetEnvironmentVariable("FABRIC_PROJECT_CONNECTION_ID") ?? throw new InvalidOperationException("FABRIC_PROJECT_CONNECTION_ID is not set.");
|
|
|
|
const string AgentInstructions = "You are a helpful assistant with access to Microsoft Fabric data. Answer questions based on data available through your Fabric connection.";
|
|
|
|
// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents.
|
|
// WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production.
|
|
// In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid
|
|
// latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
|
|
AIProjectClient aiProjectClient = new(new Uri(endpoint), new DefaultAzureCredential());
|
|
|
|
// Configure Microsoft Fabric tool options with project connection
|
|
var fabricToolOptions = new FabricDataAgentToolOptions();
|
|
fabricToolOptions.ProjectConnections.Add(new ToolProjectConnection(fabricConnectionId));
|
|
|
|
AIAgent agent = await CreateAgentWithMEAIAsync();
|
|
// AIAgent agent = await CreateAgentWithNativeSDKAsync();
|
|
|
|
Console.WriteLine($"Created agent: {agent.Name}");
|
|
|
|
// Run the agent with a sample query
|
|
AgentResponse response = await agent.RunAsync("What data is available in the connected Fabric workspace?");
|
|
|
|
Console.WriteLine("\n=== Agent Response ===");
|
|
foreach (var message in response.Messages)
|
|
{
|
|
Console.WriteLine(message.Text);
|
|
}
|
|
|
|
// Cleanup by deleting the agent
|
|
await aiProjectClient.Agents.DeleteAgentAsync(agent.Name);
|
|
Console.WriteLine($"\nDeleted agent: {agent.Name}");
|
|
|
|
// --- Agent Creation Options ---
|
|
|
|
// Option 1 - Using AsAITool wrapping for the ResponseTool returned by AgentTool.CreateMicrosoftFabricTool (MEAI + AgentFramework)
|
|
async Task<AIAgent> CreateAgentWithMEAIAsync()
|
|
{
|
|
return await aiProjectClient.CreateAIAgentAsync(
|
|
model: deploymentName,
|
|
name: "FabricAgent-MEAI",
|
|
instructions: AgentInstructions,
|
|
tools: [((ResponseTool)AgentTool.CreateMicrosoftFabricTool(fabricToolOptions)).AsAITool()]);
|
|
}
|
|
|
|
// Option 2 - Using PromptAgentDefinition with AgentTool.CreateMicrosoftFabricTool (Native SDK)
|
|
async Task<AIAgent> CreateAgentWithNativeSDKAsync()
|
|
{
|
|
return await aiProjectClient.CreateAIAgentAsync(
|
|
name: "FabricAgent-NATIVE",
|
|
creationOptions: new AgentVersionCreationOptions(
|
|
new PromptAgentDefinition(model: deploymentName)
|
|
{
|
|
Instructions = AgentInstructions,
|
|
Tools =
|
|
{
|
|
AgentTool.CreateMicrosoftFabricTool(fabricToolOptions),
|
|
}
|
|
})
|
|
);
|
|
}
|