mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
56 lines
2.7 KiB
C#
56 lines
2.7 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample shows how to create and use a simple AI agent with Azure Foundry Agents as the backend that logs telemetry using OpenTelemetry.
|
|
|
|
using Azure.AI.Projects;
|
|
using Azure.Identity;
|
|
using Azure.Monitor.OpenTelemetry.Exporter;
|
|
using Microsoft.Agents.AI;
|
|
using OpenTelemetry;
|
|
using OpenTelemetry.Trace;
|
|
|
|
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? applicationInsightsConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING");
|
|
|
|
const string JokerInstructions = "You are good at telling jokes.";
|
|
const string JokerName = "JokerAgent";
|
|
|
|
// Create TracerProvider with console exporter
|
|
// This will output the telemetry data to the console.
|
|
string sourceName = Guid.NewGuid().ToString("N");
|
|
TracerProviderBuilder tracerProviderBuilder = Sdk.CreateTracerProviderBuilder()
|
|
.AddSource(sourceName)
|
|
.AddConsoleExporter();
|
|
if (!string.IsNullOrWhiteSpace(applicationInsightsConnectionString))
|
|
{
|
|
tracerProviderBuilder.AddAzureMonitorTraceExporter(options => options.ConnectionString = applicationInsightsConnectionString);
|
|
}
|
|
using var tracerProvider = tracerProviderBuilder.Build();
|
|
|
|
// 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());
|
|
|
|
// Define the agent you want to create. (Prompt Agent in this case)
|
|
AIAgent agent = (await aiProjectClient.CreateAIAgentAsync(name: JokerName, model: deploymentName, instructions: JokerInstructions))
|
|
.AsBuilder()
|
|
.UseOpenTelemetry(sourceName: sourceName)
|
|
.Build();
|
|
|
|
// Invoke the agent and output the text result.
|
|
AgentSession session = await agent.CreateSessionAsync();
|
|
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", session));
|
|
|
|
// Invoke the agent with streaming support.
|
|
session = await agent.CreateSessionAsync();
|
|
await foreach (AgentResponseUpdate update in agent.RunStreamingAsync("Tell me a joke about a pirate.", session))
|
|
{
|
|
Console.WriteLine(update);
|
|
}
|
|
|
|
// Cleanup by agent name removes the agent version created.
|
|
await aiProjectClient.Agents.DeleteAgentAsync(agent.Name);
|