Skip to content

Extract links #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fifty-cats-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": minor
---

extract links
8 changes: 8 additions & 0 deletions evals/evals.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@
{
"name": "google_flights",
"categories": ["act"]
},
{
"name": "extract_jfk_links",
"categories": ["extract"]
},
{
"name": "extract_single_link",
"categories": ["extract"]
}
]
}
125 changes: 125 additions & 0 deletions evals/tasks/extract_jfk_links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { EvalFunction } from "@/types/evals";
import { z } from "zod";

export const extract_jfk_links: EvalFunction = async ({
logger,
debugUrl,
sessionUrl,
stagehand,
}) => {
try {
await stagehand.page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/jfk/",
);

const extraction = await stagehand.page.extract({
instruction:
"extract all the record file name and their corresponding links",
schema: z.object({
records: z.array(
z.object({
file_name: z.string().describe("the file name of the record"),
link: z.string().url(),
}),
),
}),
});

// The list of records we expect to see
const expectedRecords = [
{
file_name: "104-10003-10041.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10003-10041.pdf",
},
{
file_name: "104-10004-10143 (C06932208).pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10004-10143%20(C06932208).pdf",
},
{
file_name: "104-10004-10143.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10004-10143.pdf",
},
{
file_name: "104-10004-10156.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10004-10156.pdf",
},
{
file_name: "104-10004-10213.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10004-10213.pdf",
},
{
file_name: "104-10005-10321.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10005-10321.pdf",
},
{
file_name: "104-10006-10247.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10006-10247.pdf",
},
{
file_name: "104-10007-10345.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10007-10345.pdf",
},
{
file_name: "104-10009-10021.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10009-10021.pdf",
},
{
file_name: "104-10009-10222.pdf",
link: "https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10009-10222.pdf",
},
];

const extractedRecords = extraction.records;

// Check that all expected records exist in the extraction
const missingRecords = expectedRecords.filter((expected) => {
return !extractedRecords.some(
(r) => r.file_name === expected.file_name && r.link === expected.link,
);
});

// Check that the extraction array is exactly length 10
if (extractedRecords.length !== 10) {
await stagehand.close();
return {
_success: false,
reason: `Extraction has ${extractedRecords.length} records (expected 10).`,
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
}

if (missingRecords.length > 0) {
await stagehand.close();
return {
_success: false,
reason: "Missing one or more expected records.",
missingRecords,
extractedRecords,
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
}

// If we reach here, the number of records is correct, and all are present
await stagehand.close();
return {
_success: true,
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
} catch (error) {
await stagehand.close();

return {
_success: false,
error: JSON.parse(JSON.stringify(error, null, 2)),
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
}
};
52 changes: 52 additions & 0 deletions evals/tasks/extract_single_link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { EvalFunction } from "@/types/evals";
import { z } from "zod";

export const extract_single_link: EvalFunction = async ({
logger,
debugUrl,
sessionUrl,
stagehand,
}) => {
try {
await stagehand.page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/geniusee/",
);

const extraction = await stagehand.page.extract({
instruction: "extract the link to the 'contact us' page",
schema: z.object({
link: z.string().url(),
}),
});

await stagehand.close();
const extractedLink = extraction.link;
const expectedLink =
"https://browserbase.github.io/stagehand-eval-sites/sites/geniusee/#contact";

if (extractedLink === expectedLink) {
return {
_success: true,
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
}
return {
_success: false,
reason: `Extracted link: ${extractedLink} does not match expected link: ${expectedLink}`,
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
} catch (error) {
await stagehand.close();
return {
_success: false,
error: JSON.parse(JSON.stringify(error, null, 2)),
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
}
};
Loading