summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/base_provider.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--g4f/Provider/base_provider.py23
1 files changed, 18 insertions, 5 deletions
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 = {}