mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-16 07:10:56 -08:00
Improve code style in async support
This commit is contained in:
parent
587f4ad2c9
commit
f90741c10b
1 changed files with 22 additions and 10 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from asyncio import SelectorEventLoop
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
import browser_cookie3
|
import browser_cookie3
|
||||||
|
|
@ -57,7 +58,9 @@ class AsyncProvider(BaseProvider):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def create_async(
|
async def create_async(
|
||||||
model: str,
|
model: str,
|
||||||
messages: list[dict[str, str]], **kwargs: Any) -> str:
|
messages: list[dict[str, str]],
|
||||||
|
**kwargs
|
||||||
|
) -> str:
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -72,7 +75,7 @@ class AsyncGeneratorProvider(AsyncProvider):
|
||||||
stream: bool = True,
|
stream: bool = True,
|
||||||
**kwargs
|
**kwargs
|
||||||
) -> CreateResult:
|
) -> CreateResult:
|
||||||
loop = get_new_event_loop()
|
loop = create_event_loop()
|
||||||
try:
|
try:
|
||||||
generator = cls.create_async_generator(
|
generator = cls.create_async_generator(
|
||||||
model,
|
model,
|
||||||
|
|
@ -96,7 +99,14 @@ class AsyncGeneratorProvider(AsyncProvider):
|
||||||
messages: list[dict[str, str]],
|
messages: list[dict[str, str]],
|
||||||
**kwargs
|
**kwargs
|
||||||
) -> str:
|
) -> 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
|
@staticmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
|
@ -108,14 +118,16 @@ class AsyncGeneratorProvider(AsyncProvider):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
def create_event_loop():
|
def create_event_loop() -> SelectorEventLoop:
|
||||||
# Don't create a new loop in a running loop
|
# Don't create a new loop in a running loop
|
||||||
if asyncio.events._get_running_loop() is not None:
|
try:
|
||||||
raise RuntimeError(
|
if asyncio.get_running_loop() is not None:
|
||||||
'Use "create_async" instead of "create" function in a async loop.')
|
raise RuntimeError(
|
||||||
|
'Use "create_async" instead of "create" function in a async loop.')
|
||||||
# Force use selector event loop on windows
|
except:
|
||||||
return asyncio.SelectorEventLoop()
|
pass
|
||||||
|
# Force use selector event loop on windows and linux use it anyway
|
||||||
|
return SelectorEventLoop()
|
||||||
|
|
||||||
|
|
||||||
_cookies = {}
|
_cookies = {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue