Skip to content

Commit b56f234

Browse files
authored
Create BrowserAutomationSync.cs
1 parent 7f81f4c commit b56f234

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using Azure;
2+
using Azure.Core;
3+
using Azure.AI.Agents.Persistent;
4+
using Azure.Identity;
5+
using Microsoft.Extensions.Configuration;
6+
using System.Text.Json;
7+
8+
IConfigurationRoot configuration = new ConfigurationBuilder()
9+
.SetBasePath(AppContext.BaseDirectory)
10+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
11+
.Build();
12+
13+
var projectEndpoint = configuration["ProjectEndpoint"];
14+
var modelDeploymentName = configuration["ModelDeploymentName"];
15+
var playwrightConnectionResourceId = configuration["PlaywrightConnectionResourceId"];
16+
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
17+
18+
object browserAutomationToolDefinition = null;
19+
if (string.IsNullOrWhitespace(playwrightConnectionResourceId))
20+
{
21+
browserAutomationToolDefinition = new(
22+
type: "browser_automation",
23+
);
24+
}
25+
else
26+
{
27+
browserAutomationToolDefinition = new(
28+
type: "browser_automation",
29+
browser_automation: new(
30+
connection: new(
31+
id: playwrightConnectionResourceId,
32+
),
33+
),
34+
);
35+
}
36+
37+
object agentPayload = new(
38+
name: "Browser Automation Tool Demo Agent",
39+
description: "A simple agent that uses the browser automation tool.",
40+
model: modelDeploymentName,
41+
instructions: "You are an agent to help me with browser automation tasks. "
42+
+ "You can answer questions, provide information, and assist with various tasks "
43+
+ "related to web browsing using the browser_automation tool available to you.",
44+
tools: new[]
45+
{
46+
browserAutomationToolDefinition
47+
},
48+
);
49+
50+
RequestContent agentRequestContent = RequestContent.Create(BinaryData.FromObjectAsJson(agentPayload));
51+
Response agentResponse = client.Administration.CreateAgent(content: agentRequestContent);
52+
PersistentAgent agent = PersistentAgent.FromResponse(agentResponse);
53+
54+
PersistentAgentThread thread = client.Threads.CreateThread();
55+
56+
client.Messages.CreateMessage(
57+
thread.Id,
58+
MessageRole.User,
59+
"Find a popular quinoa salad recipe on Allrecipes with more than 500 reviews and a rating above 4 stars. Create a shopping list of ingredients for this recipe and include the total cooking and preparation time. on https://www.allrecipes.com/"
60+
);
61+
62+
ThreadRun run = client.Runs.CreateRun(thread.Id, agent.Id);
63+
64+
do
65+
{
66+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
67+
run = client.Runs.GetRun(thread.Id, run.Id);
68+
}
69+
while (run.Status == RunStatus.Queued
70+
|| run.Status == RunStatus.InProgress
71+
|| run.Status == RunStatus.RequiresAction);
72+
73+
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
74+
threadId: thread.Id,
75+
order: ListSortOrder.Ascending
76+
);
77+
78+
foreach (ThreadMessage threadMessage in messages)
79+
{
80+
foreach (MessageContent content in threadMessage.ContentItems)
81+
{
82+
switch (content)
83+
{
84+
case MessageTextContent textItem:
85+
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
86+
break;
87+
}
88+
}
89+
}
90+
91+
client.Threads.DeleteThread(thread.Id);
92+
client.Administration.DeleteAgent(agent.Id);

0 commit comments

Comments
 (0)