2024-04-07 08:36:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from .stubs import ChatCompletion, ChatCompletionChunk
|
2024-04-06 18:41:30 +00:00
|
|
|
from ..providers.types import BaseProvider, ProviderType, FinishReason
|
2024-04-07 08:36:13 +00:00
|
|
|
from typing import Union, Iterator, AsyncIterator
|
2024-04-06 18:41:30 +00:00
|
|
|
|
2024-04-06 18:31:13 +00:00
|
|
|
ImageProvider = Union[BaseProvider, object]
|
|
|
|
Proxies = Union[dict, str]
|
|
|
|
IterResponse = Iterator[Union[ChatCompletion, ChatCompletionChunk]]
|
2024-04-07 08:36:13 +00:00
|
|
|
AsyncIterResponse = AsyncIterator[Union[ChatCompletion, ChatCompletionChunk]]
|
2024-04-06 18:41:30 +00:00
|
|
|
|
2024-04-06 19:15:07 +00:00
|
|
|
class ClientProxyMixin():
|
|
|
|
def get_proxy(self) -> Union[str, 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"]
|
|
|
|
|
|
|
|
class Client(ClientProxyMixin):
|
2024-04-06 18:41:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
api_key: str = None,
|
|
|
|
proxies: Proxies = None,
|
|
|
|
**kwargs
|
|
|
|
) -> None:
|
|
|
|
self.api_key: str = api_key
|
2024-04-07 08:36:13 +00:00
|
|
|
self.proxies: Proxies = proxies
|