Skip to content

Commit f89e20d

Browse files
authored
Merge pull request #120 from petehauge/May15-UpdateSamples
Update C# samples to use latest SDK
2 parents 759e05b + cf06a76 commit f89e20d

File tree

9 files changed

+321
-132
lines changed

9 files changed

+321
-132
lines changed

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

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ var modelDeploymentName = configuration["ModelDeploymentName"];
1515
var azureAiSearchConnectionId = configuration["AzureAiSearchConnectionId"];
1616
```
1717

18-
2. Create an agent with `AzureAISearchToolDefinition` and `ToolResources` with the only member `AzureAISearchResource` to be able to perform search. We will use `azureAiSearchConnectionId` to get the Azure AI Search resource.
18+
2. Create an agent with `AzureAISearchToolDefinition` and `ToolResources` with the only member `AzureAISearchToolResource` to be able to perform search. We will use `azureAiSearchConnectionId` to get the Azure AI Search resource.
1919

2020
Synchronous sample:
2121
```C# Snippet:AgentsCreateAgentWithAzureAISearchTool_Sync
22-
AzureAISearchResource searchResource = new(
22+
AzureAISearchToolResource searchResource = new(
2323
indexConnectionId: azureAiSearchConnectionId,
24-
indexName: "sample_index",
24+
indexName: "camping-index",
2525
topK: 5,
2626
filter: "category eq 'sleeping bag'",
2727
queryType: AzureAISearchQueryType.Simple
@@ -30,25 +30,21 @@ AzureAISearchResource searchResource = new(
3030
ToolResources toolResource = new() { AzureAISearch = searchResource };
3131

3232
// Create the Agent Client
33-
PersistentAgentsClient agentClient = new(
34-
projectEndpoint,
35-
new DefaultAzureCredential(),
36-
new PersistentAgentsAdministrationClientOptions(
37-
PersistentAgentsAdministrationClientOptions.ServiceVersion.V2025_05_01
38-
));
33+
PersistentAgentsClient agentClient = new(projectEndpoint, new DefaultAzureCredential());
3934

4035
// Create an agent with Tools and Tool Resources
4136
PersistentAgent agent = agentClient.Administration.CreateAgent(
4237
model: modelDeploymentName,
4338
name: "my-agent",
44-
instructions: "You are a helpful agent.",
39+
instructions: "Use the index provided to answer questions.",
4540
tools: [new AzureAISearchToolDefinition()],
46-
toolResources: toolResource);
41+
toolResources: toolResource
42+
);
4743
```
4844

4945
Asynchronous sample:
5046
```C# Snippet:AgentsCreateAgentWithAzureAISearchTool
51-
AzureAISearchResource searchResource = new(
47+
AzureAISearchToolResource searchResource = new(
5248
indexConnectionId: azureAiSearchConnectionId,
5349
indexName: "sample_index",
5450
topK: 5,
@@ -59,31 +55,26 @@ AzureAISearchResource searchResource = new(
5955
ToolResources toolResource = new() { AzureAISearch = searchResource };
6056

6157
// Create the Agent Client
62-
PersistentAgentsClient agentClient = new(
63-
projectEndpoint,
64-
new DefaultAzureCredential(),
65-
new PersistentAgentsAdministrationClientOptions(
66-
PersistentAgentsAdministrationClientOptions.ServiceVersion.V2025_05_01
67-
));
58+
PersistentAgentsClient agentClient = new(projectEndpoint,new DefaultAzureCredential());
6859

6960
// Create an agent with Tools and Tool Resources
7061
PersistentAgent agent = await agentClient.Administration.CreateAgentAsync(
7162
model: modelDeploymentName,
7263
name: "my-agent",
73-
instructions: "You are a helpful agent.",
64+
instructions: "Use the index provided to answer questions.",
7465
tools: [new AzureAISearchToolDefinition()],
7566
toolResources: toolResource);
7667
```
7768

78-
3. Now we will create a `Thread`, add a `Message` and `Run` to run the agent, then wait until the run completes. If the run will not be successful, we will print the last error.
69+
3. Now we will create a `PersistentAgentThread`, add a `PersistentThreadMessage` and `ThreadRun` to run the agent, then wait until the run completes. If the run will not be successful, we will print the last error.
7970

8071
Synchronous sample:
8172
```C# Snippet:AgentsAzureAISearchExample_CreateRun_Sync
8273
// Create thread for communication
8374
PersistentAgentThread thread = agentClient.Threads.CreateThread();
8475

8576
// Create message and run the agent
86-
ThreadMessage message = agentClient.Messages.CreateMessage(
77+
PersistentThreadMessage message = agentClient.Messages.CreateMessage(
8778
thread.Id,
8879
MessageRole.User,
8980
"What is the temperature rating of the cozynights sleeping bag?");
@@ -111,7 +102,7 @@ Asynchronous sample:
111102
PersistentAgentThread thread = await agentClient.Threads.CreateThreadAsync();
112103

113104
// Create message and run the agent
114-
ThreadMessage message = await agentClient.Messages.CreateMessageAsync(
105+
PersistentThreadMessage message = await agentClient.Messages.CreateMessageAsync(
115106
thread.Id,
116107
MessageRole.User,
117108
"What is the temperature rating of the cozynights sleeping bag?");
@@ -138,13 +129,13 @@ if (run.Status != RunStatus.Completed)
138129
Synchronous sample:
139130
```C# Snippet:AgentsPopulateReferencesAgentWithAzureAISearchTool_Sync
140131
// Retrieve the messages from the agent client
141-
Pageable<ThreadMessage> messages = agentClient.Messages.GetMessages(
132+
Pageable<PersistentThreadMessage> messages = agentClient.Messages.GetMessages(
142133
threadId: thread.Id,
143134
order: ListSortOrder.Ascending
144135
);
145136

146137
// Process messages in order
147-
foreach (ThreadMessage threadMessage in messages)
138+
foreach (PersistentThreadMessage threadMessage in messages)
148139
{
149140
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
150141
foreach (MessageContent contentItem in threadMessage.ContentItems)
@@ -159,11 +150,11 @@ foreach (ThreadMessage threadMessage in messages)
159150
// If we have Text URL citation annotations, reformat the response to show title & URL for citations
160151
foreach (MessageTextAnnotation annotation in textItem.Annotations)
161152
{
162-
if (annotation is MessageTextUrlCitationAnnotation urlAnnotation)
153+
if (annotation is MessageTextUriCitationAnnotation urlAnnotation)
163154
{
164155
annotatedText = annotatedText.Replace(
165156
urlAnnotation.Text,
166-
$" [see {urlAnnotation.UrlCitation.Title}] ({urlAnnotation.UrlCitation.Url})");
157+
$" [see {urlAnnotation.UriCitation.Title}] ({urlAnnotation.UriCitation.Uri})");
167158
}
168159
}
169160
Console.Write(annotatedText);
@@ -180,18 +171,45 @@ foreach (ThreadMessage threadMessage in messages)
180171
Console.WriteLine();
181172
}
182173
}
174+
175+
// Retrieve the run steps used by the agent and print those to the console
176+
Console.WriteLine("Run Steps used by Agent:");
177+
Pageable<RunStep> runSteps = agentClient.Runs.GetRunSteps(run);
178+
179+
foreach (var step in runSteps)
180+
{
181+
Console.WriteLine($"Step ID: {step.Id}, Total Tokens: {step.Usage.TotalTokens}, Status: {step.Status}, Type: {step.Type}");
182+
183+
if (step.StepDetails is RunStepMessageCreationDetails messageCreationDetails)
184+
{
185+
Console.WriteLine($" Message Creation Id: {messageCreationDetails.MessageCreation.MessageId}");
186+
}
187+
else if (step.StepDetails is RunStepToolCallDetails toolCallDetails)
188+
{
189+
// We know this agent only has the AI Search tool, so we can cast it directly
190+
foreach (RunStepAzureAISearchToolCall toolCall in toolCallDetails.ToolCalls)
191+
{
192+
Console.WriteLine($" Tool Call Details: {toolCall.GetType()}");
193+
194+
foreach (var result in toolCall.AzureAISearch)
195+
{
196+
Console.WriteLine($" {result.Key}: {result.Value}");
197+
}
198+
}
199+
}
200+
}
183201
```
184202

185203
Asynchronous sample:
186204
```C# Snippet:AgentsPopulateReferencesAgentWithAzureAISearchTool
187205
// Retrieve the messages from the agent client
188-
AsyncPageable<ThreadMessage> messages = agentClient.Messages.GetMessagesAsync(
206+
AsyncPageable<PersistentThreadMessage> messages = agentClient.Messages.GetMessagesAsync(
189207
threadId: thread.Id,
190208
order: ListSortOrder.Ascending
191209
);
192210

193211
// Process messages in order
194-
await foreach (ThreadMessage threadMessage in messages)
212+
await foreach (PersistentThreadMessage threadMessage in messages)
195213
{
196214
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
197215
foreach (MessageContent contentItem in threadMessage.ContentItems)
@@ -206,11 +224,11 @@ await foreach (ThreadMessage threadMessage in messages)
206224
// If we have Text URL citation annotations, reformat the response to show title & URL for citations
207225
foreach (MessageTextAnnotation annotation in textItem.Annotations)
208226
{
209-
if (annotation is MessageTextUrlCitationAnnotation urlAnnotation)
227+
if (annotation is MessageTextUriCitationAnnotation urlAnnotation)
210228
{
211229
annotatedText = annotatedText.Replace(
212230
urlAnnotation.Text,
213-
$" [see {urlAnnotation.UrlCitation.Title}] ({urlAnnotation.UrlCitation.Url})");
231+
$" [see {urlAnnotation.UriCitation.Title}] ({urlAnnotation.UriCitation.Uri})");
214232
}
215233
}
216234
Console.Write(annotatedText);
@@ -227,6 +245,33 @@ await foreach (ThreadMessage threadMessage in messages)
227245
Console.WriteLine();
228246
}
229247
}
248+
249+
// Retrieve the run steps used by the agent and print those to the console
250+
Console.WriteLine("Run Steps used by Agent:");
251+
AsyncPageable<RunStep> runSteps = agentClient.Runs.GetRunStepsAsync(run);
252+
253+
await foreach (var step in runSteps)
254+
{
255+
Console.WriteLine($"Step ID: {step.Id}, Total Tokens: {step.Usage.TotalTokens}, Status: {step.Status}, Type: {step.Type}");
256+
257+
if (step.StepDetails is RunStepMessageCreationDetails messageCreationDetails)
258+
{
259+
Console.WriteLine($" Message Creation Id: {messageCreationDetails.MessageCreation.MessageId}");
260+
}
261+
else if (step.StepDetails is RunStepToolCallDetails toolCallDetails)
262+
{
263+
// We know this agent only has the AI Search tool, so we can cast it directly
264+
foreach (RunStepAzureAISearchToolCall toolCall in toolCallDetails.ToolCalls)
265+
{
266+
Console.WriteLine($" Tool Call Details: {toolCall.GetType()}");
267+
268+
foreach (var result in toolCall.AzureAISearch)
269+
{
270+
Console.WriteLine($" {result.Key}: {result.Value}");
271+
}
272+
}
273+
}
274+
}
230275
```
231276

232277
5. Finally, we delete all the resources, we have created in this sample.

samples/microsoft/csharp/getting-started-agents/AzureAiSearch/AzureAiSearchAsync.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
var modelDeploymentName = configuration["ModelDeploymentName"];
1616
var azureAiSearchConnectionId = configuration["AzureAiSearchConnectionId"];
1717

18-
AzureAISearchResource searchResource = new(
18+
AzureAISearchToolResource searchResource = new(
1919
indexConnectionId: azureAiSearchConnectionId,
2020
indexName: "sample_index",
2121
topK: 5,
@@ -26,26 +26,21 @@
2626
ToolResources toolResource = new() { AzureAISearch = searchResource };
2727

2828
// Create the Agent Client
29-
PersistentAgentsClient agentClient = new(
30-
projectEndpoint,
31-
new DefaultAzureCredential(),
32-
new PersistentAgentsAdministrationClientOptions(
33-
PersistentAgentsAdministrationClientOptions.ServiceVersion.V2025_05_01
34-
));
29+
PersistentAgentsClient agentClient = new(projectEndpoint,new DefaultAzureCredential());
3530

3631
// Create an agent with Tools and Tool Resources
3732
PersistentAgent agent = await agentClient.Administration.CreateAgentAsync(
3833
model: modelDeploymentName,
3934
name: "my-agent",
40-
instructions: "You are a helpful agent.",
35+
instructions: "Use the index provided to answer questions.",
4136
tools: [new AzureAISearchToolDefinition()],
4237
toolResources: toolResource);
4338

4439
// Create thread for communication
4540
PersistentAgentThread thread = await agentClient.Threads.CreateThreadAsync();
4641

4742
// Create message and run the agent
48-
ThreadMessage message = await agentClient.Messages.CreateMessageAsync(
43+
PersistentThreadMessage message = await agentClient.Messages.CreateMessageAsync(
4944
thread.Id,
5045
MessageRole.User,
5146
"What is the temperature rating of the cozynights sleeping bag?");
@@ -67,13 +62,13 @@
6762
}
6863

6964
// Retrieve the messages from the agent client
70-
AsyncPageable<ThreadMessage> messages = agentClient.Messages.GetMessagesAsync(
65+
AsyncPageable<PersistentThreadMessage> messages = agentClient.Messages.GetMessagesAsync(
7166
threadId: thread.Id,
7267
order: ListSortOrder.Ascending
7368
);
7469

7570
// Process messages in order
76-
await foreach (ThreadMessage threadMessage in messages)
71+
await foreach (PersistentThreadMessage threadMessage in messages)
7772
{
7873
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
7974
foreach (MessageContent contentItem in threadMessage.ContentItems)
@@ -88,11 +83,11 @@
8883
// If we have Text URL citation annotations, reformat the response to show title & URL for citations
8984
foreach (MessageTextAnnotation annotation in textItem.Annotations)
9085
{
91-
if (annotation is MessageTextUrlCitationAnnotation urlAnnotation)
86+
if (annotation is MessageTextUriCitationAnnotation urlAnnotation)
9287
{
9388
annotatedText = annotatedText.Replace(
9489
urlAnnotation.Text,
95-
$" [see {urlAnnotation.UrlCitation.Title}] ({urlAnnotation.UrlCitation.Url})");
90+
$" [see {urlAnnotation.UriCitation.Title}] ({urlAnnotation.UriCitation.Uri})");
9691
}
9792
}
9893
Console.Write(annotatedText);
@@ -110,6 +105,33 @@
110105
}
111106
}
112107

108+
// Retrieve the run steps used by the agent and print those to the console
109+
Console.WriteLine("Run Steps used by Agent:");
110+
AsyncPageable<RunStep> runSteps = agentClient.Runs.GetRunStepsAsync(run);
111+
112+
await foreach (var step in runSteps)
113+
{
114+
Console.WriteLine($"Step ID: {step.Id}, Total Tokens: {step.Usage.TotalTokens}, Status: {step.Status}, Type: {step.Type}");
115+
116+
if (step.StepDetails is RunStepMessageCreationDetails messageCreationDetails)
117+
{
118+
Console.WriteLine($" Message Creation Id: {messageCreationDetails.MessageCreation.MessageId}");
119+
}
120+
else if (step.StepDetails is RunStepToolCallDetails toolCallDetails)
121+
{
122+
// We know this agent only has the AI Search tool, so we can cast it directly
123+
foreach (RunStepAzureAISearchToolCall toolCall in toolCallDetails.ToolCalls)
124+
{
125+
Console.WriteLine($" Tool Call Details: {toolCall.GetType()}");
126+
127+
foreach (var result in toolCall.AzureAISearch)
128+
{
129+
Console.WriteLine($" {result.Key}: {result.Value}");
130+
}
131+
}
132+
}
133+
}
134+
113135
// Clean up resources
114136
await agentClient.Threads.DeleteThreadAsync(thread.Id);
115137
await agentClient.Administration.DeleteAgentAsync(agent.Id);

0 commit comments

Comments
 (0)