gpt4free/g4f/models.py
kqlio67 9def1aa71f
Update model configurations, provider implementations, and documentation (#2577)
* Update model configurations, provider implementations, and documentation

- Updated model names and aliases for Qwen QVQ 72B and Qwen 2 72B (@TheFirstNoob)
- Revised HuggingSpace class configuration, added default_image_model
- Added llama-3.2-70b alias for Llama 3.2 70B model in AutonomousAI
- Removed BlackboxCreateAgent class
- Added gpt-4o alias for Copilot model
- Moved api_key to Mhystical class attribute
- Added models property with default_model value for Free2GPT
- Simplified Jmuz class implementation
- Improved image generation and model handling in DeepInfra
- Standardized default models and removed aliases in Gemini
- Replaced model aliases with direct model list in GlhfChat (@TheFirstNoob)
- Removed trailing slash from image generation URL in PollinationsAI (https://github.com/xtekky/gpt4free/issues/2571)
- Updated llama and qwen model configurations
- Enhanced provider documentation and model details

* Removed from (g4f/models.py) 'Yqcloud' provider from Default due to error 'ResponseStatusError: Response 429: 文字过长,请删减后重试。'

* Update docs/providers-and-models.md

* refactor(g4f/Provider/DDG.py): Add error handling and rate limiting to DDG provider

- Add custom exception classes for rate limits, timeouts, and conversation limits
- Implement rate limiting with sleep between requests (0.75s minimum delay)
- Add model validation method to check supported models
- Add proper error handling for API responses with custom exceptions
- Improve session cookie handling for conversation persistence
- Clean up User-Agent string and remove redundant code
- Add proper error propagation through async generator

Breaking changes:
- New custom exceptions may require updates to error handling code
- Rate limiting affects request timing and throughput
- Model validation is now stricter

Related:
- Adds error handling similar to standard API clients
- Improves reliability and robustness of chat interactions

* Update g4f/models.py g4f/Provider/PollinationsAI.py

* Update g4f/models.py

* Restored provider which was not working and was disabled (g4f/Provider/DeepInfraChat.py)

* Fixing a bug with Streaming Completions

* Update g4f/Provider/PollinationsAI.py

* Update g4f/Provider/Blackbox.py g4f/Provider/DDG.py

* Added another model for generating images 'ImageGeneration2' to the 'Blackbox' provider

* Update docs/providers-and-models.md

* Update g4f/models.py g4f/Provider/Blackbox.py

* Added a new OIVSCode provider from the Text Models and Vision (Image Upload) model

* Update docs/providers-and-models.md

* docs: add Conversation Memory class with context handling requested by @TheFirstNoob

* Simplified README.md documentation added new docs/configuration.md documentation

* Update add README.md docs/configuration.md

* Update README.md

* Update docs/providers-and-models.md g4f/models.py g4f/Provider/PollinationsAI.py

* Added new model deepseek-r1 to Blackbox provider. @TheFirstNoob

* Fixed bugs and updated docs/providers-and-models.md etc/unittest/client.py g4f/models.py g4f/Provider/.

---------

Co-authored-by: kqlio67 <>
Co-authored-by: H Lohaus <hlohaus@users.noreply.github.com>
2025-01-24 03:47:57 +01:00

803 lines
18 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from .Provider import IterListProvider, ProviderType
from .Provider import (
### no auth required ###
AIChatFree,
AIUncensored,
AutonomousAI,
Blackbox,
CablyAI,
ChatGLM,
ChatGpt,
ChatGptEs,
ChatGptt,
Cloudflare,
Copilot,
DarkAI,
DDG,
DeepInfraChat,
HuggingSpace,
GPROChat,
Jmuz,
Liaobots,
Mhystical,
OIVSCode,
PerplexityLabs,
Pi,
PollinationsAI,
TeachAnything,
Yqcloud,
### needs auth ###
BingCreateImages,
CopilotAccount,
Gemini,
GeminiPro,
GigaChat,
HuggingChat,
HuggingFace,
MetaAI,
MicrosoftDesigner,
OpenaiAccount,
OpenaiChat,
Reka,
)
@dataclass(unsafe_hash=True)
class Model:
"""
Represents a machine learning model configuration.
Attributes:
name (str): Name of the model.
base_provider (str): Default provider for the model.
best_provider (ProviderType): The preferred provider for the model, typically with retry logic.
"""
name: str
base_provider: str
best_provider: ProviderType = None
@staticmethod
def __all__() -> list[str]:
"""Returns a list of all model names."""
return _all_models
class ImageModel(Model):
pass
### Default ###
default = Model(
name = "",
base_provider = "",
best_provider = IterListProvider([
DDG,
Blackbox,
Copilot,
DeepInfraChat,
ChatGptEs,
ChatGptt,
PollinationsAI,
Jmuz,
CablyAI,
OIVSCode,
DarkAI,
AIUncensored,
OpenaiChat,
Cloudflare,
])
)
############
### Text ###
############
### OpenAI ###
# gpt-3.5
gpt_35_turbo = Model(
name = 'gpt-3.5-turbo',
base_provider = 'OpenAI',
best_provider = IterListProvider([DarkAI, ChatGpt])
)
# gpt-4
gpt_4 = Model(
name = 'gpt-4',
base_provider = 'OpenAI',
best_provider = IterListProvider([DDG, Blackbox, Jmuz, ChatGptEs, ChatGptt, PollinationsAI, Yqcloud, Copilot, OpenaiChat, Liaobots, Mhystical])
)
# gpt-4o
gpt_4o = Model(
name = 'gpt-4o',
base_provider = 'OpenAI',
best_provider = IterListProvider([Blackbox, ChatGptt, Jmuz, ChatGptEs, PollinationsAI, DarkAI, Copilot, ChatGpt, Liaobots, OpenaiChat])
)
gpt_4o_mini = Model(
name = 'gpt-4o-mini',
base_provider = 'OpenAI',
best_provider = IterListProvider([DDG, ChatGptEs, ChatGptt, Jmuz, PollinationsAI, OIVSCode, ChatGpt, Liaobots, OpenaiChat])
)
# o1
o1 = Model(
name = 'o1',
base_provider = 'OpenAI',
best_provider = OpenaiAccount
)
o1_preview = Model(
name = 'o1-preview',
base_provider = 'OpenAI',
best_provider = Liaobots
)
o1_mini = Model(
name = 'o1-mini',
base_provider = 'OpenAI',
best_provider = Liaobots
)
### GigaChat ###
gigachat = Model(
name = 'GigaChat:latest',
base_provider = 'gigachat',
best_provider = GigaChat
)
### Meta ###
meta = Model(
name = "meta-ai",
base_provider = "Meta",
best_provider = MetaAI
)
# llama 2
llama_2_7b = Model(
name = "llama-2-7b",
base_provider = "Meta Llama",
best_provider = Cloudflare
)
# llama 3
llama_3_8b = Model(
name = "llama-3-8b",
base_provider = "Meta Llama",
best_provider = IterListProvider([Jmuz, Cloudflare])
)
llama_3_70b = Model(
name = "llama-3-70b",
base_provider = "Meta Llama",
best_provider = Jmuz
)
# llama 3.1
llama_3_1_8b = Model(
name = "llama-3.1-8b",
base_provider = "Meta Llama",
best_provider = IterListProvider([Blackbox, DeepInfraChat, Jmuz, PollinationsAI, Cloudflare, PerplexityLabs])
)
llama_3_1_70b = Model(
name = "llama-3.1-70b",
base_provider = "Meta Llama",
best_provider = IterListProvider([DDG, Jmuz, Blackbox, TeachAnything, DarkAI, PerplexityLabs])
)
llama_3_1_405b = Model(
name = "llama-3.1-405b",
base_provider = "Meta Llama",
best_provider = IterListProvider([Blackbox, Jmuz])
)
# llama 3.2
llama_3_2_1b = Model(
name = "llama-3.2-1b",
base_provider = "Meta Llama",
best_provider = Cloudflare
)
llama_3_2_11b = Model(
name = "llama-3.2-11b",
base_provider = "Meta Llama",
best_provider = IterListProvider([Jmuz, HuggingChat, HuggingFace])
)
llama_3_2_70b = Model(
name = "llama-3.2-70b",
base_provider = "Meta Llama",
best_provider = AutonomousAI
)
llama_3_2_90b = Model(
name = "llama-3.2-90b",
base_provider = "Meta Llama",
best_provider = IterListProvider([Jmuz, AutonomousAI])
)
# llama 3.3
llama_3_3_70b = Model(
name = "llama-3.3-70b",
base_provider = "Meta Llama",
best_provider = IterListProvider([Blackbox, DeepInfraChat, PollinationsAI, AutonomousAI, Jmuz, HuggingChat, HuggingFace, PerplexityLabs])
)
### Mistral ###
mixtral_7b = Model(
name = "mixtral-7b",
base_provider = "Mistral",
best_provider = Blackbox
)
mixtral_8x7b = Model(
name = "mixtral-8x7b",
base_provider = "Mistral",
best_provider = IterListProvider([DDG, Jmuz])
)
mistral_nemo = Model(
name = "mistral-nemo",
base_provider = "Mistral",
best_provider = IterListProvider([PollinationsAI, HuggingChat, HuggingFace])
)
mistral_large = Model(
name = "mistral-large",
base_provider = "Mistral",
best_provider = PollinationsAI
)
### NousResearch ###
hermes_2_dpo = Model(
name = "hermes-2-dpo",
base_provider = "NousResearch",
best_provider = Blackbox
)
hermes_3 = Model(
name = "hermes-3",
base_provider = "NousResearch",
best_provider = IterListProvider([AutonomousAI, AIUncensored])
)
### Microsoft ###
# phi
phi_3_5_mini = Model(
name = "phi-3.5-mini",
base_provider = "Microsoft",
best_provider = HuggingChat
)
# wizardlm
wizardlm_2_7b = Model(
name = 'wizardlm-2-7b',
base_provider = 'Microsoft',
best_provider = DeepInfraChat
)
wizardlm_2_8x22b = Model(
name = 'wizardlm-2-8x22b',
base_provider = 'Microsoft',
best_provider = IterListProvider([DeepInfraChat, Jmuz])
)
### Google DeepMind ###
# gemini
gemini = Model(
name = 'gemini',
base_provider = 'Google',
best_provider = Gemini
)
# gemini-exp
gemini_exp = Model(
name = 'gemini-exp',
base_provider = 'Google',
best_provider = Jmuz
)
# gemini-1.5
gemini_1_5_pro = Model(
name = 'gemini-1.5-pro',
base_provider = 'Google DeepMind',
best_provider = IterListProvider([Blackbox, Jmuz, GPROChat, AIChatFree, Gemini, GeminiPro, Liaobots])
)
gemini_1_5_flash = Model(
name = 'gemini-1.5-flash',
base_provider = 'Google DeepMind',
best_provider = IterListProvider([Blackbox, Jmuz, Gemini, GeminiPro, Liaobots])
)
# gemini-2.0
gemini_2_0_flash = Model(
name = 'gemini-2.0-flash',
base_provider = 'Google DeepMind',
best_provider = IterListProvider([GeminiPro, Liaobots])
)
gemini_2_0_flash_thinking = Model(
name = 'gemini-2.0-flash-thinking',
base_provider = 'Google DeepMind',
best_provider = Liaobots
)
### Anthropic ###
# claude 3
claude_3_haiku = Model(
name = 'claude-3-haiku',
base_provider = 'Anthropic',
best_provider = IterListProvider([DDG, Jmuz])
)
claude_3_sonnet = Model(
name = 'claude-3-sonnet',
base_provider = 'Anthropic',
best_provider = Liaobots
)
claude_3_opus = Model(
name = 'claude-3-opus',
base_provider = 'Anthropic',
best_provider = IterListProvider([Jmuz, Liaobots])
)
# claude 3.5
claude_3_5_haiku = Model(
name = 'claude-3.5-haiku',
base_provider = 'Anthropic',
best_provider = PollinationsAI
)
claude_3_5_sonnet = Model(
name = 'claude-3.5-sonnet',
base_provider = 'Anthropic',
best_provider = IterListProvider([Blackbox, PollinationsAI, Jmuz, Liaobots])
)
### Reka AI ###
reka_core = Model(
name = 'reka-core',
base_provider = 'Reka AI',
best_provider = Reka
)
### Blackbox AI ###
blackboxai = Model(
name = 'blackboxai',
base_provider = 'Blackbox AI',
best_provider = Blackbox
)
blackboxai_pro = Model(
name = 'blackboxai-pro',
base_provider = 'Blackbox AI',
best_provider = Blackbox
)
### CohereForAI ###
command_r = Model(
name = 'command-r',
base_provider = 'CohereForAI',
best_provider = IterListProvider([HuggingSpace, PollinationsAI])
)
command_r_plus = Model(
name = 'command-r-plus',
base_provider = 'CohereForAI',
best_provider = IterListProvider([HuggingSpace, HuggingChat])
)
command_r7b = Model(
name = 'command-r7b',
base_provider = 'CohereForAI',
best_provider = HuggingSpace
)
### Qwen ###
# qwen 1_5
qwen_1_5_7b = Model(
name = 'qwen-1.5-7b',
base_provider = 'Qwen',
best_provider = Cloudflare
)
# qwen 2
qwen_2_72b = Model(
name = 'qwen-2-72b',
base_provider = 'Qwen',
best_provider = IterListProvider([DeepInfraChat, PollinationsAI, HuggingSpace])
)
# qwen 2.5
qwen_2_5_72b = Model(
name = 'qwen-2.5-72b',
base_provider = 'Qwen',
best_provider = Jmuz
)
qwen_2_5_coder_32b = Model(
name = 'qwen-2.5-coder-32b',
base_provider = 'Qwen',
best_provider = IterListProvider([DeepInfraChat, PollinationsAI, AutonomousAI, Jmuz, HuggingChat])
)
# qwq/qvq
qwq_32b = Model(
name = 'qwq-32b',
base_provider = 'Qwen',
best_provider = IterListProvider([Blackbox, DeepInfraChat, Jmuz, HuggingChat])
)
qvq_72b = Model(
name = 'qvq-72b',
base_provider = 'Qwen',
best_provider = HuggingSpace
)
### Inflection ###
pi = Model(
name = 'pi',
base_provider = 'Inflection',
best_provider = Pi
)
### DeepSeek ###
deepseek_chat = Model(
name = 'deepseek-chat',
base_provider = 'DeepSeek',
best_provider = IterListProvider([Blackbox, Jmuz, PollinationsAI])
)
deepseek_r1 = Model(
name = 'deepseek-r1',
base_provider = 'DeepSeek',
best_provider = IterListProvider([Blackbox, Jmuz, HuggingChat, HuggingFace])
)
### x.ai ###
grok_2 = Model(
name = 'grok-2',
base_provider = 'x.ai',
best_provider = Liaobots
)
### Perplexity AI ###
sonar_online = Model(
name = 'sonar-online',
base_provider = 'Perplexity AI',
best_provider = PerplexityLabs
)
sonar_chat = Model(
name = 'sonar-chat',
base_provider = 'Perplexity AI',
best_provider = PerplexityLabs
)
### Nvidia ###
nemotron_70b = Model(
name = 'nemotron-70b',
base_provider = 'Nvidia',
best_provider = IterListProvider([DeepInfraChat, HuggingChat, HuggingFace])
)
### Liquid ###
lfm_40b = Model(
name = 'lfm-40b',
base_provider = 'Liquid',
best_provider = PerplexityLabs
)
### Databricks ###
dbrx_instruct = Model(
name = 'dbrx-instruct',
base_provider = 'Databricks',
best_provider = Blackbox
)
### PollinationsAI ###
p1 = Model(
name = 'p1',
base_provider = 'PollinationsAI',
best_provider = PollinationsAI
)
### CablyAI ###
cably_80b = Model(
name = 'cably-80b',
base_provider = 'CablyAI',
best_provider = CablyAI
)
### THUDM ###
glm_4 = Model(
name = 'glm-4',
base_provider = 'THUDM',
best_provider = ChatGLM
)
### Uncensored AI ###
evil = Model(
name = 'evil',
base_provider = 'Evil Mode - Experimental',
best_provider = PollinationsAI
)
### Other ###
midijourney = Model(
name = 'midijourney',
base_provider = 'Other',
best_provider = PollinationsAI
)
unity = Model(
name = 'unity',
base_provider = 'Other',
best_provider = PollinationsAI
)
rtist = Model(
name = 'rtist',
base_provider = 'Other',
best_provider = PollinationsAI
)
#############
### Image ###
#############
### Stability AI ###
sd_turbo = ImageModel(
name = 'sd-turbo',
base_provider = 'Stability AI',
best_provider = PollinationsAI
)
sd_3_5 = ImageModel(
name = 'sd-3.5',
base_provider = 'Stability AI',
best_provider = HuggingSpace
)
### Flux AI ###
flux = ImageModel(
name = 'flux',
base_provider = 'Flux AI',
best_provider = IterListProvider([Blackbox, PollinationsAI])
)
flux_dev = ImageModel(
name = 'flux-dev',
base_provider = 'Flux AI',
best_provider = IterListProvider([HuggingSpace, HuggingChat, HuggingFace])
)
flux_schnell = ImageModel(
name = 'flux-schnell',
base_provider = 'Flux AI',
best_provider = IterListProvider([HuggingSpace, HuggingChat, HuggingFace])
)
### OpenAI ###
dall_e_3 = ImageModel(
name = 'dall-e-3',
base_provider = 'OpenAI',
best_provider = IterListProvider([PollinationsAI, CopilotAccount, OpenaiAccount, MicrosoftDesigner, BingCreateImages])
)
### Midjourney ###
midjourney = ImageModel(
name = 'midjourney',
base_provider = 'Midjourney',
best_provider = PollinationsAI
)
class ModelUtils:
"""
Utility class for mapping string identifiers to Model instances.
Attributes:
convert (dict[str, Model]): Dictionary mapping model string identifiers to Model instances.
"""
convert: dict[str, Model] = {
############
### Text ###
############
### OpenAI ###
# gpt-3
'gpt-3': gpt_35_turbo,
# gpt-3.5
gpt_35_turbo.name: gpt_35_turbo,
# gpt-4
gpt_4.name: gpt_4,
# gpt-4o
gpt_4o.name: gpt_4o,
gpt_4o_mini.name: gpt_4o_mini,
# o1
o1.name: o1,
o1_preview.name: o1_preview,
o1_mini.name: o1_mini,
### Meta ###
meta.name: meta,
# llama-2
llama_2_7b.name: llama_2_7b,
# llama-3
llama_3_8b.name: llama_3_8b,
llama_3_70b.name: llama_3_70b,
# llama-3.1
llama_3_1_8b.name: llama_3_1_8b,
llama_3_1_70b.name: llama_3_1_70b,
llama_3_1_405b.name: llama_3_1_405b,
# llama-3.2
llama_3_2_1b.name: llama_3_2_1b,
llama_3_2_11b.name: llama_3_2_11b,
llama_3_2_70b.name: llama_3_2_70b,
llama_3_2_90b.name: llama_3_2_90b,
# llama-3.3
llama_3_3_70b.name: llama_3_3_70b,
### Mistral ###
mixtral_7b.name: mixtral_7b,
mixtral_8x7b.name: mixtral_8x7b,
mistral_nemo.name: mistral_nemo,
mistral_large.name: mistral_large,
### NousResearch ###
hermes_2_dpo.name: hermes_2_dpo,
hermes_3.name: hermes_3,
### Microsoft ###
# phi
phi_3_5_mini.name: phi_3_5_mini,
# wizardlm
wizardlm_2_7b.name: wizardlm_2_7b,
wizardlm_2_8x22b.name: wizardlm_2_8x22b,
### Google ###
# gemini
gemini.name: gemini,
# gemini-exp
gemini_exp.name: gemini_exp,
# gemini-1.5
gemini_1_5_pro.name: gemini_1_5_pro,
gemini_1_5_flash.name: gemini_1_5_flash,
# gemini-2.0
gemini_2_0_flash.name: gemini_2_0_flash,
gemini_2_0_flash_thinking.name: gemini_2_0_flash_thinking,
### Anthropic ###
# claude 3
claude_3_opus.name: claude_3_opus,
claude_3_sonnet.name: claude_3_sonnet,
claude_3_haiku.name: claude_3_haiku,
# claude 3.5
claude_3_5_haiku.name: claude_3_5_haiku,
claude_3_5_sonnet.name: claude_3_5_sonnet,
### Reka AI ###
reka_core.name: reka_core,
### Blackbox AI ###
blackboxai.name: blackboxai,
blackboxai_pro.name: blackboxai_pro,
### CohereForAI ###
command_r.name: command_r,
command_r_plus.name: command_r_plus,
command_r7b.name: command_r7b,
### GigaChat ###
gigachat.name: gigachat,
### Qwen ###
# qwen 1_5
qwen_1_5_7b.name: qwen_1_5_7b,
# qwen 2
qwen_2_72b.name: qwen_2_72b,
# qwen 2.5
qwen_2_5_72b.name: qwen_2_5_72b,
qwen_2_5_coder_32b.name: qwen_2_5_coder_32b,
# qwq/qvq
qwq_32b.name: qwq_32b,
qvq_72b.name: qvq_72b,
### Inflection ###
pi.name: pi,
### x.ai ###
grok_2.name: grok_2,
### Perplexity AI ###
sonar_online.name: sonar_online,
sonar_chat.name: sonar_chat,
### DeepSeek ###
deepseek_chat.name: deepseek_chat,
deepseek_r1.name: deepseek_r1,
### Nvidia ###
nemotron_70b.name: nemotron_70b,
### Liquid ###
lfm_40b.name: lfm_40b,
### Databricks ###
dbrx_instruct.name: dbrx_instruct,
### PollinationsAI ###
p1.name: p1,
### CablyAI ###
cably_80b.name: cably_80b,
### THUDM ###
glm_4.name: glm_4,
### Uncensored AI ###
evil.name: evil,
### Other ###
midijourney.name: midijourney,
unity.name: unity,
rtist.name: rtist,
#############
### Image ###
#############
### Stability AI ###
sd_turbo.name: sd_turbo,
sd_3_5.name: sd_3_5,
### Flux AI ###
flux.name: flux,
flux_dev.name: flux_dev,
flux_schnell.name: flux_schnell,
### OpenAI ###
dall_e_3.name: dall_e_3,
### Midjourney ###
midjourney.name: midjourney,
}
# Create a list of all models and his providers
__models__ = {
model.name: (model, providers)
for model, providers in [
(model, [provider for provider in model.best_provider.providers if provider.working]
if isinstance(model.best_provider, IterListProvider)
else [model.best_provider]
if model.best_provider is not None and model.best_provider.working
else [])
for model in ModelUtils.convert.values()]
if providers
}
_all_models = list(__models__.keys())