Skip to content

Commit 00dfe91

Browse files
committed
updated browserbase integration for review
1 parent a178d6b commit 00dfe91

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

docs/core_docs/docs/integrations/document_loaders/web_loaders/browserbase.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- Get an API key from [browserbase.com](https://browserbase.com) and set it in environment variables (`BROWSERBASE_API_KEY`).
1010
- Install the [Browserbase SDK](http://github.com/browserbase/js-sdk):
1111

12-
```
12+
```bash npm2yarn
1313
npm i @browserbasehq/sdk
1414
```
1515

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
import { Document } from "@langchain/core/documents";
2-
import type { BrowserbaseLoadOptions } from "@browserbasehq/sdk";
1+
import { Document, DocumentInterface } from "@langchain/core/documents";
2+
import Browserbase, { BrowserbaseLoadOptions } from "@browserbasehq/sdk";
33
import { BaseDocumentLoader } from "../base.js";
44
import type { DocumentLoader } from "../base.js";
55

6-
type BrowserbaseLoaderOptions = BrowserbaseLoadOptions & {
6+
interface BrowserbaseLoaderOptions extends BrowserbaseLoadOptions {
77
apiKey?: string;
8-
};
8+
}
99

1010
/**
1111
* Load pre-rendered web pages using a headless browser hosted on Browserbase.
1212
*
1313
* Depends on `@browserbasehq/sdk` package.
1414
* Get your API key from https://browserbase.com
1515
*
16+
* @example
17+
* ```javascript
18+
* import { BrowserbaseLoader } from "langchain/document_loaders/web/browserbase.js";
19+
*
20+
* const loader = new BrowserbaseLoader(["https://example.com"], {
21+
* apiKey: process.env.BROWSERBASE_API_KEY,
22+
* textContent: true,
23+
* });
24+
*
25+
* const docs = await loader.load();
26+
* ```
27+
*
1628
* @param {string[]} urls - The URLs of the web pages to load.
1729
* @param {BrowserbaseLoaderOptions} [options] - Browserbase client options.
1830
*/
@@ -25,19 +37,22 @@ export class BrowserbaseLoader
2537

2638
options: BrowserbaseLoaderOptions;
2739

40+
browserbase: Browserbase;
41+
2842
constructor(urls: string[], options: BrowserbaseLoaderOptions = {}) {
2943
super();
3044
this.urls = urls;
3145
this.options = options;
46+
this.browserbase = new Browserbase(options.apiKey);
3247
}
3348

3449
/**
3550
* Load pages from URLs.
3651
*
37-
* @returns {Promise<Document[]>} - A generator that yields loaded documents.
52+
* @returns {Promise<DocumentInterface[]>} - A promise which resolves to a list of documents.
3853
*/
3954

40-
async load(): Promise<Document[]> {
55+
async load(): Promise<DocumentInterface[]> {
4156
const documents: Document[] = [];
4257
for await (const doc of this.lazyLoad()) {
4358
documents.push(doc);
@@ -49,11 +64,10 @@ export class BrowserbaseLoader
4964
/**
5065
* Load pages from URLs.
5166
*
52-
* @returns {Generator<Document>} - A generator that yields loaded documents.
67+
* @returns {Generator<DocumentInterface>} - A generator that yields documents.
5368
*/
5469
async *lazyLoad() {
55-
const browserbase = await BrowserbaseLoader.imports(this.options.apiKey);
56-
const pages = await browserbase.loadURLs(this.urls, this.options);
70+
const pages = await this.browserbase.loadURLs(this.urls, this.options);
5771

5872
let index = 0;
5973
for await (const page of pages) {
@@ -67,17 +81,4 @@ export class BrowserbaseLoader
6781
index += index + 1;
6882
}
6983
}
70-
71-
static async imports(apiKey?: string) {
72-
try {
73-
const { default: Browserbase } = await import("@browserbasehq/sdk");
74-
return new Browserbase(apiKey);
75-
} catch (error) {
76-
throw new Error(
77-
"You must run " +
78-
"`npm install --save @browserbasehq/sdk` " +
79-
"to use the Browserbase loader."
80-
);
81-
}
82-
}
8384
}

0 commit comments

Comments
 (0)