Skip to content

Commit 8c75a00

Browse files
Merge pull request #4820 from RobiladK/patch-5
Update code-interpreter-samples.md
2 parents f098560 + 8f11279 commit 8c75a00

File tree

1 file changed

+68
-163
lines changed

1 file changed

+68
-163
lines changed

articles/ai-services/agents/how-to/tools/code-interpreter-samples.md

Lines changed: 68 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -151,196 +151,101 @@ In this example we will demonstrate the Agent streaming support, code interprete
151151
1. First, we set up configuration using `appsettings.json`, create a `PersistentAgentsClient`, and then create a `PersistentAgent` with the Code Interpreter tool.
152152

153153
```csharp
154-
using Azure;
155-
using Azure.AI.Agents.Persistent;
156-
using Azure.Identity;
157-
using Microsoft.Extensions.Configuration;
158-
using System.Diagnostics;
159-
160-
IConfigurationRoot configuration = new ConfigurationBuilder()
161-
.SetBasePath(AppContext.BaseDirectory)
162-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
163-
.Build();
164-
165-
var projectEndpoint = configuration["ProjectEndpoint"];
166-
var modelDeploymentName = configuration["ModelDeploymentName"];
167-
168-
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
169-
```
170-
171-
Synchronous sample:
172-
173-
```csharp
174-
PersistentAgent agent = client.Administration.CreateAgent(
175-
model: modelDeploymentName,
176-
name: "My Friendly Test Agent",
177-
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
178-
tools: [new CodeInterpreterToolDefinition()]
179-
);
180-
```
181-
182-
Asynchronous sample:
183-
184-
```csharp
185-
PersistentAgent agent = await client.Administration.CreateAgentAsync(
186-
model: modelDeploymentName,
187-
name: "My Friendly Test Agent",
188-
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
189-
tools: [new CodeInterpreterToolDefinition()]
190-
);
154+
using Azure;
155+
using Azure.AI.Agents.Persistent;
156+
using Azure.Identity;
157+
using Microsoft.Extensions.Configuration;
158+
using System.Diagnostics;
159+
160+
IConfigurationRoot configuration = new ConfigurationBuilder()
161+
.SetBasePath(AppContext.BaseDirectory)
162+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
163+
.Build();
164+
165+
var projectEndpoint = configuration["ProjectEndpoint"];
166+
var modelDeploymentName = configuration["ModelDeploymentName"];
167+
168+
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
169+
170+
PersistentAgent agent = client.Administration.CreateAgent(
171+
model: modelDeploymentName,
172+
name: "My Friendly Test Agent",
173+
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
174+
tools: [new CodeInterpreterToolDefinition()]
175+
);
191176
```
192177

193178
2. Next, we create a `PersistentAgentThread` and add a user message to it.
194179

195-
Synchronous sample:
196-
197180
```csharp
198-
PersistentAgentThread thread = client.Threads.CreateThread();
199-
200-
client.Messages.CreateMessage(
201-
thread.Id,
202-
MessageRole.User,
203-
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
204-
```
205-
206-
Asynchronous sample:
207-
208-
```csharp
209-
PersistentAgentThread thread = await client.Threads.CreateThreadAsync();
210-
211-
await client.Messages.CreateMessageAsync(
212-
thread.Id,
213-
MessageRole.User,
214-
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
181+
PersistentAgentThread thread = client.Threads.CreateThread();
182+
183+
client.Messages.CreateMessage(
184+
thread.Id,
185+
MessageRole.User,
186+
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
215187
```
216188

217189
3. Then, we create a `ThreadRun` for the thread and agent, providing any additional instructions. We poll the run's status until it is no longer queued, in progress, or requires action.
218190

