Skip to content

Commit d1b5fcb

Browse files
authored
Merge pull request #4925 from RobiladK/patch-12
Update openapi-spec-samples.md
2 parents 7fd1466 + 8f46b8e commit d1b5fcb

File tree

1 file changed

+67
-60
lines changed

1 file changed

+67
-60
lines changed

articles/ai-services/agents/how-to/tools/openapi-spec-samples.md

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -283,95 +283,102 @@ console.log(`Deleted agent, agent ID: ${agent.id}`);
283283
First, retrieve configuration details and create a `PersistentAgentsClient`, then define the `OpenApiToolDefinition` using the OpenAPI specification.
284284

285285
```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+
);
309314
```
310315

311316
## Create an agent
312317
Next, create a `PersistentAgent` with the necessary model deployment, name, instructions, and the previously defined OpenAPI tool.
313318

314319
```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+
);
321326
```
322327

323328
## Create thread, message, and run
324329
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.
325330

326331
```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);
344352
```
345353

346354
## Display conversation messages
347355
Retrieve and print all messages from the thread to the console in chronological order to display the conversation flow.
348356

349357
```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);
354361

355-
foreach (ThreadMessage threadMessage in messages)
362+
foreach (PersistentThreadMessage threadMessage in messages)
363+
{
364+
foreach (MessageContent content in threadMessage.ContentItems)
356365
{
357-
foreach (MessageContent content in threadMessage.ContentItems)
366+
switch (content)
358367
{
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;
365371
}
366372
}
373+
367374
```
368375

369376
## Clean up resources
370377
Finally, delete the created `PersistentAgentThread` and `PersistentAgent` to clean up the resources used in this example.
371378

372379
```csharp
373-
client.DeleteThread(thread.Id);
374-
client.DeleteAgent(agent.Id);
380+
client.Threads.DeleteThread(thread.Id);
381+
client.Administration.DeleteAgent(agent.Id);
375382
```
376383

377384
:::zone-end

0 commit comments

Comments
 (0)