File size: 20,238 Bytes
a4b70d9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
from __future__ import annotations
import asyncio
import random
from asyncio import AbstractEventLoop
from concurrent.futures import ThreadPoolExecutor
from abc import abstractmethod
import json
from inspect import signature, Parameter
from typing import Optional, _GenericAlias
from pathlib import Path
try:
from types import NoneType
except ImportError:
NoneType = type(None)
from ..typing import CreateResult, AsyncResult, Messages
from .types import BaseProvider
from .asyncio import get_running_loop, to_sync_generator, to_async_iterator
from .response import BaseConversation, AuthResult
from .helper import concat_chunks
from ..cookies import get_cookies_dir
from ..errors import ModelNotFoundError, ResponseError, MissingAuthError, NoValidHarFileError, PaymentRequiredError, CloudflareError
from ..tools.run_tools import AuthManager
from .. import debug
SAFE_PARAMETERS = [
"model", "messages", "stream", "timeout",
"proxy", "media", "response_format",
"prompt", "negative_prompt", "tools", "conversation",
"history_disabled",
"temperature", "top_k", "top_p",
"frequency_penalty", "presence_penalty",
"max_tokens", "stop",
"api_key", "api_base", "seed", "width", "height",
"max_retries", "web_search", "cache",
"guidance_scale", "num_inference_steps", "randomize_seed",
"safe", "enhance", "private", "aspect_ratio", "n", "transparent"
]
BASIC_PARAMETERS = {
"provider": None,
"model": "",
"messages": [],
"stream": False,
"timeout": 0,
"response_format": None,
"max_tokens": 4096,
"stop": ["stop1", "stop2"],
}
PARAMETER_EXAMPLES = {
"proxy": "http://user:password@127.0.0.1:3128",
"temperature": 1,
"top_k": 1,
"top_p": 1,
"frequency_penalty": 1,
"presence_penalty": 1,
"messages": [{"role": "system", "content": ""}, {"role": "user", "content": ""}],
"media": [["data:image/jpeg;base64,...", "filename.jpg"]],
"response_format": {"type": "json_object"},
"conversation": {"conversation_id": "550e8400-e29b-11d4-a716-...", "message_id": "550e8400-e29b-11d4-a716-..."},
"seed": 42,
"tools": [],
"width": 1024,
"height": 1024,
}
class AbstractProvider(BaseProvider):
@classmethod
@abstractmethod
def create_completion(
cls,
model: str,
messages: Messages,
**kwargs
) -> CreateResult:
"""
Create a completion with the given parameters.
Args:
model (str): The model to use.
messages (Messages): The messages to process.
stream (bool): Whether to use streaming.
**kwargs: Additional keyword arguments.
Returns:
CreateResult: The result of the creation process.
"""
raise NotImplementedError()
@classmethod
async def create_async(
cls,
model: str,
messages: Messages,
*,
timeout: int = None,
loop: AbstractEventLoop = None,
executor: ThreadPoolExecutor = None,
**kwargs
) -> str:
"""
Asynchronously creates a result based on the given model and messages.
Args:
cls (type): The class on which this method is called.
model (str): The model to use for creation.
messages (Messages): The messages to process.
loop (AbstractEventLoop, optional): The event loop to use. Defaults to None.
executor (ThreadPoolExecutor, optional): The executor for running async tasks. Defaults to None.
**kwargs: Additional keyword arguments.
Returns:
str: The created result as a string.
"""
loop = asyncio.get_running_loop() if loop is None else loop
def create_func() -> str:
return concat_chunks(cls.create_completion(model, messages, **kwargs))
return await asyncio.wait_for(
loop.run_in_executor(executor, create_func),
timeout=timeout
)
@classmethod
def create_function(cls, *args, **kwargs) -> CreateResult:
"""
Creates a completion using the synchronous method.
Args:
**kwargs: Additional keyword arguments.
Returns:
CreateResult: The result of the completion creation.
"""
return cls.create_completion(*args, **kwargs)
@classmethod
def async_create_function(cls, *args, **kwargs) -> AsyncResult:
"""
Creates a completion using the synchronous method.
Args:
**kwargs: Additional keyword arguments.
Returns:
CreateResult: The result of the completion creation.
"""
return cls.create_async(*args, **kwargs)
@classmethod
def get_parameters(cls, as_json: bool = False) -> dict[str, Parameter]:
params = {name: parameter for name, parameter in signature(
cls.create_async_generator if issubclass(cls, AsyncGeneratorProvider) else
cls.create_async if issubclass(cls, AsyncProvider) else
cls.create_completion
).parameters.items() if name in SAFE_PARAMETERS
and (name != "stream" or cls.supports_stream)}
if as_json:
def get_type_as_var(annotation: type, key: str, default):
if key in PARAMETER_EXAMPLES:
if key == "messages" and not cls.supports_system_message:
return [PARAMETER_EXAMPLES[key][-1]]
return PARAMETER_EXAMPLES[key]
if isinstance(annotation, type):
if issubclass(annotation, int):
return 0
elif issubclass(annotation, float):
return 0.0
elif issubclass(annotation, bool):
return False
elif issubclass(annotation, str):
return ""
elif issubclass(annotation, dict):
return {}
elif issubclass(annotation, list):
return []
elif issubclass(annotation, BaseConversation):
return {}
elif issubclass(annotation, NoneType):
return {}
elif annotation is None:
return None
elif annotation == "str" or annotation == "list[str]":
return default
elif isinstance(annotation, _GenericAlias):
if annotation.__origin__ is Optional:
return get_type_as_var(annotation.__args__[0])
else:
return str(annotation)
return { name: (
param.default
if isinstance(param, Parameter) and param.default is not Parameter.empty and param.default is not None
else get_type_as_var(param.annotation, name, param.default) if isinstance(param, Parameter) else param
) for name, param in {
**BASIC_PARAMETERS,
**params,
**{"provider": cls.__name__, "model": getattr(cls, "default_model", ""), "stream": cls.supports_stream},
}.items()}
return params
@classmethod
@property
def params(cls) -> str:
"""
Returns the parameters supported by the provider.
Args:
cls (type): The class on which this property is called.
Returns:
str: A string listing the supported parameters.
"""
def get_type_name(annotation: type) -> str:
return getattr(annotation, "__name__", str(annotation)) if annotation is not Parameter.empty else ""
args = ""
for name, param in cls.get_parameters().items():
args += f"\n {name}"
args += f": {get_type_name(param.annotation)}"
default_value = getattr(cls, "default_model", "") if name == "model" else param.default
default_value = f'"{default_value}"' if isinstance(default_value, str) else default_value
args += f" = {default_value}" if param.default is not Parameter.empty else ""
args += ","
return f"g4f.Provider.{cls.__name__} supports: ({args}\n)"
class AsyncProvider(AbstractProvider):
"""
Provides asynchronous functionality for creating completions.
"""
@classmethod
def create_completion(
cls,
model: str,
messages: Messages,
**kwargs
) -> CreateResult:
"""
Creates a completion result synchronously.
Args:
cls (type): The class on which this method is called.
model (str): The model to use for creation.
messages (Messages): The messages to process.
loop (AbstractEventLoop, optional): The event loop to use. Defaults to None.
**kwargs: Additional keyword arguments.
Returns:
CreateResult: The result of the completion creation.
"""
get_running_loop(check_nested=False)
yield asyncio.run(cls.create_async(model, messages, **kwargs))
@staticmethod
@abstractmethod
async def create_async(
model: str,
messages: Messages,
**kwargs
) -> str:
"""
Abstract method for creating asynchronous results.
Args:
model (str): The model to use for creation.
messages (Messages): The messages to process.
**kwargs: Additional keyword arguments.
Raises:
NotImplementedError: If this method is not overridden in derived classes.
Returns:
str: The created result as a string.
"""
raise NotImplementedError()
class AsyncGeneratorProvider(AbstractProvider):
"""
Provides asynchronous generator functionality for streaming results.
"""
supports_stream = True
use_stream_timeout = True
@classmethod
def create_completion(
cls,
model: str,
messages: Messages,
timeout: int = None,
stream_timeout: int = None,
**kwargs
) -> CreateResult:
"""
Creates a streaming completion result synchronously.
Args:
cls (type): The class on which this method is called.
model (str): The model to use for creation.
messages (Messages): The messages to process.
loop (AbstractEventLoop, optional): The event loop to use. Defaults to None.
**kwargs: Additional keyword arguments.
Returns:
CreateResult: The result of the streaming completion creation.
"""
return to_sync_generator(
cls.create_async_generator(model, messages, **kwargs),
timeout=stream_timeout if cls.use_stream_timeout is None else timeout,
)
@staticmethod
@abstractmethod
async def create_async_generator(
model: str,
messages: Messages,
**kwargs
) -> AsyncResult:
"""
Abstract method for creating an asynchronous generator.
Args:
model (str): The model to use for creation.
messages (Messages): The messages to process.
**kwargs: Additional keyword arguments.
Raises:
NotImplementedError: If this method is not overridden in derived classes.
Returns:
AsyncResult: An asynchronous generator yielding results.
"""
raise NotImplementedError()
@classmethod
async def async_create_function(cls, *args, **kwargs) -> AsyncResult:
"""
Creates a completion using the synchronous method.
Args:
**kwargs: Additional keyword arguments.
Returns:
CreateResult: The result of the completion creation.
"""
response = cls.create_async_generator(*args, **kwargs)
if "stream_timeout" in kwargs or "timeout" in kwargs:
while True:
try:
yield await asyncio.wait_for(
response.__anext__(),
timeout=kwargs.get("stream_timeout") if cls.use_stream_timeout else kwargs.get("timeout")
)
except StopAsyncIteration:
break
else:
async for chunk in response:
yield chunk
class ProviderModelMixin:
default_model: str = None
models: list[str] = []
model_aliases: dict[str, str] = {}
models_count: dict = {}
image_models: list = []
vision_models: list = []
video_models: list = []
audio_models: dict = {}
last_model: str = None
models_loaded: bool = False
@classmethod
def get_models(cls, api_key: str = None, **kwargs) -> list[str]:
if not cls.models and cls.default_model is not None:
cls.models = [cls.default_model]
if not cls.models_loaded and hasattr(cls, "get_cache_file"):
if cls.get_cache_file().exists():
cls.live += 1
elif not api_key:
api_key = AuthManager.load_api_key(cls)
if api_key:
cls.live += 1
cls.models_loaded = True
return cls.models
@classmethod
def get_model(cls, model: str, **kwargs) -> str:
if not model and cls.default_model is not None:
model = cls.default_model
if model in cls.model_aliases:
alias = cls.model_aliases[model]
if isinstance(alias, list):
selected_model = random.choice(alias)
debug.log(f"{cls.__name__}: Selected model '{selected_model}' from alias '{model}'")
return selected_model
debug.log(f"{cls.__name__}: Using model '{alias}' for alias '{model}'")
return alias
if model not in cls.model_aliases.values():
if model not in cls.get_models(**kwargs) and cls.models:
raise ModelNotFoundError(f"Model not found: {model} in: {cls.__name__} Valid models: {cls.models}")
cls.last_model = model
return model
class RaiseErrorMixin():
@staticmethod
def raise_error(data: dict, status: int = None):
if "error_message" in data:
raise ResponseError(data["error_message"])
elif "error" in data:
if isinstance(data["error"], str):
if status is not None:
if status == 401:
raise MissingAuthError(f"Error {status}: {data['error']}")
elif status == 402:
raise PaymentRequiredError(f"Error {status}: {data['error']}")
raise ResponseError(f"Error {status}: {data['error']}")
raise ResponseError(data["error"])
elif isinstance(data["error"], bool):
raise ResponseError(data)
elif "code" in data["error"]:
raise ResponseError("\n".join(
[e for e in [f'Error {data["error"]["code"]}: {data["error"]["message"]}', data["error"].get("failed_generation")] if e is not None]
))
elif "message" in data["error"]:
raise ResponseError(data["error"]["message"])
else:
raise ResponseError(data["error"])
#elif ("choices" not in data or not data["choices"]) and "data" not in data:
# raise ResponseError(f"Invalid response: {json.dumps(data)}")
class AuthFileMixin():
@classmethod
def get_cache_file(cls) -> Path:
return Path(get_cookies_dir()) / f"auth_{cls.parent if hasattr(cls, 'parent') else cls.__name__}.json"
class AsyncAuthedProvider(AsyncGeneratorProvider, AuthFileMixin):
@classmethod
async def on_auth_async(cls, **kwargs) -> AuthResult:
if "api_key" not in kwargs:
raise MissingAuthError(f"API key is required for {cls.__name__}")
return AuthResult()
@classmethod
def on_auth(cls, **kwargs) -> AuthResult:
auth_result = cls.on_auth_async(**kwargs)
if hasattr(auth_result, "__aiter__"):
return to_sync_generator(auth_result)
return asyncio.run(auth_result)
@classmethod
def write_cache_file(cls, cache_file: Path, auth_result: AuthResult = None):
if auth_result is not None:
cache_file.parent.mkdir(parents=True, exist_ok=True)
try:
def toJSON(obj):
if hasattr(obj, "get_dict"):
return obj.get_dict()
return str(obj)
with cache_file.open("w") as cache_file:
json.dump(auth_result, cache_file, default=toJSON)
except TypeError as e:
raise RuntimeError(f"Failed to save: {auth_result.get_dict()}\n{type(e).__name__}: {e}")
# elif cache_file.exists():
# cache_file.unlink()
@classmethod
def get_auth_result(cls) -> AuthResult:
"""
Retrieves the authentication result from cache.
"""
cache_file = cls.get_cache_file()
if cache_file.exists():
try:
with cache_file.open("r") as f:
return AuthResult(**json.load(f))
except json.JSONDecodeError:
cache_file.unlink()
raise MissingAuthError(f"Invalid auth file: {cache_file}")
else:
raise MissingAuthError
@classmethod
def create_completion(
cls,
model: str,
messages: Messages,
**kwargs
) -> CreateResult:
auth_result: AuthResult = None
cache_file = cls.get_cache_file()
try:
auth_result = cls.get_auth_result()
yield from to_sync_generator(cls.create_authed(model, messages, auth_result, **kwargs))
except (MissingAuthError, NoValidHarFileError, CloudflareError):
response = cls.on_auth(**kwargs)
for chunk in response:
if isinstance(chunk, AuthResult):
auth_result = chunk
else:
yield chunk
for chunk in to_sync_generator(cls.create_authed(model, messages, auth_result, **kwargs), kwargs.get("stream_timeout", kwargs.get("timeout"))):
if cache_file is not None:
cls.write_cache_file(cache_file, auth_result)
cache_file = None
yield chunk
@classmethod
async def create_async_generator(
cls,
model: str,
messages: Messages,
**kwargs
) -> AsyncResult:
auth_result: AuthResult = None
cache_file = cls.get_cache_file()
try:
auth_result = cls.get_auth_result()
response = to_async_iterator(cls.create_authed(model, messages, **kwargs, auth_result=auth_result))
if "stream_timeout" in kwargs or "timeout" in kwargs:
while True:
try:
yield await asyncio.wait_for(
response.__anext__(),
timeout=kwargs.get("stream_timeout") if cls.use_stream_timeout else kwargs.get("timeout")
)
except StopAsyncIteration:
break
else:
async for chunk in response:
yield chunk
except (MissingAuthError, NoValidHarFileError, CloudflareError):
# if cache_file.exists():
# cache_file.unlink()
response = cls.on_auth_async(**kwargs)
async for chunk in response:
if isinstance(chunk, AuthResult):
auth_result = chunk
else:
yield chunk
response = to_async_iterator(cls.create_authed(model, messages, **kwargs, auth_result=auth_result))
async for chunk in response:
if cache_file is not None:
cls.write_cache_file(cache_file, auth_result)
cache_file = None
yield chunk
|