From cb0e065581423460663c58d1d169c54f0faa3084 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 20 Jan 2024 18:23:54 +0100 Subject: Remove usage of get_event_loop helper --- g4f/Provider/base_provider.py | 30 +++++++++++++++++++++--------- g4f/errors.py | 3 +++ 2 files changed, 24 insertions(+), 9 deletions(-) (limited to 'g4f') diff --git a/g4f/Provider/base_provider.py b/g4f/Provider/base_provider.py index fd92d17a..95f1b0b2 100644 --- a/g4f/Provider/base_provider.py +++ b/g4f/Provider/base_provider.py @@ -8,6 +8,7 @@ from inspect import signature, Parameter from .helper import get_event_loop, get_cookies, format_prompt from ..typing import CreateResult, AsyncResult, Messages from ..base_provider import BaseProvider +from ..errors import NestAsyncioError if sys.version_info < (3, 10): NoneType = type(None) @@ -48,7 +49,7 @@ class AbstractProvider(BaseProvider): Returns: str: The created result as a string. """ - loop = loop or get_event_loop() + loop = loop or asyncio.get_running_loop() def create_func() -> str: return "".join(cls.create_completion(model, messages, False, **kwargs)) @@ -101,8 +102,6 @@ class AsyncProvider(AbstractProvider): model: str, messages: Messages, stream: bool = False, - *, - loop: AbstractEventLoop = None, **kwargs ) -> CreateResult: """ @@ -119,9 +118,15 @@ class AsyncProvider(AbstractProvider): Returns: CreateResult: The result of the completion creation. """ - loop = loop or get_event_loop() - coro = cls.create_async(model, messages, **kwargs) - yield loop.run_until_complete(coro) + try: + loop = asyncio.get_running_loop() + if not hasattr(loop.__class__, "_nest_patched"): + raise NestAsyncioError( + 'Use "create_async" instead of "create" function in a running event loop. Or use "nest_asyncio" package.' + ) + except RuntimeError: + pass + yield asyncio.run(cls.create_async(model, messages, **kwargs)) @staticmethod @abstractmethod @@ -159,8 +164,6 @@ class AsyncGeneratorProvider(AsyncProvider): model: str, messages: Messages, stream: bool = True, - *, - loop: AbstractEventLoop = None, **kwargs ) -> CreateResult: """ @@ -177,7 +180,16 @@ class AsyncGeneratorProvider(AsyncProvider): Returns: CreateResult: The result of the streaming completion creation. """ - loop = loop or get_event_loop() + try: + loop = asyncio.get_running_loop() + if not hasattr(loop.__class__, "_nest_patched"): + raise NestAsyncioError( + 'Use "create_async" instead of "create" function in a running event loop. Or use "nest_asyncio" package.' + ) + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + generator = cls.create_async_generator(model, messages, stream=stream, **kwargs) gen = generator.__aiter__() diff --git a/g4f/errors.py b/g4f/errors.py index b874435a..c0e6ddfc 100644 --- a/g4f/errors.py +++ b/g4f/errors.py @@ -23,4 +23,7 @@ class RetryNoProviderError(Exception): pass class VersionNotFoundError(Exception): + pass + +class NestAsyncioError(Exception): pass \ No newline at end of file -- cgit v1.2.3 From 8787db385eec227f2c101544363d02d2d285cc24 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 20 Jan 2024 18:36:04 +0100 Subject: Check access from chromedriver --- g4f/webdriver.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'g4f') diff --git a/g4f/webdriver.py b/g4f/webdriver.py index 9a83215f..85d6d695 100644 --- a/g4f/webdriver.py +++ b/g4f/webdriver.py @@ -6,6 +6,7 @@ from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from os import path +from os import access, R_OK from . import debug try: @@ -40,8 +41,9 @@ def get_browser( options = ChromeOptions() if proxy: options.add_argument(f'--proxy-server={proxy}') + # Check for system driver in docker driver = '/usr/bin/chromedriver' - if not path.isfile(driver): + if not path.isfile(driver) or not access(driver, R_OK): driver = None return Chrome( options=options, -- cgit v1.2.3