Skip to content

Commit 7749f59

Browse files
authored
Merge branch 'main' into Updating-Local-File-Search
2 parents 69fd9e4 + a7ba758 commit 7749f59

File tree

64 files changed

+6152
-1691
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+6152
-1691
lines changed
Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,73 @@
11
# Sample for using additional messages while creating agent run in Azure.AI.Agents
22

3-
1. First get `ProjectEndpoint` and `ModelDeploymentName` from config and create a `PersistentAgentsClient`.
4-
5-
```C# Snippet:Sample_Agent_Multiple_Messages_Create
3+
1. Set up configuration, create an agent client, and create an agent. This step includes all necessary `using` directives.
4+
5+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CommonSetup
6+
using Azure;
7+
using Azure.AI.Agents.Persistent;
8+
using Azure.Identity;
9+
using Microsoft.Extensions.Configuration;
10+
11+
IConfigurationRoot configuration = new ConfigurationBuilder()
12+
.SetBasePath(AppContext.BaseDirectory)
13+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
14+
.Build();
615
var projectEndpoint = configuration["ProjectEndpoint"];
716
var modelDeploymentName = configuration["ModelDeploymentName"];
8-
PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential());
17+
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
918
```
1019

11-
2. Next we will need to create an agent.
12-
1320
Synchronous sample:
14-
```C# Snippet:Sample_Agent_Multiple_Messages_Create
15-
PersistentAgent agent = client.CreateAgent(
21+
22+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CreateAgentSync
23+
PersistentAgent agent = client.Administration.CreateAgent(
1624
model: modelDeploymentName,
1725
name: "Math Tutor",
1826
instructions: "You are a personal electronics tutor. Write and run code to answer questions.",
1927
tools: [new CodeInterpreterToolDefinition()]);
2028
```
2129

2230
Asynchronous sample:
23-
```C# Snippet:Sample_Agent_Multiple_Messages_CreateAsync
24-
PersistentAgent agent = await client.CreateAgentAsync(
31+
32+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CreateAgentAsync
33+
PersistentAgent agent = await client.Administration.CreateAgentAsync(
2534
model: modelDeploymentName,
2635
name: "Math Tutor",
2736
instructions: "You are a personal electronics tutor. Write and run code to answer questions.",
2837
tools: [new CodeInterpreterToolDefinition()]);
2938
```
3039

31-
3. Create the thread and run. In this example we are adding two extra messages to the thread, one with `Agent` and another with `User` role.
40+
2. Create the thread and add an initial message to it.
3241

3342
Synchronous sample:
34-
```C# Snippet:Sample_Agent_Multiple_Messages_Run
35-
PersistentAgentThread thread = client.CreateThread();
36-
client.CreateMessage(
43+
44+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CreateThreadAndMessageSync
45+
PersistentAgentThread thread = client.Threads.CreateThread();
46+
47+
client.Messages.CreateMessage(
48+
thread.Id,
49+
MessageRole.User,
50+
"What is the impedance formula?");
51+
```
52+
53+
Asynchronous sample:
54+
55+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CreateThreadAndMessageAsync
56+
PersistentAgentThread thread = await client.Threads.CreateThreadAsync();
57+
58+
await client.Messages.CreateMessageAsync(
3759
thread.Id,
3860
MessageRole.User,
3961
"What is the impedance formula?");
62+
```
63+
64+
3. Create the run with additional messages and poll for completion.
65+
In this example we add two extra messages to the thread when creating the run: one with the `MessageRole.Agent` role and another with the `MessageRole.User` role.
66+
67+
Synchronous sample:
4068

41-
ThreadRun agentRun = client.CreateRun(
69+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CreateAndPollRunSync
70+
ThreadRun run = client.Runs.CreateRun(
4271
threadId: thread.Id,
4372
agent.Id,
4473
additionalMessages: [
@@ -56,21 +85,17 @@ ThreadRun agentRun = client.CreateRun(
5685
do
5786
{
5887
Thread.Sleep(TimeSpan.FromMilliseconds(500));
59-
agentRun = client.GetRun(thread.Id, agentRun.Id);
88+
run = client.Runs.GetRun(thread.Id, run.Id);
6089
}
61-
while (agentRun.Status == RunStatus.Queued
62-
|| agentRun.Status == RunStatus.InProgress);
90+
while (run.Status == RunStatus.Queued
91+
|| run.Status == RunStatus.InProgress
92+
|| run.Status == RunStatus.RequiresAction);
6393
```
6494

