Skip to content

Commit e10ddaf

Browse files
authored
Merge pull request #48 from RobiladK/patch-12
Create AdditionalMessageAsync.cs
2 parents 719b1ab + 01b092b commit e10ddaf

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Azure;
2+
using Azure.AI.Agents.Persistent;
3+
using Azure.Identity;
4+
using Microsoft.Extensions.Configuration;
5+
6+
IConfigurationRoot configuration = new ConfigurationBuilder()
7+
.SetBasePath(AppContext.BaseDirectory)
8+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
9+
.Build();
10+
11+
var projectEndpoint = configuration["ProjectEndpoint"];
12+
var modelDeploymentName = configuration["ModelDeploymentName"];
13+
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
14+
15+
PersistentAgent agent = await client.Administration.CreateAgentAsync(
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+
PersistentAgentThread thread = await client.Threads.CreateThreadAsync();
22+
await client.Messages.CreateMessageAsync(
23+
thread.Id,
24+
MessageRole.User,
25+
"What is the impedance formula?");
26+
27+
ThreadRun run = await client.Runs.CreateRunAsync(
28+
threadId: thread.Id,
29+
agent.Id,
30+
additionalMessages: [
31+
new ThreadMessageOptions(
32+
role: MessageRole.Agent,
33+
content: "E=mc^2"
34+
),
35+
new ThreadMessageOptions(
36+
role: MessageRole.User,
37+
content: "What is the impedance formula?"
38+
),
39+
]
40+
);
41+
42+
do
43+
{
44+
await Task.Delay(TimeSpan.FromMilliseconds(500));
45+
run = await client.Runs.GetRunAsync(thread.Id, run.Id);
46+
}
47+
while (run.Status == RunStatus.Queued
48+
|| run.Status == RunStatus.InProgress
49+
|| run.Status == RunStatus.RequiresAction);
50+
51+
AsyncPageable<ThreadMessage> messages = client.Messages.GetMessagesAsync(
52+
thread.Id,
53+
order: ListSortOrder.Ascending);
54+
55+
await foreach (ThreadMessage threadMessage in messages)
56+
{
57+
foreach (MessageContent contentItem in threadMessage.ContentItems)
58+
{
59+
if (contentItem is MessageTextContent textItem)
60+
{
61+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
62+
}
63+
}
64+
}
65+
66+
await client.Threads.DeleteThreadAsync(threadId: thread.Id);
67+
await client.Administration.DeleteAgentAsync(agentId: agent.Id);

0 commit comments

Comments
 (0)