summaryrefslogtreecommitdiffstats
path: root/g4f/Provider
diff options
context:
space:
mode:
authorHeiner Lohaus <heiner@lohaus.eu>2023-09-20 14:52:50 +0200
committerHeiner Lohaus <heiner@lohaus.eu>2023-09-20 14:52:50 +0200
commit55577031d556c613daae9ed174fd6c02f3d0dcaa (patch)
tree5765977cb5262f5f478a8e319e6d6530b7ffd8fd /g4f/Provider
parentCache "snlm0e" in Bard (diff)
downloadgpt4free-55577031d556c613daae9ed174fd6c02f3d0dcaa.tar
gpt4free-55577031d556c613daae9ed174fd6c02f3d0dcaa.tar.gz
gpt4free-55577031d556c613daae9ed174fd6c02f3d0dcaa.tar.bz2
gpt4free-55577031d556c613daae9ed174fd6c02f3d0dcaa.tar.lz
gpt4free-55577031d556c613daae9ed174fd6c02f3d0dcaa.tar.xz
gpt4free-55577031d556c613daae9ed174fd6c02f3d0dcaa.tar.zst
gpt4free-55577031d556c613daae9ed174fd6c02f3d0dcaa.zip
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 = {}