@@ -283,95 +283,102 @@ console.log(`Deleted agent, agent ID: ${agent.id}`);
283
283
First, retrieve configuration details and create a ` PersistentAgentsClient ` , then define the ` OpenApiToolDefinition ` using the OpenAPI specification.
284
284
285
285
``` csharp
286
- var projectEndpoint = configuration [" ProjectEndpoint" ];
287
- var modelDeploymentName = configuration [" ModelDeploymentName" ];
288
- var openApiSpec = configuration [" OpenApiSpec" ];
289
- PersistentAgentsClient client = new (new Uri (projectEndpoint ), new DefaultAzureCredential ());
290
-
291
- // Helper function to get the absolute path to the OpenAPI spec file
292
- private static string GetFile ([CallerFilePath ] string pth = " " )
293
- {
294
- var dirName = Path .GetDirectoryName (pth ) ?? " " ;
295
- return Path .Combine (dirName , " weather_openapi.json" );
296
- }
297
-
298
- var spec = BinaryData .FromBytes (File .ReadAllBytes (openApiSpec ));
299
- // Using anonymous auth for this example
300
- OpenApiAnonymousAuthDetails openApiAnonAuth = new ();
301
- // Define the OpenAPI tool
302
- OpenApiToolDefinition openApiTool = new (
303
- name : " get_weather" ,
304
- description : " Retrieve weather information for a location" ,
305
- spec : spec ,
306
- auth : openApiAnonAuth ,
307
- defaultParams : [" format" ]
308
- );
286
+ using Azure ;
287
+ using Azure .AI .Agents .Persistent ;
288
+ using Azure .Identity ;
289
+ using Microsoft .Extensions .Configuration ;
290
+
291
+ IConfigurationRoot configuration = new ConfigurationBuilder ()
292
+ .SetBasePath (AppContext .BaseDirectory )
293
+ .AddJsonFile (" appsettings.json" , optional : false , reloadOnChange : true )
294
+ .Build ();
295
+
296
+ var projectEndpoint = configuration [" ProjectEndpoint" ];
297
+ var modelDeploymentName = configuration [" ModelDeploymentName" ];
298
+ var openApiSpec = configuration [" OpenApiSpec" ];
299
+ PersistentAgentsClient client = new (projectEndpoint , new DefaultAzureCredential ());
300
+
301
+ BinaryData spec = BinaryData .FromBytes (File .ReadAllBytes (openApiSpec ));
302
+
303
+ // Using anonymous auth for this example
304
+ OpenApiAnonymousAuthDetails openApiAnonAuth = new ();
305
+
306
+ // Define the OpenAPI tool
307
+ OpenApiToolDefinition openApiToolDef = new (
308
+ name : " get_weather" ,
309
+ description : " Retrieve weather information for a location" ,
310
+ spec : spec ,
311
+ openApiAuthentication : openApiAnonAuth ,
312
+ defaultParams : [" format" ]
313
+ );
309
314
```
310
315
311
316
## Create an agent
312
317
Next, create a ` PersistentAgent ` with the necessary model deployment, name, instructions, and the previously defined OpenAPI tool.
313
318
314
319
``` csharp
315
- PersistentAgent agent = client .CreateAgent (
316
- model : modelDeploymentName ,
317
- name : " Open API Tool Calling Agent" ,
318
- instructions : " You are a helpful agent." ,
319
- tools : [openApiTool ]
320
- );
320
+ PersistentAgent agent = client . Administration .CreateAgent (
321
+ model : modelDeploymentName ,
322
+ name : " Open API Tool Calling Agent" ,
323
+ instructions : " You are a helpful agent." ,
324
+ tools : [openApiToolDef ]
325
+ );
321
326
```
322
327
323
328
## Create thread, message, and run
324
329
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.
325
330
326
331
``` csharp
327
- PersistentAgentThread thread = client .CreateThread ();
328
- ThreadMessage message = client .CreateMessage (
329
- thread .Id ,
330
- MessageRole .User ,
331
- " What's the weather in Seattle?" );
332
-
333
- ThreadRun run = client .CreateRun (thread , agent );
334
-
335
- // Poll for the run's completion status
336
- do
337
- {
338
- Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
339
- run = client .GetRun (thread .Id , run .Id );
340
- }
341
- while (run .Status == RunStatus .Queued
342
- || run .Status == RunStatus .InProgress
343
- || run .Status == RunStatus .RequiresAction );
332
+ PersistentAgentThread thread = client .Threads .CreateThread ();
333
+
334
+ client .Messages .CreateMessage (
335
+ thread .Id ,
336
+ MessageRole .User ,
337
+ " What's the weather in Seattle?" );
338
+
339
+ ThreadRun run = client .Runs .CreateRun (
340
+ thread .Id ,
341
+ agent .Id );
342
+
343
+ // Poll for the run's completion status
344
+ do
345
+ {
346
+ Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
347
+ run = client .Runs .GetRun (thread .Id , run .Id );
348
+ }
349
+ while (run .Status == RunStatus .Queued
350
+ || run .Status == RunStatus .InProgress
351
+ || run .Status == RunStatus .RequiresAction );
344
352
```
345
353
346
354
## Display conversation messages
347
355
Retrieve and print all messages from the thread to the console in chronological order to display the conversation flow.
348
356
349
357
``` csharp
350
- Pageable < ThreadMessage > messages = client .Messages .GetMessages (
351
- threadId : thread .Id ,
352
- order : ListSortOrder .Ascending
353
- );
358
+ Pageable < PersistentThreadMessage > messages = client .Messages .GetMessages (
359
+ threadId : thread .Id ,
360
+ order : ListSortOrder .Ascending );
354
361
355
- foreach (ThreadMessage threadMessage in messages )
362
+ foreach (PersistentThreadMessage threadMessage in messages )
363
+ {
364
+ foreach (MessageContent content in threadMessage .ContentItems )
356
365
{
357
- foreach ( MessageContent content in threadMessage . ContentItems )
366
+ switch ( content )
358
367
{
359
- switch (content )
360
- {
361
- case MessageTextContent textItem :
362
- Console .WriteLine ($" [{threadMessage .Role }]: {textItem .Text }" );
363
- break ;
364
- }
368
+ case MessageTextContent textItem :
369
+ Console .WriteLine ($" [{threadMessage .Role }]: {textItem .Text }" );
370
+ break ;
365
371
}
366
372
}
373
+
367
374
```
368
375
369
376
## Clean up resources
370
377
Finally , delete the created `PersistentAgentThread ` and `PersistentAgent ` to clean up the resources used in this example .
371
378
372
379
```csharp
373
- client .DeleteThread (thread .Id );
374
- client .DeleteAgent (agent .Id );
380
+ client . Threads .DeleteThread (thread .Id );
381
+ client . Administration .DeleteAgent (agent .Id );
375
382
```
376
383
377
384
::: zone - end
0 commit comments