summaryrefslogtreecommitdiffstats
path: root/g4f/client/async_client.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-04-07 11:27:26 +0200
committerGitHub <noreply@github.com>2024-04-07 11:27:26 +0200
commitd327afc60620913f5d2b0a9985b03a7934468ad4 (patch)
tree395de9142af3e6b9c0e5e3968ee7f8234b8b25e2 /g4f/client/async_client.py
parentUpdate Gemini.py (diff)
parentUpdate provider.py (diff)
downloadgpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.gz
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.bz2
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.lz
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.xz
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.zst
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.zip
Diffstat (limited to '')
-rw-r--r--g4f/client/async_client.py (renamed from g4f/client/async.py)104
1 files changed, 54 insertions, 50 deletions
diff --git a/g4f/client/async.py b/g4f/client/async_client.py
index 76e410fc..25de1c76 100644
--- a/g4f/client/async.py
+++ b/g4f/client/async_client.py
@@ -1,20 +1,21 @@
from __future__ import annotations
-import re
-import os
import time
import random
import string
from .types import Client as BaseClient
-from .types import BaseProvider, ProviderType, FinishReason
-from .stubs import ChatCompletion, ChatCompletionChunk, Image, ImagesResponse
-from ..typing import Union, Iterator, Messages, ImageType, AsyncIerator
+from .types import ProviderType, FinishReason
+from .stubs import ChatCompletion, ChatCompletionChunk, ImagesResponse, Image
+from .types import AsyncIterResponse, ImageProvider
+from .image_models import ImageModels
+from .helper import filter_json, find_stop, filter_none, cast_iter_async
+from .service import get_last_provider, get_model_and_provider
+from ..typing import Union, Iterator, Messages, AsyncIterator, ImageType
+from ..errors import NoImageResponseError
from ..image import ImageResponse as ImageProviderResponse
-from ..errors import NoImageResponseError, RateLimitError, MissingAuthError
-from .. import get_model_and_provider, get_last_provider
-from .helper import read_json, find_stop, filter_none
-รค
+from ..providers.base_provider import AsyncGeneratorProvider
+
async def iter_response(
response: AsyncIterator[str],
stream: bool,
@@ -47,10 +48,10 @@ async def iter_response(
else:
if response_format is not None and "type" in response_format:
if response_format["type"] == "json_object":
- content = read_json(content)
+ content = filter_json(content)
yield ChatCompletion(content, finish_reason, completion_id, int(time.time()))
-async def iter_append_model_and_provider(response: AsyncIterResponse) -> IterResponse:
+async def iter_append_model_and_provider(response: AsyncIterResponse) -> AsyncIterResponse:
last_provider = None
async for chunk in response:
last_provider = get_last_provider(True) if last_provider is None else last_provider
@@ -58,51 +59,50 @@ async def iter_append_model_and_provider(response: AsyncIterResponse) -> IterRes
chunk.provider = last_provider.get("name")
yield chunk
-class Client(BaseClient):
+class AsyncClient(BaseClient):
def __init__(
self,
+ provider: ProviderType = None,
+ image_provider: ImageProvider = None,
**kwargs
):
super().__init__(**kwargs)
self.chat: Chat = Chat(self, provider)
self.images: Images = Images(self, image_provider)
-async def cast_iter_async(iter):
- for chunk in iter:
- yield chunk
-
def create_response(
messages: Messages,
model: str,
provider: ProviderType = None,
stream: bool = False,
- response_format: dict = None,
+ proxy: str = None,
max_tokens: int = None,
- stop: Union[list[str], str] = None,
+ stop: list[str] = None,
api_key: str = None,
**kwargs
):
- if hasattr(provider, "create_async_generator):
+ has_asnyc = isinstance(provider, type) and issubclass(provider, AsyncGeneratorProvider)
+ if has_asnyc:
create = provider.create_async_generator
else:
create = provider.create_completion
response = create(
model, messages, stream,
**filter_none(
- proxy=self.client.get_proxy(),
+ proxy=proxy,
max_tokens=max_tokens,
stop=stop,
- api_key=self.client.api_key if api_key is None else api_key
+ api_key=api_key
),
**kwargs
)
- if not hasattr(provider, "create_async_generator")
+ if not has_asnyc:
response = cast_iter_async(response)
return response
class Completions():
- def __init__(self, client: Client, provider: ProviderType = None):
- self.client: Client = client
+ def __init__(self, client: AsyncClient, provider: ProviderType = None):
+ self.client: AsyncClient = client
self.provider: ProviderType = provider
def create(
@@ -111,6 +111,10 @@ class Completions():
model: str,
provider: ProviderType = None,
stream: bool = False,
+ proxy: str = None,
+ max_tokens: int = None,
+ stop: Union[list[str], str] = None,
+ api_key: str = None,
response_format: dict = None,
ignored : list[str] = None,
ignore_working: bool = False,
@@ -123,11 +127,18 @@ class Completions():
stream,
ignored,
ignore_working,
- ignore_stream,
- **kwargs
+ ignore_stream
)
stop = [stop] if isinstance(stop, str) else stop
- response = create_response(messages, model, provider, stream, **kwargs)
+ response = create_response(
+ messages, model,
+ provider, stream,
+ proxy=self.client.get_proxy() if proxy is None else proxy,
+ max_tokens=max_tokens,
+ stop=stop,
+ api_key=self.client.api_key if api_key is None else api_key
+ **kwargs
+ )
response = iter_response(response, stream, response_format, max_tokens, stop)
response = iter_append_model_and_provider(response)
return response if stream else anext(response)
@@ -135,44 +146,40 @@ class Completions():
class Chat():
completions: Completions
- def __init__(self, client: Client, provider: ProviderType = None):
+ def __init__(self, client: AsyncClient, provider: ProviderType = None):
self.completions = Completions(client, provider)
async def iter_image_response(response: Iterator) -> Union[ImagesResponse, None]:
- async for chunk in list(response):
+ async for chunk in response:
if isinstance(chunk, ImageProviderResponse):
return ImagesResponse([Image(image) for image in chunk.get_list()])
-def create_image(client: Client, provider: ProviderType, prompt: str, model: str = "", **kwargs) -> AsyncIterator:
+def create_image(client: AsyncClient, provider: ProviderType, prompt: str, model: str = "", **kwargs) -> AsyncIterator:
prompt = f"create a image with: {prompt}"
+ if provider.__name__ == "You":
+ kwargs["chat_mode"] = "create"
return provider.create_async_generator(
model,
[{"role": "user", "content": prompt}],
- True,
+ stream=True,
proxy=client.get_proxy(),
**kwargs
)
class Images():
- def __init__(self, client: Client, provider: ImageProvider = None):
- self.client: Client = client
+ def __init__(self, client: AsyncClient, provider: ImageProvider = None):
+ self.client: AsyncClient = client
self.provider: ImageProvider = provider
self.models: ImageModels = ImageModels(client)
- async def generate(self, prompt, model: str = None, **kwargs) -> ImagesResponse:
+ async def generate(self, prompt, model: str = "", **kwargs) -> ImagesResponse:
provider = self.models.get(model, self.provider)
- if isinstance(provider, type) and issubclass(provider, BaseProvider):
+ if isinstance(provider, type) and issubclass(provider, AsyncGeneratorProvider):
response = create_image(self.client, provider, prompt, **kwargs)
else:
- try:
- response = list(provider.create(prompt))
- except (RateLimitError, MissingAuthError) as e:
- # Fallback for default provider
- if self.provider is None:
- response = create_image(self.client, self.models.you, prompt, model or "dall-e", **kwargs)
- else:
- raise e
- image = iter_image_response(response)
+ response = await provider.create_async(prompt)
+ return ImagesResponse([Image(image) for image in response.get_list()])
+ image = await iter_image_response(response)
if image is None:
raise NoImageResponseError()
return image
@@ -180,7 +187,7 @@ class Images():
async def create_variation(self, image: ImageType, model: str = None, **kwargs):
provider = self.models.get(model, self.provider)
result = None
- if isinstance(provider, type) and issubclass(provider, BaseProvider):
+ if isinstance(provider, type) and issubclass(provider, AsyncGeneratorProvider):
response = provider.create_async_generator(
"",
[{"role": "user", "content": "create a image like this"}],
@@ -189,10 +196,7 @@ class Images():
proxy=self.client.get_proxy(),
**kwargs
)
- async for chunk in response:
- if isinstance(chunk, ImageProviderResponse):
- result = ([chunk.images] if isinstance(chunk.images, str) else chunk.images)
- result = ImagesResponse([Image(image)for image in result])
+ result = iter_image_response(response)
if result is None:
raise NoImageResponseError()
- return result
+ return result \ No newline at end of file