forked from microsoft-foundry/foundry-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserAutomationSync.cs
More file actions
92 lines (80 loc) · 2.99 KB
/
BrowserAutomationSync.cs
File metadata and controls
92 lines (80 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using Azure;
using Azure.Core;
using Azure.AI.Agents.Persistent;
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using System.Text.Json;
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var projectEndpoint = configuration["ProjectEndpoint"];
var modelDeploymentName = configuration["ModelDeploymentName"];
var playwrightConnectionResourceId = configuration["PlaywrightConnectionResourceId"];
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
object browserAutomationToolDefinition = null;
if (string.IsNullOrWhitespace(playwrightConnectionResourceId))
{
browserAutomationToolDefinition = new(
type: "browser_automation",
);
}
else
{
browserAutomationToolDefinition = new(
type: "browser_automation",
browser_automation: new(
connection: new(
id: playwrightConnectionResourceId,
),
),
);
}
object agentPayload = new(
name: "Browser Automation Tool Demo Agent",
description: "A simple agent that uses the browser automation tool.",
model: modelDeploymentName,
instructions: "You are an agent to help me with browser automation tasks. "
+ "You can answer questions, provide information, and assist with various tasks "
+ "related to web browsing using the browser_automation tool available to you.",
tools: new[]
{
browserAutomationToolDefinition
},
);
RequestContent agentRequestContent = RequestContent.Create(BinaryData.FromObjectAsJson(agentPayload));
Response agentResponse = client.Administration.CreateAgent(content: agentRequestContent);
PersistentAgent agent = PersistentAgent.FromResponse(agentResponse);
PersistentAgentThread thread = client.Threads.CreateThread();
client.Messages.CreateMessage(
thread.Id,
MessageRole.User,
"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/"
);
ThreadRun run = client.Runs.CreateRun(thread.Id, agent.Id);
do
{
Thread.Sleep(TimeSpan.FromMilliseconds(500));
run = client.Runs.GetRun(thread.Id, run.Id);
}
while (run.Status == RunStatus.Queued
|| run.Status == RunStatus.InProgress
|| run.Status == RunStatus.RequiresAction);
Pageable<ThreadMessage> messages = client.Messages.GetMessages(
threadId: thread.Id,
order: ListSortOrder.Ascending
);
foreach (ThreadMessage threadMessage in messages)
{
foreach (MessageContent content in threadMessage.ContentItems)
{
switch (content)
{
case MessageTextContent textItem:
Console.WriteLine($"[{threadMessage.Role}]: {textItem.Text}");
break;
}
}
}
client.Threads.DeleteThread(thread.Id);
client.Administration.DeleteAgent(agent.Id);