diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-09-26 01:20:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-26 01:20:27 +0200 |
commit | fd5d28cf7b936855b36b540bb589219b84eea376 (patch) | |
tree | 65f3b7b5c3a15d4681cf07d1bb120489d61072b7 /g4f/Provider/base_provider.py | |
parent | Merge pull request #940 from hlohaus/myshell (diff) | |
parent | AItianhuSpace Provider with GPT 4 added (diff) | |
download | gpt4free-fd5d28cf7b936855b36b540bb589219b84eea376.tar gpt4free-fd5d28cf7b936855b36b540bb589219b84eea376.tar.gz gpt4free-fd5d28cf7b936855b36b540bb589219b84eea376.tar.bz2 gpt4free-fd5d28cf7b936855b36b540bb589219b84eea376.tar.lz gpt4free-fd5d28cf7b936855b36b540bb589219b84eea376.tar.xz gpt4free-fd5d28cf7b936855b36b540bb589219b84eea376.tar.zst gpt4free-fd5d28cf7b936855b36b540bb589219b84eea376.zip |
Diffstat (limited to 'g4f/Provider/base_provider.py')
-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 = {} |