Skip to content

The agent engineering platform.

Merge branch 'master' into ywkim/master

Eugene Yurtsev6da158388b12
diff --cc libs/langchain/langchain/document_loaders/url_playwright.py
index bce53f9155,0db4168e5c..16f5b00fd3
--- a/libs/langchain/langchain/document_loaders/url_playwright.py
+++ b/libs/langchain/langchain/document_loaders/url_playwright.py
@@@ -15,92 -9,9 +15,93 @@@ if TYPE_CHECKING
logger = logging.getLogger(__name__)
+class PlaywrightEvaluator(ABC):
+ """Abstract base class for all evaluators.
+
+ Each evaluator should take a page, a browser instance, and a response
+ object, process the page as necessary, and return the resulting text.
+ """
+
+ @abstractmethod
+ def evaluate(self, page: "Page", browser: "Browser", response: "Response") -> str:
+ """Synchronously process the page and return the resulting text.
+
+ Args:
+ page: The page to process.
+ browser: The browser instance.
+ response: The response from page.goto().
+
+ Returns:
+ text: The text content of the page.
+ """
+ pass
+
+ @abstractmethod
+ async def evaluate_async(
+ self, page: "AsyncPage", browser: "AsyncBrowser", response: "AsyncResponse"
+ ) -> str:
+ """Asynchronously process the page and return the resulting text.
+
+ Args:
+ page: The page to process.
+ browser: The browser instance.
+ response: The response from page.goto().
+
+ Returns:
+ text: The text content of the page.
+ """
+ pass
+
+
+class UnstructuredHtmlEvaluator(PlaywrightEvaluator):
+ """Evaluates the page HTML content using the `unstructured` library."""
+
+ def __init__(self, remove_selectors: Optional[List[str]] = None):
+ """Initialize UnstructuredHtmlEvaluator."""
+ try:
+ import unstructured # noqa:F401
+ except ImportError:
+ raise ImportError(
+ "unstructured package not found, please install it with "
+ "`pip install unstructured`"
+ )
+
+ self.remove_selectors = remove_selectors
+
+ def evaluate(self, page: "Page", browser: "Browser", response: "Response") -> str:
+ """Synchronously process the HTML content of the page."""
+ from unstructured.partition.html import partition_html
+
+ for selector in self.remove_selectors or []:
+ elements = page.locator(selector).all()
+ for element in elements:
+ if element.is_visible():
+ element.evaluate("element => element.remove()")
+
+ page_source = page.content()
+ elements = partition_html(text=page_source)
+ return "\n\n".join([str(el) for el in elements])
+
+ async def evaluate_async(
+ self, page: "AsyncPage", browser: "AsyncBrowser", response: "AsyncResponse"
+ ) -> str:
+ """Asynchronously process the HTML content of the page."""
+ from unstructured.partition.html import partition_html
+
+ for selector in self.remove_selectors or []:
+ elements = await page.locator(selector).all()
+ for element in elements:
+ if await element.is_visible():
+ await element.evaluate("element => element.remove()")
+
+ page_source = await page.content()
+ elements = partition_html(text=page_source)
+ return "\n\n".join([str(el) for el in elements])
+
+
class PlaywrightURLLoader(BaseLoader):
- """Loader that uses Playwright and PlaywrightEvaluator to load the html.
+ """Load `HTML` pages with `Playwright` and parse with `Unstructured`.
+
This is useful for loading pages that require javascript to render.
Attributes: