@@ -15,13 +15,13 @@ var modelDeploymentName = configuration["ModelDeploymentName"];
1515var 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
2020Synchronous 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(
3030ToolResources 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
4136PersistentAgent 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
4945Asynchronous 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(
5955ToolResources 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
7061PersistentAgent 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
8071Synchronous sample:
8172``` C# Snippet:AgentsAzureAISearchExample_CreateRun_Sync
8273// Create thread for communication
8374PersistentAgentThread 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:
111102PersistentAgentThread 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)
138129Synchronous 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
185203Asynchronous 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
2322775 . Finally, we delete all the resources, we have created in this sample.
0 commit comments