|
| 1 | +using Azure.AI.Agents.Persistent; |
| 2 | +using Azure.Identity; |
| 3 | +using System; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using Azure; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +// Get Connection information from App Configuration |
| 9 | +IConfigurationRoot configuration = new ConfigurationBuilder() |
| 10 | + .SetBasePath(AppContext.BaseDirectory) |
| 11 | + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
| 12 | + .Build(); |
| 13 | + |
| 14 | +var projectEndpoint = configuration["ProjectEndpoint"]; |
| 15 | +var modelDeploymentName = configuration["ModelDeploymentName"]; |
| 16 | +var fabricConnectionId = configuration["FabricConnectionId"]; |
| 17 | + |
| 18 | +// Create the Agent Client |
| 19 | +PersistentAgentsClient agentClient = new( |
| 20 | + projectEndpoint, |
| 21 | + new DefaultAzureCredential(), |
| 22 | + new PersistentAgentsAdministrationClientOptions( |
| 23 | + PersistentAgentsAdministrationClientOptions.ServiceVersion.V2025_05_01 |
| 24 | + )); |
| 25 | + |
| 26 | +// Create the MicrosoftFabricToolDefinition object needed when creating the agent |
| 27 | +ToolConnectionList connectionList = new() |
| 28 | +{ |
| 29 | + ConnectionList = { new ToolConnection(fabricConnectionId) } |
| 30 | +}; |
| 31 | +MicrosoftFabricToolDefinition fabricTool = new(connectionList); |
| 32 | + |
| 33 | +// Create the Agent |
| 34 | +PersistentAgent agent = await agentClient.Administration.CreateAgentAsync( |
| 35 | + model: modelDeploymentName, |
| 36 | + name: "my-assistant", |
| 37 | + instructions: "You are a helpful assistant.", |
| 38 | + tools: [fabricTool]); |
| 39 | + |
| 40 | +PersistentAgentThread thread = await agentClient.Threads.CreateThreadAsync(); |
| 41 | + |
| 42 | +// Create message and run the agent |
| 43 | +ThreadMessage message = await agentClient.Messages.CreateMessageAsync( |
| 44 | + thread.Id, |
| 45 | + MessageRole.User, |
| 46 | + "What are the top 3 weather events with highest property damage?"); |
| 47 | +ThreadRun run = agentClient.Runs.CreateRun(thread, agent); |
| 48 | + |
| 49 | +// Wait for the agent to finish running |
| 50 | +do |
| 51 | +{ |
| 52 | + await Task.Delay(TimeSpan.FromMilliseconds(500)); |
| 53 | + run = await agentClient.Runs.GetRunAsync(thread.Id, run.Id); |
| 54 | +} |
| 55 | +while (run.Status == RunStatus.Queued |
| 56 | + || run.Status == RunStatus.InProgress); |
| 57 | + |
| 58 | +// Confirm that the run completed successfully |
| 59 | +if (run.Status != RunStatus.Completed) |
| 60 | +{ |
| 61 | + throw new Exception("Run did not complete successfully, error: " + run.LastError?.Message); |
| 62 | +} |
| 63 | + |
| 64 | +// Retrieve all messages from the agent client |
| 65 | +AsyncPageable<ThreadMessage> messages = agentClient.Messages.GetMessagesAsync( |
| 66 | + threadId: thread.Id, |
| 67 | + order: ListSortOrder.Ascending |
| 68 | +); |
| 69 | + |
| 70 | +// Process messages in order |
| 71 | +await foreach (ThreadMessage threadMessage in messages) |
| 72 | +{ |
| 73 | + Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: "); |
| 74 | + foreach (MessageContent contentItem in threadMessage.ContentItems) |
| 75 | + { |
| 76 | + if (contentItem is MessageTextContent textItem) |
| 77 | + { |
| 78 | + string response = textItem.Text; |
| 79 | + |
| 80 | + // If we have Text URL citation annotations, reformat the response to show title & URL for citations |
| 81 | + if (textItem.Annotations != null) |
| 82 | + { |
| 83 | + foreach (MessageTextAnnotation annotation in textItem.Annotations) |
| 84 | + { |
| 85 | + if (annotation is MessageTextUrlCitationAnnotation urlAnnotation) |
| 86 | + { |
| 87 | + response = response.Replace(urlAnnotation.Text, $" [{urlAnnotation.UrlCitation.Title}]({urlAnnotation.UrlCitation.Url})"); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + Console.Write($"Agent response: {response}"); |
| 92 | + } |
| 93 | + else if (contentItem is MessageImageFileContent imageFileItem) |
| 94 | + { |
| 95 | + Console.Write($"<image from ID: {imageFileItem.FileId}"); |
| 96 | + } |
| 97 | + Console.WriteLine(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// Delete thread and agent |
| 102 | +await agentClient.Threads.DeleteThreadAsync(threadId: thread.Id); |
| 103 | +await agentClient.Administration.DeleteAgentAsync(agentId: agent.Id); |
0 commit comments