Skip to content

Commit 528d945

Browse files
author
Peter Hauge
committed
Revert "Updates for OpenAI sample"
This reverts commit 321c48d.
1 parent 321c48d commit 528d945

File tree

1 file changed

+61
-80
lines changed

1 file changed

+61
-80
lines changed

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

Lines changed: 61 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -181,114 +181,95 @@ 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-
using Azure;
185-
using Azure.AI.Agents.Persistent;
186-
using Azure.Identity;
187-
using Microsoft.Extensions.Configuration;
188-
using System;
189-
using System.IO;
190-
using System.Threading;
191-
192-
// Get Connection information from app configuration
193-
IConfigurationRoot configuration = new ConfigurationBuilder()
194-
.SetBasePath(AppContext.BaseDirectory)
195-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
196-
.Build();
197-
198-
var projectEndpoint = configuration["ProjectEndpoint"];
199-
var modelDeploymentName = configuration["ModelDeploymentName"];
200-
var openApiSpec = configuration["OpenApiSpec"];
201-
202-
PersistentAgentsClient agentClient = new(projectEndpoint, new DefaultAzureCredential());
203-
204-
// Read in the OpenAPI spec file
205-
var spec = BinaryData.FromBytes(File.ReadAllBytes(openApiSpec));
206-
207-
// Using anonymous auth for this example
208-
OpenApiAnonymousAuthDetails openApiAnonAuth = new();
209-
210-
// Define the OpenAPI tool
211-
OpenApiToolDefinition openApiTool = new(
212-
name: "get_weather",
213-
description: "Retrieve weather information for a location",
214-
spec: spec,
215-
openApiAuthentication: openApiAnonAuth,
216-
defaultParams: ["format"]
217-
);
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+
);
218207
```
219208

220209
## Create an agent
221210
Next, create a `PersistentAgent` with the necessary model deployment, name, instructions, and the previously defined OpenAPI tool.
222211

223212
```csharp
224-
PersistentAgent agent = agentClient.Administration.CreateAgent(
225-
model: modelDeploymentName,
226-
name: "Open API Tool Calling Agent",
227-
instructions: "You are a helpful agent.",
228-
tools: [openApiTool]
229-
);
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+
);
230219
```
231220

232221
## Create thread, message, and run
233222
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.
234223

235224
```csharp
236-
PersistentAgentThread thread = agentClient.Threads.CreateThread();
237-
PersistentThreadMessage message = agentClient.Messages.CreateMessage(
238-
thread.Id,
239-
MessageRole.User,
240-
"What's the weather in Seattle?"
241-
);
242-
243-
ThreadRun run = agentClient.Runs.CreateRun(thread, agent);
244-
245-
// Poll for the run's completion status
246-
do
247-
{
248-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
249-
run = agentClient.Runs.GetRun(thread.Id, run.Id);
250-
}
251-
while (run.Status == RunStatus.Queued
252-
|| run.Status == RunStatus.InProgress
253-
|| run.Status == RunStatus.RequiresAction);
254-
255-
// Confirm that the run completed successfully
256-
if (run.Status != RunStatus.Completed)
257-
{
258-
throw new Exception("Run did not complete successfully, error: " + run.LastError?.Message);
259-
}
260-
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);
261242
```
262243

263244
## Display conversation messages
264245
Retrieve and print all messages from the thread to the console in chronological order to display the conversation flow.
265246

266247
```csharp
267-
Pageable<PersistentThreadMessage> messages = agentClient.Messages.GetMessages(
268-
threadId: thread.Id,
269-
order: ListSortOrder.Ascending
270-
);
271-
272-
foreach (PersistentThreadMessage threadMessage in messages)
273-
{
274-
foreach (MessageContent content in threadMessage.ContentItems)
248+
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
249+
threadId: thread.Id,
250+
order: ListSortOrder.Ascending
251+
);
252+
253+
foreach (ThreadMessage threadMessage in messages)
275254
{
276-
switch (content)
255+
foreach (MessageContent content in threadMessage.ContentItems)
277256
{
278-
case MessageTextContent textItem:
279-
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
280-
break;
257+
switch (content)
258+
{
259+
case MessageTextContent textItem:
260+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
261+
break;
262+
}
281263
}
282264
}
283-
}
284265
```
285266

286267
## Clean up resources
287268
Finally, delete the created `PersistentAgentThread` and `PersistentAgent` to clean up the resources used in this example.
288269

289270
```csharp
290-
agentClient.Threads.DeleteThread(thread.Id);
291-
agentClient.Administration.DeleteAgent(agent.Id);
271+
client.DeleteThread(thread.Id);
272+
client.DeleteAgent(agent.Id);
292273
```
293274

294275
:::zone-end

0 commit comments

Comments
 (0)