mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-29 07:19:59 +00:00
This PR upgrades langchain-community to pydantic 2.
* Most of this PR was auto-generated using code mods with gritql
(https://github.com/eyurtsev/migrate-pydantic/tree/main)
* Subsequently, some code was fixed manually due to accommodate
differences between pydantic 1 and 2
Breaking Changes:
- Use TEXTEMBED_API_KEY and TEXTEMBEB_API_URL for env variables for text
embed integrations:
cbea780492
Other changes:
- Added pydantic_settings as a required dependency for community. This
may be removed if we have enough time to convert the dependency into an
optional one.
---------
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Optional, Type
|
|
|
|
from langchain_core.callbacks import (
|
|
AsyncCallbackManagerForToolRun,
|
|
CallbackManagerForToolRun,
|
|
)
|
|
from pydantic import BaseModel
|
|
|
|
from langchain_community.tools.playwright.base import BaseBrowserTool
|
|
from langchain_community.tools.playwright.utils import (
|
|
aget_current_page,
|
|
get_current_page,
|
|
)
|
|
|
|
|
|
class NavigateBackTool(BaseBrowserTool):
|
|
"""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
|
|
|
|
def _run(self, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
|
|
"""Use the tool."""
|
|
if self.sync_browser is None:
|
|
raise ValueError(f"Synchronous browser not provided to {self.name}")
|
|
page = get_current_page(self.sync_browser)
|
|
response = page.go_back()
|
|
|
|
if response:
|
|
return (
|
|
f"Navigated back to the previous page with URL '{response.url}'."
|
|
f" Status code {response.status}"
|
|
)
|
|
else:
|
|
return "Unable to navigate back; no previous page in the history"
|
|
|
|
async def _arun(
|
|
self,
|
|
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the tool."""
|
|
if self.async_browser is None:
|
|
raise ValueError(f"Asynchronous browser not provided to {self.name}")
|
|
page = await aget_current_page(self.async_browser)
|
|
response = await page.go_back()
|
|
|
|
if response:
|
|
return (
|
|
f"Navigated back to the previous page with URL '{response.url}'."
|
|
f" Status code {response.status}"
|
|
)
|
|
else:
|
|
return "Unable to navigate back; no previous page in the history"
|