2024-05-16 15:21:23 +00:00
|
|
|
from typing import Iterator, Optional, Sequence
|
2024-04-25 01:11:03 +00:00
|
|
|
|
|
|
|
from langchain_core.documents import Document
|
|
|
|
|
|
|
|
from langchain_community.document_loaders.base import BaseLoader
|
|
|
|
|
|
|
|
|
|
|
|
class BrowserbaseLoader(BaseLoader):
|
|
|
|
"""Load pre-rendered web pages using a headless browser hosted on Browserbase.
|
|
|
|
|
|
|
|
Depends on `browserbase` package.
|
|
|
|
Get your API key from https://browserbase.com
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2024-05-16 15:21:23 +00:00
|
|
|
urls: Sequence[str],
|
2024-04-25 01:11:03 +00:00
|
|
|
text_content: bool = False,
|
2024-05-16 15:21:23 +00:00
|
|
|
api_key: Optional[str] = None,
|
|
|
|
project_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
proxy: Optional[bool] = None,
|
2024-04-25 01:11:03 +00:00
|
|
|
):
|
|
|
|
self.urls = urls
|
|
|
|
self.text_content = text_content
|
2024-05-16 15:21:23 +00:00
|
|
|
self.session_id = session_id
|
|
|
|
self.proxy = proxy
|
2024-04-25 01:11:03 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from browserbase import Browserbase
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError(
|
|
|
|
"You must run "
|
|
|
|
"`pip install --upgrade "
|
|
|
|
"browserbase` "
|
|
|
|
"to use the Browserbase loader."
|
|
|
|
)
|
|
|
|
|
2024-05-16 15:21:23 +00:00
|
|
|
self.browserbase = Browserbase(api_key, project_id)
|
2024-04-25 01:11:03 +00:00
|
|
|
|
|
|
|
def lazy_load(self) -> Iterator[Document]:
|
|
|
|
"""Load pages from URLs"""
|
2024-05-16 15:21:23 +00:00
|
|
|
pages = self.browserbase.load_urls(
|
|
|
|
self.urls, self.text_content, self.session_id, self.proxy
|
|
|
|
)
|
2024-04-25 01:11:03 +00:00
|
|
|
|
|
|
|
for i, page in enumerate(pages):
|
|
|
|
yield Document(
|
|
|
|
page_content=page,
|
|
|
|
metadata={
|
|
|
|
"url": self.urls[i],
|
|
|
|
},
|
|
|
|
)
|