gpt4free/g4f/Provider/__init__.py
H Lohaus 0a070bdf10
feat: introduce AnyProvider & LM Arena, overhaul model/provider logic (#2925)
* feat: introduce AnyProvider & LM Arena, overhaul model/provider logic

- **Provider additions & removals**
  - Added `Provider/LMArenaProvider.py` with full async stream implementation and vision model support
  - Registered `LMArenaProvider` in `Provider/__init__.py`; removed old `hf_space/LMArenaProvider.py`
  - Created `providers/any_provider.py`; registers `AnyProvider` dynamically in `Provider`
- **Provider framework enhancements**
  - `providers/base_provider.py`
    - Added `video_models` and `audio_models` attributes
  - `providers/retry_provider.py`
    - Introduced `is_content()` helper; now treats `AudioResponse` as stream content
- **Cloudflare provider refactor**
  - `Provider/Cloudflare.py`
    - Re‑implemented `get_models()` with `read_models()` helper, `fallback_models`, robust nodriver/curl handling and model‑name cleaning
- **Other provider tweaks**
  - `Provider/Copilot.py` – removed `"reasoning"` alias and initial `setOptions` WS message
  - `Provider/PollinationsAI.py` & `PollinationsImage.py`
    - Converted `audio_models` from list to dict, adjusted usage checks and labels
  - `Provider/hf/__init__.py` – applies `model_aliases` remap before dispatch
  - `Provider/hf_space/DeepseekAI_JanusPro7b.py` – now merges media before upload
  - `needs_auth/Gemini.py` – dropped obsolete Gemini model entries
  - `needs_auth/GigaChat.py` – added lowercase `"gigachat"` alias
- **API & client updates**
  - Replaced `ProviderUtils` with new `Provider` map usage throughout API and GUI server
  - Integrated `AnyProvider` as default fallback in `g4f/client` sync & async flows
  - API endpoints now return counts of providers per model and filter by `x_ignored` header
- **GUI improvements**
  - Updated JS labels with emoji icons, provider ignore logic, model count display
- **Model registry**
  - Renamed base model `"GigaChat:latest"` ➜ `"gigachat"` in `models.py`
- **Miscellaneous**
  - Added audio/video flags to GUI provider list
  - Tightened error propagation in `retry_provider.raise_exceptions`

* Fix unittests

* fix: handle None conversation when accessing provider-specific data

- Modified `AnyProvider` class in `g4f/providers/any_provider.py`
- Updated logic to check if `conversation` is not None before accessing `provider.__name__` attribute
- Wrapped `getattr(conversation, provider.__name__, None)` block in an additional `if conversation is not None` condition
- Changed `setattr(conversation, provider.__name__, chunk)` to use `chunk.get_dict()` instead of the object directly
- Ensured consistent use of `JsonConversation` when modifying or assigning `conversation` data

* ```
feat: add provider string conversion & update IterListProvider call

- In g4f/client/__init__.py, within both Completions and AsyncCompletions, added a check to convert the provider from a string using convert_to_provider(provider) when applicable.
- In g4f/providers/any_provider.py, removed the second argument (False) from the IterListProvider constructor call in the async for loop.
```

---------

Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>
2025-04-18 14:10:51 +02:00

96 lines
3.5 KiB
Python

from __future__ import annotations
from ..providers.types import BaseProvider, ProviderType
from ..providers.retry_provider import RetryProvider, IterListProvider
from ..providers.base_provider import AsyncProvider, AsyncGeneratorProvider
from ..providers.create_images import CreateImagesProvider
from .. import debug
try:
from .deprecated import *
except ImportError as e:
debug.error("Deprecated providers not loaded:", e)
from .needs_auth import *
from .template import OpenaiTemplate, BackendApi
from .hf import HuggingFace, HuggingChat, HuggingFaceAPI, HuggingFaceInference, HuggingFaceMedia
try:
from .not_working import *
except ImportError as e:
debug.error("Not working providers not loaded:", e)
try:
from .local import *
except ImportError as e:
debug.error("Local providers not loaded:", e)
try:
from .hf_space import *
except ImportError as e:
debug.error("HuggingFace Space providers not loaded:", e)
try:
from .mini_max import HailuoAI, MiniMax
except ImportError as e:
debug.error("MiniMax providers not loaded:", e)
try:
from .AllenAI import AllenAI
from .ARTA import ARTA
from .Blackbox import Blackbox
from .Chatai import Chatai
from .ChatGLM import ChatGLM
from .ChatGpt import ChatGpt
from .ChatGptEs import ChatGptEs
from .Cloudflare import Cloudflare
from .Copilot import Copilot
from .DDG import DDG
from .DeepInfraChat import DeepInfraChat
from .DuckDuckGo import DuckDuckGo
from .Dynaspark import Dynaspark
except ImportError as e:
debug.error("Providers not loaded (A-D):", e)
try:
from .Free2GPT import Free2GPT
from .FreeGpt import FreeGpt
from .FreeRouter import FreeRouter
from .GizAI import GizAI
from .Glider import Glider
from .Goabror import Goabror
from .ImageLabs import ImageLabs
from .Jmuz import Jmuz
from .LambdaChat import LambdaChat
from .Liaobots import Liaobots
from .LMArenaProvider import LMArenaProvider
from .OIVSCode import OIVSCode
except ImportError as e:
debug.error("Providers not loaded (F-L):", e)
try:
from .PerplexityLabs import PerplexityLabs
from .Pi import Pi
from .Pizzagpt import Pizzagpt
from .PollinationsAI import PollinationsAI
from .PollinationsImage import PollinationsImage
from .TeachAnything import TeachAnything
from .TypeGPT import TypeGPT
from .You import You
from .Websim import Websim
from .Yqcloud import Yqcloud
except ImportError as e:
debug.error("Providers not loaded (M-Z):", e)
import sys
__modules__: list = [
getattr(sys.modules[__name__], provider) for provider in dir()
if not provider.startswith("__")
]
__providers__: list[ProviderType] = [
provider for provider in __modules__
if isinstance(provider, type)
and issubclass(provider, BaseProvider)
]
__all__: list[str] = [
provider.__name__ for provider in __providers__
]
__map__: dict[str, ProviderType] = {
provider.__name__: provider for provider in __providers__
}
class ProviderUtils:
convert: dict[str, ProviderType] = __map__