Skip to content

Commit 8f46b8e

Browse files
authored
Update openapi-spec-samples.md
Switching to use PersistentThreadMessage openApiAuthentication on OpenApiToolDefinition. Some of the prior changes were reverted so added them back in with the comments from previous version.
1 parent aa412e1 commit 8f46b8e

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
@@ -181,95 +181,102 @@ After the interaction is complete, the script performs cleanup by deleting the c
181181
First, retrieve configuration details and create a `PersistentAgentsClient`, then define the `OpenApiToolDefinition` using the OpenAPI specification.
182182

183183
```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+
);
207212
```
208213

209214
## Create an agent
210215
Next, create a `PersistentAgent` with the necessary model deployment, name, instructions, and the previously defined OpenAPI tool.
211216

212217
```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+
);
219224
```
220225

221226
## Create thread, message, and run
222227
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.
223228

224229
```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);
242250
```
243251

244252
## Display conversation messages
245253
Retrieve and print all messages from the thread to the console in chronological order to display the conversation flow.
246254

247255
```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);
252259

253-
foreach (ThreadMessage threadMessage in messages)
260+
foreach (PersistentThreadMessage threadMessage in messages)
261+
{
262+
foreach (MessageContent content in threadMessage.ContentItems)
254263
{
255-
foreach (MessageContent content in threadMessage.ContentItems)
264+
switch (content)
256265
{
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;
263269
}
264270
}
271+
265272
```
266273

267274
## Clean up resources
268275
Finally, delete the created `PersistentAgentThread` and `PersistentAgent` to clean up the resources used in this example.
269276

270277
```csharp
271-
client.DeleteThread(thread.Id);
272-
client.DeleteAgent(agent.Id);
278+
client.Threads.DeleteThread(thread.Id);
279+
client.Administration.DeleteAgent(agent.Id);
273280
```
274281

275282
:::zone-end

0 commit comments

Comments
 (0)