diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-02-23 03:08:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-23 03:08:30 +0100 |
commit | cf87d467c927d4b93916a42e7139f4e801172d62 (patch) | |
tree | 175b39f8486ce72be00a40cc345834abe859c84c /g4f/client.py | |
parent | ~ (diff) | |
parent | Fix unittests, use Union typing (diff) | |
download | gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.gz gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.bz2 gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.lz gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.xz gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.tar.zst gpt4free-cf87d467c927d4b93916a42e7139f4e801172d62.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/client.py | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/g4f/client.py b/g4f/client.py index a1494d47..023d53f6 100644 --- a/g4f/client.py +++ b/g4f/client.py @@ -1,17 +1,23 @@ from __future__ import annotations import re +import os +import time +import random +import string from .stubs import ChatCompletion, ChatCompletionChunk, Image, ImagesResponse from .typing import Union, Generator, Messages, ImageType -from .base_provider import BaseProvider, ProviderType +from .providers.types import BaseProvider, ProviderType from .image import ImageResponse as ImageProviderResponse -from .Provider import BingCreateImages, Gemini, OpenaiChat +from .Provider.BingCreateImages import BingCreateImages +from .Provider.needs_auth import Gemini, OpenaiChat from .errors import NoImageResponseError -from . import get_model_and_provider +from . import get_model_and_provider, get_last_provider ImageProvider = Union[BaseProvider, object] Proxies = Union[dict, str] +IterResponse = Generator[Union[ChatCompletion, ChatCompletionChunk], None, None] def read_json(text: str) -> dict: """ @@ -29,21 +35,19 @@ def read_json(text: str) -> dict: return text def iter_response( - response: iter, + response: iter[str], stream: bool, response_format: dict = None, max_tokens: int = None, stop: list = None -) -> Generator: +) -> IterResponse: content = "" finish_reason = None - last_chunk = None + completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28)) for idx, chunk in enumerate(response): - if last_chunk is not None: - yield ChatCompletionChunk(last_chunk, finish_reason) content += str(chunk) if max_tokens is not None and idx + 1 >= max_tokens: - finish_reason = "max_tokens" + finish_reason = "length" first = -1 word = None if stop is not None: @@ -61,16 +65,25 @@ def iter_response( if first != -1: finish_reason = "stop" if stream: - last_chunk = chunk + yield ChatCompletionChunk(chunk, None, completion_id, int(time.time())) if finish_reason is not None: break - if last_chunk is not None: - yield ChatCompletionChunk(last_chunk, finish_reason) - if not stream: + finish_reason = "stop" if finish_reason is None else finish_reason + if stream: + yield ChatCompletionChunk(None, finish_reason, completion_id, int(time.time())) + else: if response_format is not None and "type" in response_format: if response_format["type"] == "json_object": - response = read_json(response) - yield ChatCompletion(content, finish_reason) + content = read_json(content) + yield ChatCompletion(content, finish_reason, completion_id, int(time.time())) + +def iter_append_model_and_provider(response: IterResponse) -> IterResponse: + last_provider = None + for chunk in response: + last_provider = get_last_provider(True) if last_provider is None else last_provider + chunk.model = last_provider.get("model") + chunk.provider = last_provider.get("name") + yield chunk class Client(): proxies: Proxies = None @@ -89,13 +102,14 @@ class Client(): self.proxies: Proxies = proxies def get_proxy(self) -> Union[str, None]: - if isinstance(self.proxies, str) or self.proxies is None: + if isinstance(self.proxies, str): return self.proxies + elif self.proxies is None: + return os.environ.get("G4F_PROXY") elif "all" in self.proxies: return self.proxies["all"] elif "https" in self.proxies: return self.proxies["https"] - return None class Completions(): def __init__(self, client: Client, provider: ProviderType = None): @@ -110,7 +124,7 @@ class Completions(): stream: bool = False, response_format: dict = None, max_tokens: int = None, - stop: Union[list. str] = None, + stop: Union[list[str], str] = None, **kwargs ) -> Union[ChatCompletion, Generator[ChatCompletionChunk]]: if max_tokens is not None: @@ -123,9 +137,9 @@ class Completions(): stream, **kwargs ) - response = provider.create_completion(model, messages, stream=stream, **kwargs) + response = provider.create_completion(model, messages, stream=stream, proxy=self.client.get_proxy(), **kwargs) stop = [stop] if isinstance(stop, str) else stop - response = iter_response(response, stream, response_format, max_tokens, stop) + response = iter_append_model_and_provider(iter_response(response, stream, response_format, max_tokens, stop)) return response if stream else next(response) class Chat(): |