Skip to content

Commit 9dd6de5

Browse files
authored
Update openapi-spec-samples.md
Removed sync and async headings. removed async examples and intended code blocks.
1 parent 6c58119 commit 9dd6de5

File tree

1 file changed

+49
-115
lines changed

1 file changed

+49
-115
lines changed

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

Lines changed: 49 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -51,147 +51,81 @@ In this example we will demonstrate the possibility to use services with [OpenAP
5151
1. First get `ProjectEndpoint` and `ModelDeploymentName` from config and create a `PersistentAgentsClient`. Also, create an `OpenApiAnonymousAuthDetails` and `OpenApiToolDefinition` from config.
5252

5353
```csharp
54-
var projectEndpoint = configuration["ProjectEndpoint"];
55-
var modelDeploymentName = configuration["ModelDeploymentName"];
56-
var openApiSpec = configuration["OpenApiSpec"];
57-
PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential());
58-
59-
var spec = BinaryData.FromBytes(File.ReadAllBytes(openApiSpec));
60-
OpenApiAnonymousAuthDetails openApiAnonAuth = new();
61-
OpenApiToolDefinition openApiTool = new(
62-
name: "get_weather",
63-
description: "Retrieve weather information for a location",
64-
spec: spec,
65-
auth: openApiAnonAuth,
66-
defaultParams: ["format"]
67-
);
54+
var projectEndpoint = configuration["ProjectEndpoint"];
55+
var modelDeploymentName = configuration["ModelDeploymentName"];
56+
var openApiSpec = configuration["OpenApiSpec"];
57+
PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential());
58+
59+
var spec = BinaryData.FromBytes(File.ReadAllBytes(openApiSpec));
60+
OpenApiAnonymousAuthDetails openApiAnonAuth = new();
61+
OpenApiToolDefinition openApiTool = new(
62+
name: "get_weather",
63+
description: "Retrieve weather information for a location",
64+
spec: spec,
65+
auth: openApiAnonAuth,
66+
defaultParams: ["format"]
67+
);
6868
```
6969

7070
2. Next we will need to create an agent.
7171

72-
Synchronous sample:
73-
74-
```csharp
75-
PersistentAgent agent = client.CreateAgent(
76-
model: modelDeploymentName,
77-
name: "Open API Tool Calling Agent",
78-
instructions: "You are a helpful agent.",
79-
tools: [openApiTool]
80-
);
81-
```
82-
83-
Asynchronous sample:
84-
8572
```csharp
86-
PersistentAgent agent = await client.CreateAgentAsync(
87-
model: modelDeploymentName,
88-
name: "Open API Tool Calling Agent",
89-
instructions: "You are a helpful agent.",
90-
tools: [openApiTool]
91-
);
73+
PersistentAgent agent = client.CreateAgent(
74+
model: modelDeploymentName,
75+
name: "Open API Tool Calling Agent",
76+
instructions: "You are a helpful agent.",
77+
tools: [openApiTool]
78+
);
9279
```
9380

9481
3. Now we will create a `ThreadRun` and wait until it is complete. If the run will not be successful, we will print the last error.
9582

96-
Synchronous sample:
97-
```csharp
98-
PersistentAgentThread thread = client.CreateThread();
99-
ThreadMessage message = client.CreateMessage(
100-
thread.Id,
101-
MessageRole.User,
102-
"What's the weather in Seattle?");
103-
104-
ThreadRun run = client.CreateRun(thread, agent);
105-
106-
do
107-
{
108-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
109-
run = client.GetRun(thread.Id, run.Id);
110-
}
111-
while (run.Status == RunStatus.Queued
112-
|| run.Status == RunStatus.InProgress
113-
|| run.Status == RunStatus.RequiresAction);
114-
```
115-
116-
Asynchronous sample:
117-
118-
```csharp
119-
PersistentAgentThread thread = await client.CreateThreadAsync();
120-
ThreadMessage message = await client.CreateMessageAsync(
121-
thread.Id,
122-
MessageRole.User,
123-
"What's the weather in Seattle?");
124-
125-
ThreadRun run = await client.CreateRunAsync(thread, agent);
126-
127-
do
128-
{
129-
await Task.Delay(TimeSpan.FromMilliseconds(500));
130-
run = await client.GetRunAsync(thread.Id, run.Id);
131-
}
132-
while (run.Status == RunStatus.Queued
133-
|| run.Status == RunStatus.InProgress
134-
|| run.Status == RunStatus.RequiresAction);
135-
```
136-
137-
4. Print the messages to the console in chronological order.
138-
139-
Synchronous sample:
14083

14184
```csharp
142-
PageableList<ThreadMessage> messages = client.GetMessages(
143-
threadId: thread.Id,
144-
order: ListSortOrder.Ascending
145-
);
146-
147-
foreach (ThreadMessage threadMessage in messages)
148-
{
149-
foreach (MessageContent contentItem in threadMessage.ContentItems)
85+
PersistentAgentThread thread = client.CreateThread();
86+
ThreadMessage message = client.CreateMessage(
87+
thread.Id,
88+
MessageRole.User,
89+
"What's the weather in Seattle?");
90+
91+
ThreadRun run = client.CreateRun(thread, agent);
92+
93+
do
15094
{
151-
if (contentItem is MessageTextContent textItem)
152-
{
153-
Console.Write($"{threadMessage.Role}: {textItem.Text}");
154-
}
155-
Console.WriteLine();
95+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
96+
run = client.GetRun(thread.Id, run.Id);
15697
}
157-
}
98+
while (run.Status == RunStatus.Queued
99+
|| run.Status == RunStatus.InProgress
100+
|| run.Status == RunStatus.RequiresAction);
158101
```
159102

160-
Asynchronous sample:
103+
4. Print the messages to the console in chronological order.
161104

162105
```csharp
163-
PageableList<ThreadMessage> messages = await client.GetMessagesAsync(
164-
threadId: thread.Id,
165-
order: ListSortOrder.Ascending
166-
);
167-
168-
foreach (ThreadMessage threadMessage in messages)
169-
{
170-
foreach (MessageContent contentItem in threadMessage.ContentItems)
106+
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
107+
threadId: thread.Id,
108+
order: ListSortOrder.Ascending);
109+
110+
foreach (ThreadMessage threadMessage in messages)
171111
{
172-
if (contentItem is MessageTextContent textItem)
112+
foreach (MessageContent content in threadMessage.ContentItems)
173113
{
174-
Console.Write($"{threadMessage.Role}: {textItem.Text}");
114+
switch (content)
115+
{
116+
case MessageTextContent textItem:
117+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
118+
break;
119+
}
175120
}
176-
Console.WriteLine();
177121
}
178-
}
179122
```
180123

181124
5. Finally, we delete all the resources, we have created in this sample.
182125

183-
Synchronous sample:
184-
185-
```csharp
186-
client.DeleteThread(thread.Id);
187-
client.DeleteAgent(agent.Id);
188-
```
189-
190-
Asynchronous sample:
191-
192126
```csharp
193-
await client.DeleteThreadAsync(thread.Id);
194-
await client.DeleteAgentAsync(agent.Id);
127+
client.DeleteThread(thread.Id);
128+
client.DeleteAgent(agent.Id);
195129
```
196130

197131
:::zone-end

0 commit comments

Comments
 (0)