219-
Synchronous sample:
220-
221-
```csharp
222-
ThreadRun run = client.Runs.CreateRun(
223-
thread.Id,
224-
agent.Id,
225-
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
226-
227-
do
228-
{
229-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
230-
run = client.Runs.GetRun(thread.Id, run.Id);
231-
}
232-
while (run.Status == RunStatus.Queued
233-
|| run.Status == RunStatus.InProgress
234-
|| run.Status == RunStatus.RequiresAction);
235-
```
236-
237-
Asynchronous sample:
238-
239-
```csharp
240-
ThreadRun run = await client.Runs.CreateRunAsync(
241-
thread.Id,
242-
agent.Id,
243-
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
244-
245-
do
246-
{
247-
await Task.Delay(TimeSpan.FromMilliseconds(500));
248-
run = await client.Runs.GetRunAsync(thread.Id, run.Id);
249-
}
250-
while (run.Status == RunStatus.Queued
251-
|| run.Status == RunStatus.InProgress
252-
|| run.Status == RunStatus.RequiresAction);
253-
```
254-
255-
4. Once the run is finished, we retrieve all messages from the thread. We then iterate through the messages to display text content and handle any image files by saving them locally and opening them.
256-
257-
Synchronous sample:
258-
259191
```csharp
260-
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
261-
threadId: thread.Id,
262-
order: ListSortOrder.Ascending);
263-
264-
foreach (ThreadMessage threadMessage in messages)
265-
{
266-
foreach (MessageContent content in threadMessage.ContentItems)
192+
ThreadRun run = client.Runs.CreateRun(
193+
thread.Id,
194+
agent.Id,
195+
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
196+
197+
do
267198
{
268-
switch (content)
269-
{
270-
case MessageTextContent textItem:
271-
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
272-
break;
273-
case MessageImageFileContent imageFileContent:
274-
Console.WriteLine($"[{threadMessage.Role}]: Image content file ID = {imageFileContent.FileId}");
275-
BinaryData imageContent = client.Files.GetFileContent(imageFileContent.FileId);
276-
string tempFilePath = Path.Combine(AppContext.BaseDirectory, $"{Guid.NewGuid()}.png");
277-
File.WriteAllBytes(tempFilePath, imageContent.ToArray());
278-
client.Files.DeleteFile(imageFileContent.FileId);
279-
280-
ProcessStartInfo psi = new()
281-
{
282-
FileName = tempFilePath,
283-
UseShellExecute = true
284-
};
285-
Process.Start(psi);
286-
break;
287-
}
199+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
200+
run = client.Runs.GetRun(thread.Id, run.Id);
288201
}
289-
}
202+
while (run.Status == RunStatus.Queued
203+
|| run.Status == RunStatus.InProgress
204+
|| run.Status == RunStatus.RequiresAction);
290205
```
291206

292-
Asynchronous sample:
207+
4. Once the run is finished, we retrieve all messages from the thread. We then iterate through the messages to display text content and handle any image files by saving them locally and opening them.
293208

294209
```csharp
295-
AsyncPageable<ThreadMessage> messages = client.Messages.GetMessagesAsync(
296-
threadId: thread.Id,
297-
order: ListSortOrder.Ascending);
298-
299-
await foreach (ThreadMessage threadMessage in messages)
300-
{
301-
foreach (MessageContent content in threadMessage.ContentItems)
210+
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
211+
threadId: thread.Id,
212+
order: ListSortOrder.Ascending);
213+
214+
foreach (ThreadMessage threadMessage in messages)
302215
{
303-
switch (content)
216+
foreach (MessageContent content in threadMessage.ContentItems)
304217
{
305-
case MessageTextContent textItem:
306-
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
307-
break;
308-
case MessageImageFileContent imageFileContent:
309-
Console.WriteLine($"[{threadMessage.Role}]: Image content file ID = {imageFileContent.FileId}");
310-
BinaryData imageContent = await client.Files.GetFileContentAsync(imageFileContent.FileId);
311-
string tempFilePath = Path.Combine(AppContext.BaseDirectory, $"{Guid.NewGuid()}.png");
312-
File.WriteAllBytes(tempFilePath, imageContent.ToArray());
313-
await client.Files.DeleteFileAsync(imageFileContent.FileId);
314-
315-
ProcessStartInfo psi = new()
316-
{
317-
FileName = tempFilePath,
318-
UseShellExecute = true
319-
};
320-
Process.Start(psi);
321-
break;
218+
switch (content)
219+
{
220+
case MessageTextContent textItem:
221+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
222+
break;
223+
case MessageImageFileContent imageFileContent:
224+
Console.WriteLine($"[{threadMessage.Role}]: Image content file ID = {imageFileContent.FileId}");
225+
BinaryData imageContent = client.Files.GetFileContent(imageFileContent.FileId);
226+
string tempFilePath = Path.Combine(AppContext.BaseDirectory, $"{Guid.NewGuid()}.png");
227+
File.WriteAllBytes(tempFilePath, imageContent.ToArray());
228+
client.Files.DeleteFile(imageFileContent.FileId);
229+
230+
ProcessStartInfo psi = new()
231+
{
232+
FileName = tempFilePath,
233+
UseShellExecute = true
234+
};
235+
Process.Start(psi);
236+
break;
237+
}
322238
}
323239
}
324-
}
325240
```
326241

327242
5. Finally, we delete the thread and the agent to clean up the resources created in this sample.
328243

329-
Synchronous sample:
330-
331244
```csharp
332-
client.Threads.DeleteThread(threadId: thread.Id);
333-
client.Administration.DeleteAgent(agentId: agent.Id);
245+
client.Threads.DeleteThread(threadId: thread.Id);
246+
client.Administration.DeleteAgent(agentId: agent.Id);
334247
```
335248

336-
Asynchronous sample:
337-
338-
```csharp
339-
await client.Threads.DeleteThreadAsync(threadId: thread.Id);
340-
await client.Administration.DeleteAgentAsync(agentId: agent.Id);
341-
```
342-
343-
344249
:::zone-end
345250

346251
:::zone pivot="javascript"

0 commit comments

Comments
 (0)