summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/base_provider.py
diff options
context:
space:
mode:
authorHeiner Lohaus <heiner@lohaus.eu>2023-09-20 17:31:25 +0200
committerHeiner Lohaus <heiner@lohaus.eu>2023-09-20 17:31:25 +0200
commitf90741c10b444abea918c7a0c58c6a1a7367b29c (patch)
tree0787760f5eb41d81fab190c63f2209b7187a78ce /g4f/Provider/base_provider.py
parentAdd "create_event_loop" helper (diff)
downloadgpt4free-f90741c10b444abea918c7a0c58c6a1a7367b29c.tar
gpt4free-f90741c10b444abea918c7a0c58c6a1a7367b29c.tar.gz
gpt4free-f90741c10b444abea918c7a0c58c6a1a7367b29c.tar.bz2
gpt4free-f90741c10b444abea918c7a0c58c6a1a7367b29c.tar.lz
gpt4free-f90741c10b444abea918c7a0c58c6a1a7367b29c.tar.xz
gpt4free-f90741c10b444abea918c7a0c58c6a1a7367b29c.tar.zst
gpt4free-f90741c10b444abea918c7a0c58c6a1a7367b29c.zip
Diffstat (limited to '')
-rw-r--r--g4f/Provider/base_provider.py32
1 files changed, 22 insertions, 10 deletions
diff --git a/g4f/Provider/base_provider.py b/g4f/Provider/base_provider.py
index 1e2d4c64..003f86ba 100644
--- a/g4f/Provider/base_provider.py
+++ b/g4f/Provider/base_provider.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
+from asyncio import SelectorEventLoop
from abc import ABC, abstractmethod
import browser_cookie3
@@ -57,7 +58,9 @@ class AsyncProvider(BaseProvider):
@abstractmethod
async def create_async(
model: str,
- messages: list[dict[str, str]], **kwargs: Any) -> str:
+ messages: list[dict[str, str]],
+ **kwargs
+ ) -> str:
raise NotImplementedError()
@@ -72,7 +75,7 @@ class AsyncGeneratorProvider(AsyncProvider):
stream: bool = True,
**kwargs
) -> CreateResult:
- loop = get_new_event_loop()
+ loop = create_event_loop()
try:
generator = cls.create_async_generator(
model,
@@ -96,7 +99,14 @@ class AsyncGeneratorProvider(AsyncProvider):
messages: list[dict[str, str]],
**kwargs
) -> str:
- return "".join([chunk async for chunk in cls.create_async_generator(model, messages, stream=False, **kwargs)])
+ return "".join([
+ chunk async for chunk in cls.create_async_generator(
+ model,
+ messages,
+ stream=False,
+ **kwargs
+ )
+ ])
@staticmethod
@abstractmethod
@@ -108,14 +118,16 @@ class AsyncGeneratorProvider(AsyncProvider):
raise NotImplementedError()
-def create_event_loop():
+def create_event_loop() -> SelectorEventLoop:
# Don't create a new loop in a running loop
- if asyncio.events._get_running_loop() is not None:
- raise RuntimeError(
- 'Use "create_async" instead of "create" function in a async loop.')
-
- # Force use selector event loop on windows
- return asyncio.SelectorEventLoop()
+ try:
+ if asyncio.get_running_loop() is not None:
+ raise RuntimeError(
+ 'Use "create_async" instead of "create" function in a async loop.')
+ except:
+ pass
+ # Force use selector event loop on windows and linux use it anyway
+ return SelectorEventLoop()
_cookies = {}