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
105 lines
3.0 KiB
Python
105 lines
3.0 KiB
Python
"""Utilities for the Playwright browser tools."""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from typing import TYPE_CHECKING, Any, Coroutine, List, Optional, TypeVar
|
|
|
|
if TYPE_CHECKING:
|
|
from playwright.async_api import Browser as AsyncBrowser
|
|
from playwright.async_api import Page as AsyncPage
|
|
from playwright.sync_api import Browser as SyncBrowser
|
|
from playwright.sync_api import Page as SyncPage
|
|
|
|
|
|
async def aget_current_page(browser: AsyncBrowser) -> AsyncPage:
|
|
"""
|
|
Asynchronously get the current page of the browser.
|
|
|
|
Args:
|
|
browser: The browser (AsyncBrowser) to get the current page from.
|
|
|
|
Returns:
|
|
AsyncPage: The current page.
|
|
"""
|
|
if not browser.contexts:
|
|
context = await browser.new_context()
|
|
return await context.new_page()
|
|
context = browser.contexts[0] # Assuming you're using the default browser context
|
|
if not context.pages:
|
|
return await context.new_page()
|
|
# Assuming the last page in the list is the active one
|
|
return context.pages[-1]
|
|
|
|
|
|
def get_current_page(browser: SyncBrowser) -> SyncPage:
|
|
"""
|
|
Get the current page of the browser.
|
|
Args:
|
|
browser: The browser to get the current page from.
|
|
|
|
Returns:
|
|
SyncPage: The current page.
|
|
"""
|
|
if not browser.contexts:
|
|
context = browser.new_context()
|
|
return context.new_page()
|
|
context = browser.contexts[0] # Assuming you're using the default browser context
|
|
if not context.pages:
|
|
return context.new_page()
|
|
# Assuming the last page in the list is the active one
|
|
return context.pages[-1]
|
|
|
|
|
|
def create_async_playwright_browser(
|
|
headless: bool = True, args: Optional[List[str]] = None
|
|
) -> AsyncBrowser:
|
|
"""
|
|
Create an async playwright browser.
|
|
|
|
Args:
|
|
headless: Whether to run the browser in headless mode. Defaults to True.
|
|
args: arguments to pass to browser.chromium.launch
|
|
|
|
Returns:
|
|
AsyncBrowser: The playwright browser.
|
|
"""
|
|
from playwright.async_api import async_playwright
|
|
|
|
browser = run_async(async_playwright().start())
|
|
return run_async(browser.chromium.launch(headless=headless, args=args))
|
|
|
|
|
|
def create_sync_playwright_browser(
|
|
headless: bool = True, args: Optional[List[str]] = None
|
|
) -> SyncBrowser:
|
|
"""
|
|
Create a playwright browser.
|
|
|
|
Args:
|
|
headless: Whether to run the browser in headless mode. Defaults to True.
|
|
args: arguments to pass to browser.chromium.launch
|
|
|
|
Returns:
|
|
SyncBrowser: The playwright browser.
|
|
"""
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
browser = sync_playwright().start()
|
|
return browser.chromium.launch(headless=headless, args=args)
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
def run_async(coro: Coroutine[Any, Any, T]) -> T:
|
|
"""Run an async coroutine.
|
|
|
|
Args:
|
|
coro: The coroutine to run. Coroutine[Any, Any, T]
|
|
|
|
Returns:
|
|
T: The result of the coroutine.
|
|
"""
|
|
event_loop = asyncio.get_event_loop()
|
|
return event_loop.run_until_complete(coro)
|