Skip to content

Commit

Permalink
ai sdk example
Browse files Browse the repository at this point in the history
  • Loading branch information
sameelarif committed Jan 7, 2025
1 parent 73e206b commit 587871f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
47 changes: 47 additions & 0 deletions examples/ai_sdk_example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { z } from "zod";
import { type ConstructorParams, type LogLine, Stagehand } from "../lib";
import { AISdkClient } from "./external_clients/aisdk";
import { openai } from "@ai-sdk/openai";

const StagehandConfig: ConstructorParams = {
env: "BROWSERBASE",
apiKey: process.env.BROWSERBASE_API_KEY,
projectId: process.env.BROWSERBASE_PROJECT_ID,
verbose: 1,
llmClient: new AISdkClient(
(message: LogLine) =>
console.log(`[stagehand::${message.category}] ${message.message}`),
openai("gpt-4o"),
),
debugDom: true,
};

async function example() {
const stagehand = new Stagehand(StagehandConfig);

await stagehand.init();
await stagehand.page.goto("https://news.ycombinator.com");

const headlines = await stagehand.page.extract({
instruction: "Extract only 3 stories from the Hacker News homepage.",
schema: z.object({
stories: z
.array(
z.object({
title: z.string(),
url: z.string(),
points: z.number(),
}),
)
.length(3),
}),
});

console.log(headlines);

await stagehand.close();
}

(async () => {
await example();
})();
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"popup": "npm run build-dom-scripts && tsx examples/popup.ts",
"2048": "npm run build-dom-scripts && tsx examples/2048.ts",
"popup": "npm run build-dom-scripts && tsx examples/popup.ts",
"example": "npm run build-dom-scripts && tsx examples/example.ts",
"debug-url": "npm run build-dom-scripts && tsx examples/debugUrl.ts",
"external-client": "npm run build-dom-scripts && tsx examples/external_client.ts",
"external-client": "npm run build-dom-scripts && tsx examples/ai_sdk_example.ts",
"format": "prettier --write .",
"prettier": "prettier --check .",
"prettier:fix": "prettier --write .",
Expand All @@ -34,6 +34,7 @@
"author": "Browserbase",
"license": "MIT",
"devDependencies": {
"@ai-sdk/openai": "^1.0.13",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.9",
"@eslint/js": "^9.16.0",
Expand Down

0 comments on commit 587871f

Please sign in to comment.