diff options
author | Heiner Lohaus <heiner@lohaus.eu> | 2023-09-26 01:02:02 +0200 |
---|---|---|
committer | Heiner Lohaus <heiner@lohaus.eu> | 2023-09-26 01:02:02 +0200 |
commit | 72c3ff7a25630582844cdd6a82055fdd2aff6c60 (patch) | |
tree | 1a31086e1f8240f9bd67d3a16afc54753a8f5f93 /g4f/Provider/base_provider.py | |
parent | "create_async" support for BaseProvider, (diff) | |
download | gpt4free-72c3ff7a25630582844cdd6a82055fdd2aff6c60.tar gpt4free-72c3ff7a25630582844cdd6a82055fdd2aff6c60.tar.gz gpt4free-72c3ff7a25630582844cdd6a82055fdd2aff6c60.tar.bz2 gpt4free-72c3ff7a25630582844cdd6a82055fdd2aff6c60.tar.lz gpt4free-72c3ff7a25630582844cdd6a82055fdd2aff6c60.tar.xz gpt4free-72c3ff7a25630582844cdd6a82055fdd2aff6c60.tar.zst gpt4free-72c3ff7a25630582844cdd6a82055fdd2aff6c60.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/base_provider.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/g4f/Provider/base_provider.py b/g4f/Provider/base_provider.py index ea81502f..e8a54f78 100644 --- a/g4f/Provider/base_provider.py +++ b/g4f/Provider/base_provider.py @@ -1,7 +1,9 @@ from __future__ import annotations import asyncio -from asyncio import SelectorEventLoop +import functools +from asyncio import SelectorEventLoop, AbstractEventLoop +from concurrent.futures import ThreadPoolExecutor from abc import ABC, abstractmethod import browser_cookie3 @@ -27,6 +29,31 @@ class BaseProvider(ABC): ) -> CreateResult: raise NotImplementedError() + @classmethod + async def create_async( + cls, + model: str, + messages: list[dict[str, str]], + *, + loop: AbstractEventLoop = None, + executor: ThreadPoolExecutor = None, + **kwargs + ) -> str: + if not loop: + loop = asyncio.get_event_loop() + + partial_func = functools.partial( + cls.create_completion, + model, + messages, + False, + **kwargs + ) + response = await loop.run_in_executor( + executor, + partial_func + ) + return "".join(response) @classmethod @property @@ -127,7 +154,7 @@ def create_event_loop() -> SelectorEventLoop: except RuntimeError: return SelectorEventLoop() raise RuntimeError( - 'Use "create_async" instead of "create" function in a async loop.') + 'Use "create_async" instead of "create" function in a running event loop.') _cookies = {} |