From 55577031d556c613daae9ed174fd6c02f3d0dcaa Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 20 Sep 2023 14:52:50 +0200 Subject: Add check_running_loop requirement Add create_async function in ChatCompletion Use SelectorEventLoop on windows --- g4f/Provider/base_provider.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'g4f/Provider/base_provider.py') diff --git a/g4f/Provider/base_provider.py b/g4f/Provider/base_provider.py index 79f8f617..9d45aa44 100644 --- a/g4f/Provider/base_provider.py +++ b/g4f/Provider/base_provider.py @@ -25,6 +25,7 @@ class BaseProvider(ABC): raise NotImplementedError() + @classmethod @property def params(cls): @@ -46,6 +47,8 @@ class AsyncProvider(BaseProvider): stream: bool = False, **kwargs ) -> CreateResult: + check_running_loop() + yield asyncio.run(cls.create_async(model, messages, **kwargs)) @staticmethod @@ -67,10 +70,17 @@ class AsyncGeneratorProvider(AsyncProvider): stream: bool = True, **kwargs ) -> CreateResult: - loop = asyncio.new_event_loop() + check_running_loop() + + # Force use selector event loop on windows + loop = asyncio.SelectorEventLoop() try: - asyncio.set_event_loop(loop) - generator = cls.create_async_generator(model, messages, stream=stream, **kwargs) + generator = cls.create_async_generator( + model, + messages, + stream=stream, + **kwargs + ) gen = generator.__aiter__() while True: try: @@ -78,10 +88,8 @@ class AsyncGeneratorProvider(AsyncProvider): except StopAsyncIteration: break finally: - asyncio.set_event_loop(None) loop.close() - @classmethod async def create_async( cls, @@ -100,6 +108,11 @@ class AsyncGeneratorProvider(AsyncProvider): ) -> AsyncGenerator: raise NotImplementedError() +# Don't create a new loop in a running loop +def check_running_loop(): + if asyncio.events._get_running_loop() is not None: + raise RuntimeError( + 'Use "create_async" instead of "create" function in a async loop.') _cookies = {} -- cgit v1.2.3