Skip to content

Commit 719b1ab

Browse files
authored
Merge pull request #47 from RobiladK/patch-11
Create AdditionalMessageSync.cs
2 parents eb5a0cb + 85f65c3 commit 719b1ab

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
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 = client.Administration.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+
PersistentAgentThread thread = client.Threads.CreateThread();
22+
client.Messages.CreateMessage(
23+
thread.Id,
24+
MessageRole.User,
25+
"What is the impedance formula?");
26+
27+
ThreadRun run = client.Runs.CreateRun(
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+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
45+
run = client.Runs.GetRun(thread.Id, run.Id);
46+
}
47+
while (run.Status == RunStatus.Queued
48+
|| run.Status == RunStatus.InProgress
49+
|| run.Status == RunStatus.RequiresAction);
50+
51+
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
52+
thread.Id,
53+
order: ListSortOrder.Ascending);
54+
55+
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+
client.Threads.DeleteThread(threadId: thread.Id);
67+
client.Administration.DeleteAgent(agentId: agent.Id);

0 commit comments

Comments
 (0)