Skip to content

Commit f073202

Browse files
authored
Merge pull request #68 from petehauge/Update-Fabric-Sample
Updated Fabric sample (pulled from live docs instead of repo)
2 parents e7ab1ce + 281daa3 commit f073202

File tree

3 files changed

+207
-1
lines changed

3 files changed

+207
-1
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using Azure.AI.Agents.Persistent;
2+
using Azure.Identity;
3+
using System;
4+
using Microsoft.Extensions.Configuration;
5+
using System.Threading;
6+
using Azure;
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 = agentClient.Administration.CreateAgent(
35+
model: modelDeploymentName,
36+
name: "my-assistant",
37+
instructions: "You are a helpful assistant.",
38+
tools: [fabricTool]);
39+
40+
PersistentAgentThread thread = agentClient.Threads.CreateThread();
41+
42+
// Create message and run the agent
43+
ThreadMessage message = agentClient.Messages.CreateMessage(
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+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
53+
run = agentClient.Runs.GetRun(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+
Pageable<ThreadMessage> messages = agentClient.Messages.GetMessages(
66+
threadId: thread.Id,
67+
order: ListSortOrder.Ascending
68+
);
69+
70+
// Process messages in order
71+
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+
agentClient.Threads.DeleteThread(threadId: thread.Id);
103+
agentClient.Administration.DeleteAgent(agentId: agent.Id);

samples/microsoft/csharp/getting-started-agents/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This table tracks the current status of code samples for each supported tool in
2121
| | Using blob storage (project data assets)| ⚠️ Exists| Sample not yet tested - but compiles properly and follows updated coding patterns |
2222
| | Managing files | ❌ Doesn't exist| |
2323
| **Azure AI Search**| Using a knowledge store | ⚠️ Exists | Sample not yet tested - but compiles properly and follows updated coding patterns |
24-
| **Fabric** | Grounding with Fabric data | ❌ Doesn't exist| |
24+
| **Fabric** | Grounding with Fabric data | ⚠️ Exists| Sample not yet tested - but compiles properly and follows updated coding patterns |
2525
| **SharePoint** | Grounding with SharePoint files | ❌ Doesn't exist| |
2626
| **TripAdvisor** | Using licensed TripAdvisor data | ❌ Doesn't exist| |
2727
| **Function Calling**| Calling local functions | ✅ Fully functional and validated| |

0 commit comments

Comments
 (0)