Skip to content

Commit 2ac973b

Browse files
authored
[AI Agents] Add Fabric Sample (Azure#50637)
* Add convenience method for FabricDataAgentToolParameters * Add Fabric sample
1 parent b579d07 commit 2ac973b

File tree

5 files changed

+209
-0
lines changed

5 files changed

+209
-0
lines changed

sdk/ai/Azure.AI.Agents.Persistent/api/Azure.AI.Agents.Persistent.net8.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ protected override void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter write
279279
public partial class FabricDataAgentToolParameters : System.ClientModel.Primitives.IJsonModel<Azure.AI.Agents.Persistent.FabricDataAgentToolParameters>, System.ClientModel.Primitives.IPersistableModel<Azure.AI.Agents.Persistent.FabricDataAgentToolParameters>
280280
{
281281
public FabricDataAgentToolParameters() { }
282+
public FabricDataAgentToolParameters(string connectionId) { }
282283
public System.Collections.Generic.IList<Azure.AI.Agents.Persistent.ToolConnection> ConnectionList { get { throw null; } }
283284
protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
284285
Azure.AI.Agents.Persistent.FabricDataAgentToolParameters System.ClientModel.Primitives.IJsonModel<Azure.AI.Agents.Persistent.FabricDataAgentToolParameters>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }

sdk/ai/Azure.AI.Agents.Persistent/api/Azure.AI.Agents.Persistent.netstandard2.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ protected override void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter write
279279
public partial class FabricDataAgentToolParameters : System.ClientModel.Primitives.IJsonModel<Azure.AI.Agents.Persistent.FabricDataAgentToolParameters>, System.ClientModel.Primitives.IPersistableModel<Azure.AI.Agents.Persistent.FabricDataAgentToolParameters>
280280
{
281281
public FabricDataAgentToolParameters() { }
282+
public FabricDataAgentToolParameters(string connectionId) { }
282283
public System.Collections.Generic.IList<Azure.AI.Agents.Persistent.ToolConnection> ConnectionList { get { throw null; } }
283284
protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
284285
Azure.AI.Agents.Persistent.FabricDataAgentToolParameters System.ClientModel.Primitives.IJsonModel<Azure.AI.Agents.Persistent.FabricDataAgentToolParameters>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
6+
namespace Azure.AI.Agents.Persistent
7+
{
8+
public partial class FabricDataAgentToolParameters
9+
{
10+
public FabricDataAgentToolParameters( string connectionId )
11+
{
12+
// Additional initialization logic if needed
13+
var toolConnection = new ToolConnection
14+
{
15+
ConnectionId = connectionId,
16+
};
17+
18+
this.ConnectionList = new List<ToolConnection> { toolConnection };
19+
}
20+
}
21+
}

sdk/ai/Azure.AI.Agents.Persistent/tests/AIAgentsTestEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public class AIAgentsTestEnvironment : TestEnvironment
1818
public string AI_SEARCH_CONNECTION_ID => GetRecordedVariable("AZURE_AI_CONNECTION_ID");
1919
public string BING_CONECTION_ID => GetRecordedVariable("AZURE_BING_CONECTION_ID");
2020
public string SHAREPOINT_CONNECTION_ID => GetRecordedVariable("AZURE_SHAREPOINT_CONNECTION_ID");
21+
public string FABRIC_CONNECTION_ID => GetRecordedVariable("AZURE_FABRIC_CONNECTION_ID");
2122
}
2223
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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

Comments
 (0)