Skip to content

add laminar observability example #539

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions examples/laminar_observability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// This example demonstrates how to use Laminar to observe a Stagehand session.
//
// Prerequisites:
// 1. Sign up on Laminar with a free account and get a project API key.
// - You can alternatively spin up Laminar locally, see https://github.com/lmnr-ai/lmnr
// 2. Run `export LMNR_PROJECT_API_KEY=<your-api-key>`
// 3. Expose your OPENAI_API_KEY to the environment.

import { Laminar, getTracer } from "@lmnr-ai/lmnr";
import { Stagehand } from "@/dist";
import { OpenAI } from "openai";
import { z } from "zod";

Laminar.initialize({
projectApiKey: process.env.LMNR_PROJECT_API_KEY,
instrumentModules: {
stagehand: Stagehand,
OpenAI: OpenAI,
},
});

async function example() {
const stagehand = new Stagehand({
env: "LOCAL",
modelName: "gpt-4o-mini",
modelClientOptions: {
apiKey: process.env.OPENAI_API_KEY,
aiSdkTelemetrySettings: {
isEnabled: true,
tracer: getTracer(),
},
},
});
await stagehand.init();
const page = stagehand.page;
await page.goto("https://www.lmnr.ai/blog");

const latestBlogPost = await page.extract({
instruction: "Get the information about the latest blog post",
schema: z.object({
title: z.string(),
date: z.string(),
}),
});
console.log(latestBlogPost);

await stagehand.close();

// In one-off scripts like this, it is important to flush Laminar to make
// sure all spans and session data is flushed and sent to the server.
await Laminar.flush();
}

(async () => {
await example();
})();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@eslint/js": "^9.16.0",
"@langchain/core": "^0.3.40",
"@langchain/openai": "^0.4.4",
"@lmnr-ai/lmnr": "^0.6.0",
"@types/adm-zip": "^0.5.7",
"@types/cheerio": "^0.22.35",
"@types/express": "^4.17.21",
Expand Down
Loading