mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Optional, Type
|
|
from urllib.parse import urlparse
|
|
|
|
from langchain_core.callbacks import (
|
|
AsyncCallbackManagerForToolRun,
|
|
CallbackManagerForToolRun,
|
|
)
|
|
from langchain_core.pydantic_v1 import BaseModel, Field, validator
|
|
|
|
from langchain_community.tools.playwright.base import BaseBrowserTool
|
|
from langchain_community.tools.playwright.utils import (
|
|
aget_current_page,
|
|
get_current_page,
|
|
)
|
|
|
|
|
|
class NavigateToolInput(BaseModel):
|
|
"""Input for NavigateToolInput."""
|
|
|
|
url: str = Field(..., description="url to navigate to")
|
|
|
|
@validator("url")
|
|
def validate_url_scheme(cls, url: str) -> str:
|
|
"""Check that the URL scheme is valid."""
|
|
parsed_url = urlparse(url)
|
|
if parsed_url.scheme not in ("http", "https"):
|
|
raise ValueError("URL scheme must be 'http' or 'https'")
|
|
return url
|
|
|
|
|
|
class NavigateTool(BaseBrowserTool):
|
|
"""Tool for navigating a browser to a URL.
|
|
|
|
**Security Note**: This tool provides code to control web-browser navigation.
|
|
|
|
This tool can navigate to any URL, including internal network URLs, and
|
|
URLs exposed on the server itself.
|
|
|
|
However, if exposing this tool to end-users, consider limiting network
|
|
access to the server that hosts the agent.
|
|
|
|
By default, the URL scheme has been limited to 'http' and 'https' to
|
|
prevent navigation to local file system URLs (or other schemes).
|
|
|
|
If access to the local file system is required, consider creating a custom
|
|
tool or providing a custom args_schema that allows the desired URL schemes.
|
|
|
|
See https://python.langchain.com/docs/security for more information.
|
|
"""
|
|
|
|
name: str = "navigate_browser"
|
|
description: str = "Navigate a browser to the specified URL"
|
|
args_schema: Type[BaseModel] = NavigateToolInput
|
|
|
|
def _run(
|
|
self,
|
|
url: str,
|
|
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.goto(url)
|
|
status = response.status if response else "unknown"
|
|
return f"Navigating to {url} returned status code {status}"
|
|
|
|
async def _arun(
|
|
self,
|
|
url: str,
|
|
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.goto(url)
|
|
status = response.status if response else "unknown"
|
|
return f"Navigating to {url} returned status code {status}"
|