mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
3ec881509c
* Delete sync extension methods for agent * Fix comments and obsolete attribute * Remove more sync methods. * Fix naming and comments. * Fix unit tests
53 lines
2.4 KiB
C#
53 lines
2.4 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.
|
|
AIProjectClient aiProjectClient = new(new Uri(endpoint), new AzureCliCredential());
|
|
|
|
// 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.
|
|
AgentThread thread = await agent.GetNewThreadAsync();
|
|
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", thread));
|
|
|
|
// Invoke the agent with streaming support.
|
|
thread = await agent.GetNewThreadAsync();
|
|
await foreach (AgentResponseUpdate update in agent.RunStreamingAsync("Tell me a joke about a pirate.", thread))
|
|
{
|
|
Console.WriteLine(update);
|
|
}
|
|
|
|
// Cleanup by agent name removes the agent version created.
|
|
await aiProjectClient.Agents.DeleteAgentAsync(agent.Name);
|