6595
Asynchronous sample:
66-
```C# Snippet:Sample_Agent_Multiple_Messages_RunAsync
67-
PersistentAgentThread thread = await client.CreateThreadAsync();
68-
await client.CreateMessageAsync(
69-
thread.Id,
70-
MessageRole.User,
71-
"What is the impedance formula?");
7296

73-
ThreadRun agentRun = await client.CreateRunAsync(
97+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CreateAndPollRunAsync
98+
ThreadRun run = await client.Runs.CreateRunAsync(
7499
threadId: thread.Id,
75100
agent.Id,
76101
additionalMessages: [
@@ -88,58 +113,69 @@ ThreadRun agentRun = await client.CreateRunAsync(
88113
do
89114
{
90115
await Task.Delay(TimeSpan.FromMilliseconds(500));
91-
agentRun = await client.GetRunAsync(thread.Id, agentRun.Id);
116+
run = await client.Runs.GetRunAsync(thread.Id, run.Id);
92117
}
93-
while (agentRun.Status == RunStatus.Queued
94-
|| agentRun.Status == RunStatus.InProgress);
118+
while (run.Status == RunStatus.Queued
119+
|| run.Status == RunStatus.InProgress
120+
|| run.Status == RunStatus.RequiresAction);
95121
```
96122

97-
4. Next, we print out all the messages to the console.
123+
4. Print out all the messages to the console.
98124

99125
Synchronous sample:
100-
```C# Snippet:Sample_Agent_Multiple_Messages_Print
101-
PageableList<ThreadMessage> messages = client.GetMessages(thread.Id, order: ListSortOrder.Ascending);
126+
127+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_PrintMessagesSync
128+
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
129+
threadId: thread.Id,
130+
order: ListSortOrder.Ascending);
102131

103132
foreach (ThreadMessage threadMessage in messages)
104133
{
105-
foreach (MessageContent contentItem in threadMessage.ContentItems)
134+
foreach (MessageContent content in threadMessage.ContentItems)
106135
{
107-
if (contentItem is MessageTextContent textItem)
136+
switch (content)
108137
{
109-
Console.Write($"{threadMessage.Role}: {textItem.Text}");
138+
case MessageTextContent textItem:
139+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
140+
break;
110141
}
111-
Console.WriteLine();
112142
}
113143
}
114144
```
115145

116146
Asynchronous sample:
117-
```C# Snippet:Sample_Agent_Multiple_Messages_PrintAsync
118-
PageableList<ThreadMessage> messages = await client.GetMessagesAsync(thread.Id, order:ListSortOrder.Ascending);
119147

120-
foreach (ThreadMessage threadMessage in messages)
148+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_PrintMessagesAsync
149+
AsyncPageable<ThreadMessage> messages = client.Messages.GetMessagesAsync(
150+
threadId: thread.Id,
151+
order: ListSortOrder.Ascending);
152+
153+
await foreach (ThreadMessage threadMessage in messages)
121154
{
122-
foreach (MessageContent contentItem in threadMessage.ContentItems)
155+
foreach (MessageContent content in threadMessage.ContentItems)
123156
{
124-
if (contentItem is MessageTextContent textItem)
157+
switch (content)
125158
{
126-
Console.Write($"{threadMessage.Role}: {textItem.Text}");
159+
case MessageTextContent textItem:
160+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
161+
break;
127162
}
128-
Console.WriteLine();
129163
}
130164
}
131165
```
132166

133-
5. Clean up resources by deleting thread and agent.
167+
5. Finally, clean up resources (delete the thread and agent).
134168

135169
Synchronous sample:
136-
```C# Snippet:AgentsOverviewCleanupSync
137-
client.DeleteThread(threadId: thread.Id);
138-
client.DeleteAgent(agentId: agent.Id);
170+
171+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CleanupSync
172+
client.Threads.DeleteThread(threadId: thread.Id);
173+
client.Administration.DeleteAgent(agentId: agent.Id);
139174
```
140175

