|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#nullable disable |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Azure.Core.TestFramework; |
| 10 | +using NUnit.Framework; |
| 11 | + |
| 12 | +namespace Azure.AI.Agents.Persistent.Tests; |
| 13 | + |
| 14 | +public partial class Sample_PersistentAgents_Fabric : SamplesBase<AIAgentsTestEnvironment> |
| 15 | +{ |
| 16 | + [Test] |
| 17 | + [AsyncOnly] |
| 18 | + public async Task FabricExampleAsync() |
| 19 | + { |
| 20 | +#if SNIPPET |
| 21 | + var projectEndpoint = System.Environment.GetEnvironmentVariable("PROJECT_ENDPOINT"); |
| 22 | + var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME"); |
| 23 | + var connectionId = System.Environment.GetEnvironmentVariable("AZURE_FABRIC_CONNECTION_ID"); |
| 24 | +#else |
| 25 | + var projectEndpoint = TestEnvironment.PROJECT_ENDPOINT; |
| 26 | + var modelDeploymentName = TestEnvironment.MODELDEPLOYMENTNAME; |
| 27 | + var connectionId = TestEnvironment.FABRIC_CONNECTION_ID; |
| 28 | +#endif |
| 29 | + PersistentAgentsClient agentClient = new(projectEndpoint, new DefaultAzureCredential()); |
| 30 | + MicrosoftFabricToolDefinition fabricTool = new( |
| 31 | + new FabricDataAgentToolParameters( |
| 32 | + connectionId |
| 33 | + ) |
| 34 | + ); |
| 35 | + PersistentAgent agent = await agentClient.Administration.CreateAgentAsync( |
| 36 | + model: modelDeploymentName, |
| 37 | + name: "my-agent", |
| 38 | + instructions: "You are a helpful agent.", |
| 39 | + tools: [ fabricTool ]); |
| 40 | + |
| 41 | + // Create thread for communication |
| 42 | + PersistentAgentThread thread = await agentClient.Threads.CreateThreadAsync(); |
| 43 | + |
| 44 | + // Create message to thread |
| 45 | + PersistentThreadMessage message = await agentClient.Messages.CreateMessageAsync( |
| 46 | + thread.Id, |
| 47 | + MessageRole.User, |
| 48 | + "<User query against Fabric resource>"); |
| 49 | + |
| 50 | + // Run the agent |
| 51 | + ThreadRun run = await agentClient.Runs.CreateRunAsync(thread, agent); |
| 52 | + do |
| 53 | + { |
| 54 | + await Task.Delay(TimeSpan.FromMilliseconds(500)); |
| 55 | + run = await agentClient.Runs.GetRunAsync(thread.Id, run.Id); |
| 56 | + } |
| 57 | + while (run.Status == RunStatus.Queued |
| 58 | + || run.Status == RunStatus.InProgress); |
| 59 | + |
| 60 | + Assert.AreEqual( |
| 61 | + RunStatus.Completed, |
| 62 | + run.Status, |
| 63 | + run.LastError?.Message); |
| 64 | + |
| 65 | + AsyncPageable<PersistentThreadMessage> messages = agentClient.Messages.GetMessagesAsync( |
| 66 | + threadId: thread.Id, |
| 67 | + order: ListSortOrder.Ascending |
| 68 | + ); |
| 69 | + |
| 70 | + await foreach (PersistentThreadMessage threadMessage in messages) |
| 71 | + { |
| 72 | + Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: "); |
| 73 | + foreach (MessageContent contentItem in threadMessage.ContentItems) |
| 74 | + { |
| 75 | + if (contentItem is MessageTextContent textItem) |
| 76 | + { |
| 77 | + string response = textItem.Text; |
| 78 | + if (textItem.Annotations != null) |
| 79 | + { |
| 80 | + foreach (MessageTextAnnotation annotation in textItem.Annotations) |
| 81 | + { |
| 82 | + if (annotation is MessageTextUriCitationAnnotation uriAnnotation) |
| 83 | + { |
| 84 | + response = response.Replace(uriAnnotation.Text, $" [{uriAnnotation.UriCitation.Title}]({uriAnnotation.UriCitation.Uri})"); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + Console.Write($"Agent response: {response}"); |
| 89 | + } |
| 90 | + else if (contentItem is MessageImageFileContent imageFileItem) |
| 91 | + { |
| 92 | + Console.Write($"<image from ID: {imageFileItem.FileId}>"); |
| 93 | + } |
| 94 | + Console.WriteLine(); |
| 95 | + } |
| 96 | + } |
| 97 | + await agentClient.Threads.DeleteThreadAsync(threadId: thread.Id); |
| 98 | + await agentClient.Administration.DeleteAgentAsync(agentId: agent.Id); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + [SyncOnly] |
| 103 | + public void SharepointExample() |
| 104 | + { |
| 105 | +#if SNIPPET |
| 106 | + var projectEndpoint = System.Environment.GetEnvironmentVariable("PROJECT_ENDPOINT"); |
| 107 | + var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME"); |
| 108 | + var connectionId = System.Environment.GetEnvironmentVariable("AZURE_FABRIC_CONNECTION_ID"); |
| 109 | +#else |
| 110 | + var projectEndpoint = TestEnvironment.PROJECT_ENDPOINT; |
| 111 | + var modelDeploymentName = TestEnvironment.MODELDEPLOYMENTNAME; |
| 112 | + var connectionId = TestEnvironment.FABRIC_CONNECTION_ID; |
| 113 | +#endif |
| 114 | + PersistentAgentsClient agentClient = new(projectEndpoint, new DefaultAzureCredential()); |
| 115 | + MicrosoftFabricToolDefinition fabricTool = new( |
| 116 | + new FabricDataAgentToolParameters( |
| 117 | + connectionId |
| 118 | + ) |
| 119 | + ); |
| 120 | + PersistentAgent agent = agentClient.Administration.CreateAgent( |
| 121 | + model: modelDeploymentName, |
| 122 | + name: "my-agent", |
| 123 | + instructions: "You are a helpful agent.", |
| 124 | + tools: [fabricTool]); |
| 125 | + |
| 126 | + // Create thread for communication |
| 127 | + PersistentAgentThread thread = agentClient.Threads.CreateThread(); |
| 128 | + |
| 129 | + // Create message to thread |
| 130 | + PersistentThreadMessage message = agentClient.Messages.CreateMessage( |
| 131 | + thread.Id, |
| 132 | + MessageRole.User, |
| 133 | + "<User query against Fabric resource>"); |
| 134 | + |
| 135 | + // Run the agent |
| 136 | + ThreadRun run = agentClient.Runs.CreateRun(thread, agent); |
| 137 | + do |
| 138 | + { |
| 139 | + Thread.Sleep(TimeSpan.FromMilliseconds(500)); |
| 140 | + run = agentClient.Runs.GetRun(thread.Id, run.Id); |
| 141 | + } |
| 142 | + while (run.Status == RunStatus.Queued |
| 143 | + || run.Status == RunStatus.InProgress); |
| 144 | + |
| 145 | + Assert.AreEqual( |
| 146 | + RunStatus.Completed, |
| 147 | + run.Status, |
| 148 | + run.LastError?.Message); |
| 149 | + |
| 150 | + Pageable<PersistentThreadMessage> messages = agentClient.Messages.GetMessages( |
| 151 | + threadId: thread.Id, |
| 152 | + order: ListSortOrder.Ascending |
| 153 | + ); |
| 154 | + |
| 155 | + foreach (PersistentThreadMessage threadMessage in messages) |
| 156 | + { |
| 157 | + Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: "); |
| 158 | + foreach (MessageContent contentItem in threadMessage.ContentItems) |
| 159 | + { |
| 160 | + if (contentItem is MessageTextContent textItem) |
| 161 | + { |
| 162 | + string response = textItem.Text; |
| 163 | + if (textItem.Annotations != null) |
| 164 | + { |
| 165 | + foreach (MessageTextAnnotation annotation in textItem.Annotations) |
| 166 | + { |
| 167 | + if (annotation is MessageTextUriCitationAnnotation uriAnnotation) |
| 168 | + { |
| 169 | + response = response.Replace(uriAnnotation.Text, $" [{uriAnnotation.UriCitation.Title}]({uriAnnotation.UriCitation.Uri})"); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + Console.Write($"Agent response: {response}"); |
| 174 | + } |
| 175 | + else if (contentItem is MessageImageFileContent imageFileItem) |
| 176 | + { |
| 177 | + Console.Write($"<image from ID: {imageFileItem.FileId}>"); |
| 178 | + } |
| 179 | + Console.WriteLine(); |
| 180 | + } |
| 181 | + } |
| 182 | + agentClient.Threads.DeleteThread(threadId: thread.Id); |
| 183 | + agentClient.Administration.DeleteAgent(agentId: agent.Id); |
| 184 | + } |
| 185 | +} |
0 commit comments