summaryrefslogtreecommitdiffstats
path: root/g4f/client
diff options
context:
space:
mode:
authorkqlio67 <kqlio67@users.noreply.github.com>2024-10-30 11:35:55 +0100
committerkqlio67 <kqlio67@users.noreply.github.com>2024-10-30 11:35:55 +0100
commit8adaf3320206cc5bf45d49cb06d1a1111a2e24be (patch)
treee7ddc15fbd65755f8f6c6d09da4599ca3c7bd995 /g4f/client
parentnew discord link (diff)
downloadgpt4free-8adaf3320206cc5bf45d49cb06d1a1111a2e24be.tar
gpt4free-8adaf3320206cc5bf45d49cb06d1a1111a2e24be.tar.gz
gpt4free-8adaf3320206cc5bf45d49cb06d1a1111a2e24be.tar.bz2
gpt4free-8adaf3320206cc5bf45d49cb06d1a1111a2e24be.tar.lz
gpt4free-8adaf3320206cc5bf45d49cb06d1a1111a2e24be.tar.xz
gpt4free-8adaf3320206cc5bf45d49cb06d1a1111a2e24be.tar.zst
gpt4free-8adaf3320206cc5bf45d49cb06d1a1111a2e24be.zip
Diffstat (limited to 'g4f/client')
-rw-r--r--g4f/client/AsyncClient.py38
-rw-r--r--g4f/client/__init__.py3
2 files changed, 40 insertions, 1 deletions
diff --git a/g4f/client/AsyncClient.py b/g4f/client/AsyncClient.py
new file mode 100644
index 00000000..fd2cc353
--- /dev/null
+++ b/g4f/client/AsyncClient.py
@@ -0,0 +1,38 @@
+from __future__ import annotations
+
+from .Client import Client, Chat, Images, Completions
+from .Client import async_iter_response, async_iter_append_model_and_provider
+from aiohttp import ClientSession
+from typing import Union, AsyncIterator
+
+class AsyncClient(Client):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.chat = AsyncChat(self)
+ self._images = AsyncImages(self)
+
+ @property
+ def images(self) -> 'AsyncImages':
+ return self._images
+
+class AsyncCompletions(Completions):
+ async def async_create(self, *args, **kwargs) -> Union['ChatCompletion', AsyncIterator['ChatCompletionChunk']]:
+ response = await super().async_create(*args, **kwargs)
+ async for result in response:
+ return result
+
+class AsyncChat(Chat):
+ def __init__(self, client: AsyncClient):
+ self.completions = AsyncCompletions(client)
+
+class AsyncImages(Images):
+ async def async_generate(self, *args, **kwargs) -> 'ImagesResponse':
+ return await super().async_generate(*args, **kwargs)
+
+ async def _fetch_image(self, url: str) -> bytes:
+ async with ClientSession() as session:
+ async with session.get(url) as resp:
+ if resp.status == 200:
+ return await resp.read()
+ else:
+ raise Exception(f"Failed to fetch image from {url}, status code {resp.status}")
diff --git a/g4f/client/__init__.py b/g4f/client/__init__.py
index d1e7e298..0d4685cc 100644
--- a/g4f/client/__init__.py
+++ b/g4f/client/__init__.py
@@ -1,2 +1,3 @@
from .stubs import ChatCompletion, ChatCompletionChunk, ImagesResponse
-from .client import Client, AsyncClient
+from .client import Client
+from .client import AsyncClient