Skip to content

Commit 6a815b4

Browse files
authored
Merge pull request #4924 from RobiladK/patch-11
Update code-interpreter-samples.md
2 parents d1b5fcb + b9e3478 commit 6a815b4

File tree

1 file changed

+67
-67
lines changed

1 file changed

+67
-67
lines changed

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

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -150,98 +150,98 @@ This ensures proper resource management and prevents unnecessary resource consum
150150
First, set up the configuration using `appsettings.json`, create a `PersistentAgentsClient`, and then create a `PersistentAgent` with the Code Interpreter tool enabled.
151151

152152
```csharp
153-
using Azure;
154-
using Azure.AI.Agents.Persistent;
155-
using Azure.Identity;
156-
using Microsoft.Extensions.Configuration;
157-
using System.Diagnostics;
158-
159-
IConfigurationRoot configuration = new ConfigurationBuilder()
160-
.SetBasePath(AppContext.BaseDirectory)
161-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
162-
.Build();
163-
164-
var projectEndpoint = configuration["ProjectEndpoint"];
165-
var modelDeploymentName = configuration["ModelDeploymentName"];
166-
167-
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
168-
169-
PersistentAgent agent = client.Administration.CreateAgent(
170-
model: modelDeploymentName,
171-
name: "My Friendly Test Agent",
172-
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
173-
tools: [new CodeInterpreterToolDefinition()]
174-
);
153+
using Azure;
154+
using Azure.AI.Agents.Persistent;
155+
using Azure.Identity;
156+
using Microsoft.Extensions.Configuration;
157+
using System.Diagnostics;
158+
159+
IConfigurationRoot configuration = new ConfigurationBuilder()
160+
.SetBasePath(AppContext.BaseDirectory)
161+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
162+
.Build();
163+
164+
var projectEndpoint = configuration["ProjectEndpoint"];
165+
var modelDeploymentName = configuration["ModelDeploymentName"];
166+
167+
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
168+
169+
PersistentAgent agent = client.Administration.CreateAgent(
170+
model: modelDeploymentName,
171+
name: "My Friendly Test Agent",
172+
instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
173+
tools: [new CodeInterpreterToolDefinition()]
174+
);
175175
```
176176

177177
## Create a thread and add a message
178178

179179
Next, create a `PersistentAgentThread` for the conversation and add the initial user message.
180180

181181
```csharp
182-
PersistentAgentThread thread = client.Threads.CreateThread();
183-
184-
client.Messages.CreateMessage(
185-
thread.Id,
186-
MessageRole.User,
187-
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
182+
PersistentAgentThread thread = client.Threads.CreateThread();
183+
184+
client.Messages.CreateMessage(
185+
thread.Id,
186+
MessageRole.User,
187+
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
188188
```
189189

190190
## Create and monitor a run
191191

192192
Then, create a `ThreadRun` for the thread and agent. Poll the run's status until it completes or requires action.
193193

194194
```csharp
195-
ThreadRun run = client.Runs.CreateRun(
196-
thread.Id,
197-
agent.Id,
198-
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
199-
200-
do
201-
{
202-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
203-
run = client.Runs.GetRun(thread.Id, run.Id);
204-
}
205-
while (run.Status == RunStatus.Queued
206-
|| run.Status == RunStatus.InProgress
207-
|| run.Status == RunStatus.RequiresAction);
195+
ThreadRun run = client.Runs.CreateRun(
196+
thread.Id,
197+
agent.Id,
198+
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
199+
200+
do
201+
{
202+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
203+
run = client.Runs.GetRun(thread.Id, run.Id);
204+
}
205+
while (run.Status == RunStatus.Queued
206+
|| run.Status == RunStatus.InProgress
207+
|| run.Status == RunStatus.RequiresAction);
208208
```
209209

210210
## Process the results and handle files
211211

212212
Once the run is finished, retrieve all messages from the thread. Iterate through the messages to display text content and handle any generated image files by saving them locally and opening them.
213213

214214
```csharp
215-
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
216-
threadId: thread.Id,
217-
order: ListSortOrder.Ascending);
218-
219-
foreach (ThreadMessage threadMessage in messages)
215+
Pageable<PersistentThreadMessage> messages = client.Messages.GetMessages(
216+
threadId: thread.Id,
217+
order: ListSortOrder.Ascending);
218+
219+
foreach (PersistentThreadMessage threadMessage in messages)
220+
{
221+
foreach (MessageContent content in threadMessage.ContentItems)
220222
{
221-
foreach (MessageContent content in threadMessage.ContentItems)
223+
switch (content)
222224
{
223-
switch (content)
224-
{
225-
case MessageTextContent textItem:
226-
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
227-
break;
228-
case MessageImageFileContent imageFileContent:
229-
Console.WriteLine($"[{threadMessage.Role}]: Image content file ID = {imageFileContent.FileId}");
230-
BinaryData imageContent = client.Files.GetFileContent(imageFileContent.FileId);
231-
string tempFilePath = Path.Combine(AppContext.BaseDirectory, $"{Guid.NewGuid()}.png");
232-
File.WriteAllBytes(tempFilePath, imageContent.ToArray());
233-
client.Files.DeleteFile(imageFileContent.FileId);
234-
235-
ProcessStartInfo psi = new()
236-
{
237-
FileName = tempFilePath,
238-
UseShellExecute = true
239-
};
240-
Process.Start(psi);
241-
break;
242-
}
225+
case MessageTextContent textItem:
226+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
227+
break;
228+
case MessageImageFileContent imageFileContent:
229+
Console.WriteLine($"[{threadMessage.Role}]: Image content file ID = {imageFileContent.FileId}");
230+
BinaryData imageContent = client.Files.GetFileContent(imageFileContent.FileId);
231+
string tempFilePath = Path.Combine(AppContext.BaseDirectory, $"{Guid.NewGuid()}.png");
232+
File.WriteAllBytes(tempFilePath, imageContent.ToArray());
233+
client.Files.DeleteFile(imageFileContent.FileId);
234+
235+
ProcessStartInfo psi = new()
236+
{
237+
FileName = tempFilePath,
238+
UseShellExecute = true
239+
};
240+
Process.Start(psi);
241+
break;
243242
}
244243
}
244+
}
245245
```
246246

247247
## Clean up resources

0 commit comments

Comments
 (0)