Skip to content

Commit 8c99959

Browse files
authored
Merge pull request #23 from RobiladK/patch-6
Create AdditionalMessage.md
2 parents be304d8 + fa48ba5 commit 8c99959

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# Sample for using additional messages while creating agent run in Azure.AI.Agents
2+
3+
1. First get `ProjectEndpoint` and `ModelDeploymentName` from config and create a `PersistentAgentsClient`.
4+
5+
```C# Snippet:Sample_Agent_Multiple_Messages_Create
6+
var projectEndpoint = configuration["ProjectEndpoint"];
7+
var modelDeploymentName = configuration["ModelDeploymentName"];
8+
PersistentAgentsClient client = new(new Uri(projectEndpoint), new DefaultAzureCredential());
9+
```
10+
11+
2. Next we will need to create an agent.
12+
13+
Synchronous sample:
14+
```C# Snippet:Sample_Agent_Multiple_Messages_Create
15+
PersistentAgent agent = client.CreateAgent(
16+
model: modelDeploymentName,
17+
name: "Math Tutor",
18+
instructions: "You are a personal electronics tutor. Write and run code to answer questions.",
19+
tools: [new CodeInterpreterToolDefinition()]);
20+
```
21+
22+
Asynchronous sample:
23+
```C# Snippet:Sample_Agent_Multiple_Messages_CreateAsync
24+
PersistentAgent agent = await client.CreateAgentAsync(
25+
model: modelDeploymentName,
26+
name: "Math Tutor",
27+
instructions: "You are a personal electronics tutor. Write and run code to answer questions.",
28+
tools: [new CodeInterpreterToolDefinition()]);
29+
```
30+
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.
32+
33+
Synchronous sample:
34+
```C# Snippet:Sample_Agent_Multiple_Messages_Run
35+
PersistentAgentThread thread = client.CreateThread();
36+
client.CreateMessage(
37+
thread.Id,
38+
MessageRole.User,
39+
"What is the impedance formula?");
40+
41+
ThreadRun agentRun = client.CreateRun(
42+
threadId: thread.Id,
43+
agent.Id,
44+
additionalMessages: [
45+
new ThreadMessageOptions(
46+
role: MessageRole.Agent,
47+
content: "E=mc^2"
48+
),
49+
new ThreadMessageOptions(
50+
role: MessageRole.User,
51+
content: "What is the impedance formula?"
52+
),
53+
]
54+
);
55+
56+
do
57+
{
58+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
59+
agentRun = client.GetRun(thread.Id, agentRun.Id);
60+
}
61+
while (agentRun.Status == RunStatus.Queued
62+
|| agentRun.Status == RunStatus.InProgress);
63+
```
64+
65+
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?");
72+
73+
ThreadRun agentRun = await client.CreateRunAsync(
74+
threadId: thread.Id,
75+
agent.Id,
76+
additionalMessages: [
77+
new ThreadMessageOptions(
78+
role: MessageRole.Agent,
79+
content: "E=mc^2"
80+
),
81+
new ThreadMessageOptions(
82+
role: MessageRole.User,
83+
content: "What is the impedance formula?"
84+
),
85+
]
86+
);
87+
88+
do
89+
{
90+
await Task.Delay(TimeSpan.FromMilliseconds(500));
91+
agentRun = await client.GetRunAsync(thread.Id, agentRun.Id);
92+
}
93+
while (agentRun.Status == RunStatus.Queued
94+
|| agentRun.Status == RunStatus.InProgress);
95+
```
96+
97+
4. Next, we print out all the messages to the console.
98+
99+
Synchronous sample:
100+
```C# Snippet:Sample_Agent_Multiple_Messages_Print
101+
PageableList<ThreadMessage> messages = client.GetMessages(thread.Id, order: ListSortOrder.Ascending);
102+
103+
foreach (ThreadMessage threadMessage in messages)
104+
{
105+
foreach (MessageContent contentItem in threadMessage.ContentItems)
106+
{
107+
if (contentItem is MessageTextContent textItem)
108+
{
109+
Console.Write($"{threadMessage.Role}: {textItem.Text}");
110+
}
111+
Console.WriteLine();
112+
}
113+
}
114+
```
115+
116+
Asynchronous sample:
117+
```C# Snippet:Sample_Agent_Multiple_Messages_PrintAsync
118+
PageableList<ThreadMessage> messages = await client.GetMessagesAsync(thread.Id, order:ListSortOrder.Ascending);
119+
120+
foreach (ThreadMessage threadMessage in messages)
121+
{
122+
foreach (MessageContent contentItem in threadMessage.ContentItems)
123+
{
124+
if (contentItem is MessageTextContent textItem)
125+
{
126+
Console.Write($"{threadMessage.Role}: {textItem.Text}");
127+
}
128+
Console.WriteLine();
129+
}
130+
}
131+
```
132+
133+
5. Clean up resources by deleting thread and agent.
134+
135+
Synchronous sample:
136+
```C# Snippet:AgentsOverviewCleanupSync
137+
client.DeleteThread(threadId: thread.Id);
138+
client.DeleteAgent(agentId: agent.Id);
139+
```
140+
141+
Asynchronous sample:
142+
```C# Snippet:AgentsOverviewCleanup
143+
await client.DeleteThreadAsync(threadId: thread.Id);
144+
await client.DeleteAgentAsync(agentId: agent.Id);
145+
```

0 commit comments

Comments
 (0)