141176
Asynchronous sample:
142-
```C# Snippet:AgentsOverviewCleanup
143-
await client.DeleteThreadAsync(threadId: thread.Id);
144-
await client.DeleteAgentAsync(agentId: agent.Id);
177+
178+
```C# Snippet:Sample_PersistentAgent_AdditionalMessages_CleanupAsync
179+
await client.Threads.DeleteThreadAsync(threadId: thread.Id);
180+
await client.Administration.DeleteAgentAsync(agentId: agent.Id);
145181
```

samples/microsoft/csharp/getting-started-agents/AdditionalMessageAsync.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
.SetBasePath(AppContext.BaseDirectory)
88
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
99
.Build();
10-
1110
var projectEndpoint = configuration["ProjectEndpoint"];
1211
var modelDeploymentName = configuration["ModelDeploymentName"];
1312
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
@@ -19,6 +18,7 @@
1918
tools: [new CodeInterpreterToolDefinition()]);
2019

2120
PersistentAgentThread thread = await client.Threads.CreateThreadAsync();
21+
2222
await client.Messages.CreateMessageAsync(
2323
thread.Id,
2424
MessageRole.User,
@@ -49,19 +49,21 @@ await client.Messages.CreateMessageAsync(
4949
|| run.Status == RunStatus.RequiresAction);
5050

5151
AsyncPageable<ThreadMessage> messages = client.Messages.GetMessagesAsync(
52-
thread.Id,
52+
threadId: thread.Id,
5353
order: ListSortOrder.Ascending);
5454

5555
await foreach (ThreadMessage threadMessage in messages)
5656
{
57-
foreach (MessageContent contentItem in threadMessage.ContentItems)
57+
foreach (MessageContent content in threadMessage.ContentItems)
5858
{
59-
if (contentItem is MessageTextContent textItem)
59+
switch (content)
6060
{
61-
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
61+
case MessageTextContent textItem:
62+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
63+
break;
6264
}
6365
}
6466
}
6567

6668
await client.Threads.DeleteThreadAsync(threadId: thread.Id);
67-
await client.Administration.DeleteAgentAsync(agentId: agent.Id);
69+
await client.Administration.DeleteAgentAsync(agentId: agent.Id);

samples/microsoft/csharp/getting-started-agents/AdditionalMessageSync.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
.SetBasePath(AppContext.BaseDirectory)
88
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
99
.Build();
10-
1110
var projectEndpoint = configuration["ProjectEndpoint"];
1211
var modelDeploymentName = configuration["ModelDeploymentName"];
1312
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
1413

15-
PersistentAgent agent = client.Administration.CreateAgent(
14+
PersistentAgent agent = await client.Administration.CreateAgentAsync(
1615
model: modelDeploymentName,
1716
name: "Math Tutor",
1817
instructions: "You are a personal electronics tutor. Write and run code to answer questions.",
1918
tools: [new CodeInterpreterToolDefinition()]);
2019

21-
PersistentAgentThread thread = client.Threads.CreateThread();
22-
client.Messages.CreateMessage(
20+
PersistentAgentThread thread = await client.Threads.CreateThreadAsync();
21+
22+
await client.Messages.CreateMessageAsync(
2323
thread.Id,
2424
MessageRole.User,
2525
"What is the impedance formula?");
2626

27-
ThreadRun run = client.Runs.CreateRun(
27+
ThreadRun run = await client.Runs.CreateRunAsync(
2828
threadId: thread.Id,
2929
agent.Id,
3030
additionalMessages: [
@@ -41,27 +41,29 @@
4141

4242
do
4343
{
44-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
45-
run = client.Runs.GetRun(thread.Id, run.Id);
44+
await Task.Delay(TimeSpan.FromMilliseconds(500));
45+
run = await client.Runs.GetRunAsync(thread.Id, run.Id);
4646
}
4747
while (run.Status == RunStatus.Queued
4848
|| run.Status == RunStatus.InProgress
4949
|| run.Status == RunStatus.RequiresAction);
5050

51-
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
52-
thread.Id,
51+
AsyncPageable<ThreadMessage> messages = client.Messages.GetMessagesAsync(
52+
threadId: thread.Id,
5353
order: ListSortOrder.Ascending);
5454

55-
foreach (ThreadMessage threadMessage in messages)
55+
await foreach (ThreadMessage threadMessage in messages)
5656
{
57-
foreach (MessageContent contentItem in threadMessage.ContentItems)
57+
foreach (MessageContent content in threadMessage.ContentItems)
5858
{
59-
if (contentItem is MessageTextContent textItem)
59+
switch (content)
6060
{
61-
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
61+
case MessageTextContent textItem:
62+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
63+
break;
6264
}
6365
}
6466
}
6567

66-
client.Threads.DeleteThread(threadId: thread.Id);
67-
client.Administration.DeleteAgent(agentId: agent.Id);
68+
await client.Threads.DeleteThreadAsync(threadId: thread.Id);
69+
await client.Administration.DeleteAgentAsync(agentId: agent.Id);

0 commit comments

Comments
 (0)