Skip to content

Commit 1036423

Browse files
committed
browse, fix syntax
1 parent e8f3978 commit 1036423

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/content/docs/agents/examples/browse-the-web.mdx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Agents can browse the web using the [Browser Rendering](/browser-rendering/) API
1313

1414
The [Browser Rendering](/browser-rendering/) allows you to spin up headless browser instances, render web pages, and interact with websites through your Agent.
1515

16+
You can define a method that uses Puppeteer to pull the content of a web page, parse the DOM, and extract relevant information by calling the OpenAI model:
17+
1618
<TypeScriptExample>
1719

1820
```ts
@@ -21,9 +23,33 @@ interface Env {
2123
}
2224

2325
export class MyAgent extends Agent<Env> {
24-
constructor(env: Env) {
25-
super(env);
26-
}
26+
async browse(browserInstance: Fetcher, urls: string[]) {
27+
for (const url of urls) {
28+
const browser = await puppeteer.launch(browserInstance);
29+
const page = await browser.newPage();
30+
await page.goto(url);
31+
32+
await page.waitForSelector('body');
33+
const bodyContent = await page.$eval('body', (element) => element.innerHTML);
34+
const client = new OpenAI({
35+
apiKey: this.env.OPENAI_API_KEY,
36+
});
37+
38+
await client.chat.completions.create({
39+
model: this.env.MODEL || "4o",
40+
messages: [
41+
{
42+
role: 'user',
43+
content: `Return a JSON object with the product names, prices and URLs with the following format: { "name": "Product Name", "price": "Price", "url": "URL" } from the website content below. <content>${bodyContent}</content>`,
44+
},
45+
],
46+
response_format: {
47+
type: 'json_object',
48+
},
49+
});
50+
51+
await browser.close();
52+
}
2753
}
2854
```
2955

src/content/docs/agents/examples/using-ai-models.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class MyAgent extends Agent<Env> {
7070
connection.send(JSON.stringify({ type: 'error', error: error }));
7171
}
7272
}
73+
}
7374
```
7475

7576
</TypeScriptExample>

0 commit comments

Comments
 (0)