Skip to content
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

add gh actions #72

Merged
merged 25 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
OPENAI_API_KEY=""
BROWSERBASE_API_KEY=""
BRAINTRUST_API_KEY=""
ANTHROPIC_API_KEY=""
ANTHROPIC_API_KEY=""
HEADLESS=false
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Continuous Integration

on:
pull_request:
branches:
- main

jobs:
run-evals:
runs-on: ubuntu-latest
timeout-minutes: 25

steps:
- name: Check out repository code
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install pnpm
run: npm install -g pnpm

- name: Install dependencies
run: pnpm install --no-frozen-lockfile

- name: Install Playwright browsers
run: pnpm exec playwright install --with-deps

- name: Run Evals
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
BRAINTRUST_API_KEY: ${{ secrets.BRAINTRUST_API_KEY }}
HEADLESS: true
run: pnpm evals
timeout-minutes: 20
106 changes: 58 additions & 48 deletions evals/index.eval.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Eval } from "braintrust";
import { Stagehand } from "../lib";
import { z } from "zod";
import process from 'process';

const vanta = async () => {
const stagehand = new Stagehand({ env: "LOCAL" });
const stagehand = new Stagehand({ env: "LOCAL", headless: process.env.HEADLESS !== 'false' });
await stagehand.init();

await stagehand.page.goto("https://www.vanta.com/");
Expand Down Expand Up @@ -33,7 +34,7 @@ const vanta = async () => {


const vanta_h = async () => {
const stagehand = new Stagehand({ env: "LOCAL" });
const stagehand = new Stagehand({ env: "LOCAL", headless: process.env.HEADLESS !== 'false'});
await stagehand.init();

await stagehand.page.goto("https://www.vanta.com/");
Expand All @@ -48,7 +49,7 @@ const vanta_h = async () => {
};

const peeler_simple = async () => {
const stagehand = new Stagehand({ env: "LOCAL" });
const stagehand = new Stagehand({ env: "LOCAL", headless: process.env.HEADLESS !== 'false'});
await stagehand.init();

await stagehand.page.goto(`file://${process.cwd()}/evals/assets/peeler.html`);
Expand All @@ -68,7 +69,8 @@ const peeler_simple = async () => {
const peeler_complex = async () => {
const stagehand = new Stagehand({
env: "LOCAL",
verbose: true,
verbose: 1,
headless: process.env.HEADLESS !== 'false',
});
await stagehand.init();

Expand Down Expand Up @@ -96,7 +98,8 @@ const peeler_complex = async () => {
const wikipedia = async () => {
const stagehand = new Stagehand({
env: "LOCAL",
verbose: true,
verbose: 2,
headless: process.env.HEADLESS !== 'false',
});
await stagehand.init();

Expand All @@ -114,38 +117,48 @@ const wikipedia = async () => {


const costar = async () => {
const stagehand = new Stagehand({ env: "LOCAL", verbose: true, debugDom: true });
const stagehand = new Stagehand({ env: "LOCAL", verbose: 2, debugDom: true, headless: process.env.HEADLESS !== 'false' });
await stagehand.init();
// TODO: fix this eval - does not work in headless mode
try {
await Promise.race([
stagehand.page.goto("https://www.costar.com/"),
new Promise((_, reject) => setTimeout(() => reject(new Error('Navigation timeout')), 30000))
]);
await stagehand.waitForSettledDom();

await stagehand.page.goto("https://www.costar.com/");
await stagehand.waitForSettledDom();

await stagehand.act({ action: "click on the first article" });
await stagehand.act({ action: "click on the first article" });

await stagehand.act({ action: "find the footer of the page" });
await stagehand.act({ action: "find the footer of the page" });

await stagehand.waitForSettledDom();
const articleTitle = await stagehand.extract({
await stagehand.waitForSettledDom();
const articleTitle = await stagehand.extract({
instruction: "extract the title of the article",
schema: z.object({
title: z.string().describe("the title of the article").nullable(),
title: z.string().describe("the title of the article").nullable(),
}),
modelName: "gpt-4o-2024-08-06"
});

console.log("articleTitle", articleTitle);

// Check if the title is more than 5 characters
const isTitleValid = articleTitle.title !== null && articleTitle.title.length > 5;

await stagehand.context.close();

return isTitleValid;
});

console.log("articleTitle", articleTitle);

// Check if the title is more than 5 characters
const isTitleValid = articleTitle.title !== null && articleTitle.title.length > 5;

await stagehand.context.close();

return isTitleValid;

} catch (error) {
console.error(`Error in costar function: ${error.message}`);
return { title: null };
} finally {
await stagehand.context.close();
}
};


const google_jobs = async () => {
const stagehand = new Stagehand({ env: "LOCAL", verbose: true, debugDom: true });
const stagehand = new Stagehand({ env: "LOCAL", verbose: 2, debugDom: true, headless: process.env.HEADLESS !== 'false' });
await stagehand.init({ modelName: "gpt-4o-2024-08-06" });

await stagehand.page.goto("https://www.google.com/");
Expand Down Expand Up @@ -212,35 +225,32 @@ const exactMatch = (args: { input; output; expected? }) => {
};
};


Eval("stagehand", {
data: () => {
return [
{
input: {
name: "vanta",
},
},
{
input: {
name: "vanta_h",
},
},
{
input: {
name: "peeler_simple",
},
},
{
input: { name: "wikipedia" },
},
{ input: { name: "vanta" } },
{ input: { name: "vanta_h" } },
{ input: { name: "peeler_simple" } },
{ input: { name: "wikipedia" } },
{ input: { name: "peeler_complex" } },
{ input: { name: "costar" } },
{ input: { name: "google_jobs" } },
// { input: { name: "costar" } }, // TODO: fix this eval - does not work in headless mode
{ input: { name: "google_jobs" } }
];
},
task: async (input) => {
const result = await tasks[input.name](input);
return result;
try {
const result = await tasks[input.name](input);
if (result) {
console.log(`✅ ${input.name}: Passed`);
} else {
console.log(`❌ ${input.name}: Failed`);
}
return result;
} catch (error) {
console.error(`❌ ${input.name}: Error - ${error}`);
return false;
}
},
scores: [exactMatch],
});
94 changes: 39 additions & 55 deletions evals/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,53 @@ import { Eval } from "braintrust";
import { Stagehand } from "../lib";
import { z } from "zod";

const google_jobs = async () => {
const stagehand = new Stagehand({ env: "LOCAL", verbose: true, debugDom: true });
await stagehand.init({ modelName: "gpt-4o-2024-08-06" });

await stagehand.page.goto("https://www.google.com/");
await stagehand.waitForSettledDom();

await stagehand.act({ action: "click on the about page" });

await stagehand.act({ action: "click on the careers page" });

await stagehand.act({ action: "input data scientist into role" });

await stagehand.act({ action: "input new york city into location" });

await stagehand.act({ action: "click on the search button" });

await stagehand.act({ action: "click on the learn more button for the first job" });

const jobDetails = await stagehand.extract({
instruction: "Extract the following details from the job posting: application deadline, minimum qualifications (degree and years of experience), and preferred qualifications (degree and years of experience)",
schema: z.object({
applicationDeadline: z.string().describe("The date until which the application window will be open"),
minimumQualifications: z.object({
degree: z.string().describe("The minimum required degree"),
yearsOfExperience: z.number().describe("The minimum required years of experience")
const costar = async () => {
const stagehand = new Stagehand({ env: "LOCAL", verbose: 2, debugDom: true, headless: process.env.HEADLESS !== 'false' });
await stagehand.init();
// TODO: fix this eval
try {
await Promise.race([
stagehand.page.goto("https://www.costar.com/"),
new Promise((_, reject) => setTimeout(() => reject(new Error('Navigation timeout')), 30000))
]);
await stagehand.waitForSettledDom();

await stagehand.act({ action: "click on the first article" });

await stagehand.act({ action: "find the footer of the page" });

await stagehand.waitForSettledDom();
const articleTitle = await stagehand.extract({
instruction: "extract the title of the article",
schema: z.object({
title: z.string().describe("the title of the article").nullable(),
}),
preferredQualifications: z.object({
degree: z.string().describe("The preferred degree"),
yearsOfExperience: z.number().describe("The preferred years of experience")
})
}),
modelName: "gpt-4o-2024-08-06"
});

console.log("Job Details:", jobDetails);

const isJobDetailsValid = jobDetails &&
Object.values(jobDetails).every(value =>
value !== null &&
value !== undefined &&
value !== '' &&
(typeof value !== 'object' || Object.values(value).every(v =>
v !== null &&
v !== undefined &&
v !== '' &&
(typeof v === 'number' || typeof v === 'string')
))
);
modelName: "gpt-4o-2024-08-06"
});

await stagehand.context.close();
console.log("articleTitle", articleTitle);

console.log("Job Details valid:", isJobDetailsValid);

return isJobDetailsValid;
// Check if the title is more than 5 characters
const isTitleValid = articleTitle.title !== null && articleTitle.title.length > 5;

await stagehand.context.close();

return isTitleValid;

} catch (error) {
console.error(`Error in costar function: ${error.message}`);
return { title: null };
} finally {
await stagehand.context.close();
}
};

async function main() {
const [googleJobsResult] = await Promise.all([
google_jobs(),
const [costarResult] = await Promise.all([
costar(),
]);

console.log("Google Jobs result:", googleJobsResult);
console.log("Costar result:", costarResult);
}

main().catch(console.error);
Loading