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 ();
615var projectEndpoint = configuration [" ProjectEndpoint" ];
716var 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-
1320Synchronous 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
2230Asynchronous 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
3342Synchronous 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(
5685do
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
6595Asynchronous 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(
88113do
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
99125Synchronous 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
103132foreach (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
116146Asynchronous 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
135169Synchronous 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
141176Asynchronous 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```
0 commit comments