@@ -181,95 +181,102 @@ After the interaction is complete, the script performs cleanup by deleting the c
181
181
First, retrieve configuration details and create a ` PersistentAgentsClient ` , then define the ` OpenApiToolDefinition ` using the OpenAPI specification.
182
182
183
183
``` csharp
184
- var projectEndpoint = configuration [" ProjectEndpoint" ];
185
- var modelDeploymentName = configuration [" ModelDeploymentName" ];
186
- var openApiSpec = configuration [" OpenApiSpec" ];
187
- PersistentAgentsClient client = new (new Uri (projectEndpoint ), new DefaultAzureCredential ());
188
-
189
- // Helper function to get the absolute path to the OpenAPI spec file
190
- private static string GetFile ([CallerFilePath ] string pth = " " )
191
- {
192
- var dirName = Path .GetDirectoryName (pth ) ?? " " ;
193
- return Path .Combine (dirName , " weather_openapi.json" );
194
- }
195
-
196
- var spec = BinaryData .FromBytes (File .ReadAllBytes (openApiSpec ));
197
- // Using anonymous auth for this example
198
- OpenApiAnonymousAuthDetails openApiAnonAuth = new ();
199
- // Define the OpenAPI tool
200
- OpenApiToolDefinition openApiTool = new (
201
- name : " get_weather" ,
202
- description : " Retrieve weather information for a location" ,
203
- spec : spec ,
204
- auth : openApiAnonAuth ,
205
- defaultParams : [" format" ]
206
- );
184
+ using Azure ;
185
+ using Azure .AI .Agents .Persistent ;
186
+ using Azure .Identity ;
187
+ using Microsoft .Extensions .Configuration ;
188
+
189
+ IConfigurationRoot configuration = new ConfigurationBuilder ()
190
+ .SetBasePath (AppContext .BaseDirectory )
191
+ .AddJsonFile (" appsettings.json" , optional : false , reloadOnChange : true )
192
+ .Build ();
193
+
194
+ var projectEndpoint = configuration [" ProjectEndpoint" ];
195
+ var modelDeploymentName = configuration [" ModelDeploymentName" ];
196
+ var openApiSpec = configuration [" OpenApiSpec" ];
197
+ PersistentAgentsClient client = new (projectEndpoint , new DefaultAzureCredential ());
198
+
199
+ BinaryData spec = BinaryData .FromBytes (File .ReadAllBytes (openApiSpec ));
200
+
201
+ // Using anonymous auth for this example
202
+ OpenApiAnonymousAuthDetails openApiAnonAuth = new ();
203
+
204
+ // Define the OpenAPI tool
205
+ OpenApiToolDefinition openApiToolDef = new (
206
+ name : " get_weather" ,
207
+ description : " Retrieve weather information for a location" ,
208
+ spec : spec ,
209
+ openApiAuthentication : openApiAnonAuth ,
210
+ defaultParams : [" format" ]
211
+ );
207
212
```
208
213
209
214
## Create an agent
210
215
Next, create a ` PersistentAgent ` with the necessary model deployment, name, instructions, and the previously defined OpenAPI tool.
211
216
212
217
``` csharp
213
- PersistentAgent agent = client .CreateAgent (
214
- model : modelDeploymentName ,
215
- name : " Open API Tool Calling Agent" ,
216
- instructions : " You are a helpful agent." ,
217
- tools : [openApiTool ]
218
- );
218
+ PersistentAgent agent = client . Administration .CreateAgent (
219
+ model : modelDeploymentName ,
220
+ name : " Open API Tool Calling Agent" ,
221
+ instructions : " You are a helpful agent." ,
222
+ tools : [openApiToolDef ]
223
+ );
219
224
```
220
225
221
226
## Create thread, message, and run
222
227
Create a ` PersistentAgentThread ` for the conversation, add a user message to it, and then create a ` ThreadRun ` to process the message, waiting for its completion.
223
228
224
229
``` csharp
225
- PersistentAgentThread thread = client .CreateThread ();
226
- ThreadMessage message = client .CreateMessage (
227
- thread .Id ,
228
- MessageRole .User ,
229
- " What's the weather in Seattle?" );
230
-
231
- ThreadRun run = client .CreateRun (thread , agent );
232
-
233
- // Poll for the run's completion status
234
- do
235
- {
236
- Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
237
- run = client .GetRun (thread .Id , run .Id );
238
- }
239
- while (run .Status == RunStatus .Queued
240
- || run .Status == RunStatus .InProgress
241
- || run .Status == RunStatus .RequiresAction );
230
+ PersistentAgentThread thread = client .Threads .CreateThread ();
231
+
232
+ client .Messages .CreateMessage (
233
+ thread .Id ,
234
+ MessageRole .User ,
235
+ " What's the weather in Seattle?" );
236
+
237
+ ThreadRun run = client .Runs .CreateRun (
238
+ thread .Id ,
239
+ agent .Id );
240
+
241
+ // Poll for the run's completion status
242
+ do
243
+ {
244
+ Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
245
+ run = client .Runs .GetRun (thread .Id , run .Id );
246
+ }
247
+ while (run .Status == RunStatus .Queued
248
+ || run .Status == RunStatus .InProgress
249
+ || run .Status == RunStatus .RequiresAction );
242
250
```
243
251
244
252
## Display conversation messages
245
253
Retrieve and print all messages from the thread to the console in chronological order to display the conversation flow.
246
254
247
255
``` csharp
248
- Pageable < ThreadMessage > messages = client .Messages .GetMessages (
249
- threadId : thread .Id ,
250
- order : ListSortOrder .Ascending
251
- );
256
+ Pageable < PersistentThreadMessage > messages = client .Messages .GetMessages (
257
+ threadId : thread .Id ,
258
+ order : ListSortOrder .Ascending );
252
259
253
- foreach (ThreadMessage threadMessage in messages )
260
+ foreach (PersistentThreadMessage threadMessage in messages )
261
+ {
262
+ foreach (MessageContent content in threadMessage .ContentItems )
254
263
{
255
- foreach ( MessageContent content in threadMessage . ContentItems )
264
+ switch ( content )
256
265
{
257
- switch (content )
258
- {
259
- case MessageTextContent textItem :
260
- Console .WriteLine ($" [{threadMessage .Role }]: {textItem .Text }" );
261
- break ;
262
- }
266
+ case MessageTextContent textItem :
267
+ Console .WriteLine ($" [{threadMessage .Role }]: {textItem .Text }" );
268
+ break ;
263
269
}
264
270
}
271
+
265
272
```
266
273
267
274
## Clean up resources
268
275
Finally , delete the created `PersistentAgentThread ` and `PersistentAgent ` to clean up the resources used in this example .
269
276
270
277
```csharp
271
- client .DeleteThread (thread .Id );
272
- client .DeleteAgent (agent .Id );
278
+ client . Threads .DeleteThread (thread .Id );
279
+ client . Administration .DeleteAgent (agent .Id );
273
280
```
274
281
275
282
::: zone - end
0 commit comments