community[patch]: Fix Playwright Tools bug with Pydantic schemas (#27050)

- Add tests for Playwright tools schema serialization
- Introduce base empty args Input class for BaseBrowserTool

Test Plan: `poetry run pytest
tests/unit_tests/tools/playwright/test_all.py`

Fixes #26758

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
Sergey Ryabov 2024-10-30 23:45:36 +00:00 committed by GitHub
parent 92024d0d7d
commit 8180637345
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 42 additions and 3 deletions

View File

@ -58,6 +58,7 @@ oracledb>=2.2.0,<3
pandas>=2.0.1,<3
pdfminer-six>=20221105,<20240706
pgvector>=0.1.6,<0.2
playwright>=1.48.0,<2
praw>=7.7.1,<8
premai>=0.3.25,<0.4
psychicapi>=0.8.0,<0.9

View File

@ -15,12 +15,16 @@ from langchain_community.tools.playwright.utils import (
)
class CurrentWebPageToolInput(BaseModel):
"""Explicit no-args input for CurrentWebPageTool."""
class CurrentWebPageTool(BaseBrowserTool): # type: ignore[override, override]
"""Tool for getting the URL of the current webpage."""
name: str = "current_webpage"
description: str = "Returns the URL of the current page"
args_schema: Type[BaseModel] = BaseModel
args_schema: Type[BaseModel] = CurrentWebPageToolInput
def _run(
self,

View File

@ -15,12 +15,16 @@ from langchain_community.tools.playwright.utils import (
)
class ExtractTextToolInput(BaseModel):
"""Explicit no-args input for ExtractTextTool."""
class ExtractTextTool(BaseBrowserTool): # type: ignore[override, override]
"""Tool for extracting all the text on the current webpage."""
name: str = "extract_text"
description: str = "Extract all the text on the current webpage"
args_schema: Type[BaseModel] = BaseModel
args_schema: Type[BaseModel] = ExtractTextToolInput
@model_validator(mode="before")
@classmethod

View File

@ -15,12 +15,16 @@ from langchain_community.tools.playwright.utils import (
)
class NavigateBackToolInput(BaseModel):
"""Explicit no-args input for NavigateBackTool."""
class NavigateBackTool(BaseBrowserTool): # type: ignore[override, override]
"""Navigate back to the previous page in the browser history."""
name: str = "previous_webpage"
description: str = "Navigate back to the previous page in the browser history"
args_schema: Type[BaseModel] = BaseModel
args_schema: Type[BaseModel] = NavigateBackToolInput
def _run(self, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
"""Use the tool."""

View File

@ -0,0 +1,26 @@
"""Test Playwright's Tools."""
from unittest.mock import Mock
import pytest
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
@pytest.mark.requires("playwright")
@pytest.mark.requires("bs4")
def test_playwright_tools_schemas() -> None:
"""Test calling 'tool_call_schema' for every tool to check to init issues."""
from playwright.sync_api import Browser
sync_browser = Mock(spec=Browser)
tools = PlayWrightBrowserToolkit.from_browser(sync_browser=sync_browser).get_tools()
for tool in tools:
try:
tool.tool_call_schema
except Exception as e:
raise AssertionError(
f"Error for '{tool.name}' tool: {type(e).__name__}: {e}"
) from e