Skip to content

Commit b9e3478

Browse files
authored
Update code-interpreter-samples.md
Switching to PersistentThreadMessage
1 parent aa412e1 commit b9e3478

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
@@ -145,98 +145,98 @@ This ensures proper resource management and prevents unnecessary resource consum
145145
First, set up the configuration using `appsettings.json`, create a `PersistentAgentsClient`, and then create a `PersistentAgent` with the Code Interpreter tool enabled.
146146

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

172172
## Create a thread and add a message
173173

174174
Next, create a `PersistentAgentThread` for the conversation and add the initial user message.
175175

176176
```csharp
177-
PersistentAgentThread thread = client.Threads.CreateThread();
178-
179-
client.Messages.CreateMessage(
180-
thread.Id,
181-
MessageRole.User,
182-
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
177+
PersistentAgentThread thread = client.Threads.CreateThread();
178+
179+
client.Messages.CreateMessage(
180+
thread.Id,
181+
MessageRole.User,
182+
"Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
183183
```
184184

185185
## Create and monitor a run
186186

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

189189
```csharp
190-
ThreadRun run = client.Runs.CreateRun(
191-
thread.Id,
192-
agent.Id,
193-
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
194-
195-
do
196-
{
197-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
198-
run = client.Runs.GetRun(thread.Id, run.Id);
199-
}
200-
while (run.Status == RunStatus.Queued
201-
|| run.Status == RunStatus.InProgress
202-
|| run.Status == RunStatus.RequiresAction);
190+
ThreadRun run = client.Runs.CreateRun(
191+
thread.Id,
192+
agent.Id,
193+
additionalInstructions: "Please address the user as Jane Doe. The user has a premium account.");
194+
195+
do
196+
{
197+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
198+
run = client.Runs.GetRun(thread.Id, run.Id);
199+
}
200+
while (run.Status == RunStatus.Queued
201+
|| run.Status == RunStatus.InProgress
202+
|| run.Status == RunStatus.RequiresAction);
203203
```
204204

205205
## Process the results and handle files
206206

207207
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.
208208

209209
```csharp
210-
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
211-
threadId: thread.Id,
212-
order: ListSortOrder.Ascending);
213-
214-
foreach (ThreadMessage threadMessage in messages)
210+
Pageable<PersistentThreadMessage> messages = client.Messages.GetMessages(
211+
threadId: thread.Id,
212+
order: ListSortOrder.Ascending);
213+
214+
foreach (PersistentThreadMessage threadMessage in messages)
215+
{
216+
foreach (MessageContent content in threadMessage.ContentItems)
215217
{
216-
foreach (MessageContent content in threadMessage.ContentItems)
218+
switch (content)
217219
{
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-
}
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;
238237
}
239238
}
239+
}
240240
```
241241

242242
## Clean up resources

0 commit comments

Comments
 (0)