From 9def1aa71f5c0340967297a94b7742c8d7c7fd8d Mon Sep 17 00:00:00 2001 From: kqlio67 <166700875+kqlio67@users.noreply.github.com> Date: Fri, 24 Jan 2025 02:47:57 +0000 Subject: Update model configurations, provider implementations, and documentation (#2577) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- g4f/Provider/needs_auth/DeepInfra.py | 81 ++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 23 deletions(-) (limited to 'g4f/Provider/needs_auth/DeepInfra.py') diff --git a/g4f/Provider/needs_auth/DeepInfra.py b/g4f/Provider/needs_auth/DeepInfra.py index 86993314..ea537b3b 100644 --- a/g4f/Provider/needs_auth/DeepInfra.py +++ b/g4f/Provider/needs_auth/DeepInfra.py @@ -2,41 +2,59 @@ from __future__ import annotations import requests from ...typing import AsyncResult, Messages -from .OpenaiAPI import OpenaiAPI from ...requests import StreamSession, raise_for_status from ...image import ImageResponse +from .OpenaiAPI import OpenaiAPI +from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin -class DeepInfra(OpenaiAPI): +class DeepInfra(OpenaiAPI, AsyncGeneratorProvider, ProviderModelMixin): label = "DeepInfra" url = "https://deepinfra.com" login_url = "https://deepinfra.com/dash/api_keys" working = True - api_base = "https://api.deepinfra.com/v1/openai", + api_base = "https://api.deepinfra.com/v1/openai" needs_auth = True supports_stream = True supports_message_history = True default_model = "meta-llama/Meta-Llama-3.1-70B-Instruct" - default_image_model = '' - image_models = [default_image_model] + default_image_model = "stabilityai/sd3.5" + models = [] + image_models = [] @classmethod def get_models(cls, **kwargs): if not cls.models: url = 'https://api.deepinfra.com/models/featured' - models = requests.get(url).json() - cls.models = [model['model_name'] for model in models if model["type"] == "text-generation"] - cls.image_models = [model['model_name'] for model in models if model["reported_type"] == "text-to-image"] + response = requests.get(url) + models = response.json() + + cls.models = [] + cls.image_models = [] + + for model in models: + if model["type"] == "text-generation": + cls.models.append(model['model_name']) + elif model["reported_type"] == "text-to-image": + cls.image_models.append(model['model_name']) + + cls.models.extend(cls.image_models) + return cls.models + @classmethod + def get_image_models(cls, **kwargs): + if not cls.image_models: + cls.get_models() + return cls.image_models + @classmethod def create_async_generator( cls, model: str, messages: Messages, - stream: bool = True, + stream: bool, temperature: float = 0.7, max_tokens: int = 1028, - prompt: str = None, **kwargs ) -> AsyncResult: headers = { @@ -47,12 +65,6 @@ class DeepInfra(OpenaiAPI): 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36', 'X-Deepinfra-Source': 'web-embed', } - - # Check if the model is an image model - if model in cls.image_models: - return cls.create_image_generator(messages[-1]["content"] if prompt is None else prompt, model, headers=headers, **kwargs) - - # Text generation return super().create_async_generator( model, messages, stream=stream, @@ -63,7 +75,7 @@ class DeepInfra(OpenaiAPI): ) @classmethod - async def create_image_generator( + async def create_async_image( cls, prompt: str, model: str, @@ -71,13 +83,26 @@ class DeepInfra(OpenaiAPI): api_base: str = "https://api.deepinfra.com/v1/inference", proxy: str = None, timeout: int = 180, - headers: dict = None, extra_data: dict = {}, **kwargs - ) -> AsyncResult: - if api_key is not None and headers is not None: + ) -> ImageResponse: + headers = { + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'en-US', + 'Connection': 'keep-alive', + 'Origin': 'https://deepinfra.com', + 'Referer': 'https://deepinfra.com/', + 'Sec-Fetch-Dest': 'empty', + 'Sec-Fetch-Mode': 'cors', + 'Sec-Fetch-Site': 'same-site', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36', + 'X-Deepinfra-Source': 'web-embed', + 'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"macOS"', + } + if api_key is not None: headers["Authorization"] = f"Bearer {api_key}" - async with StreamSession( proxies={"all": proxy}, headers=headers, @@ -85,7 +110,7 @@ class DeepInfra(OpenaiAPI): ) as session: model = cls.get_model(model) data = {"prompt": prompt, **extra_data} - data = {"input": data} if model == cls.default_image_model else data + data = {"input": data} if model == cls.default_model else data async with session.post(f"{api_base.rstrip('/')}/{model}", json=data) as response: await raise_for_status(response) data = await response.json() @@ -93,4 +118,14 @@ class DeepInfra(OpenaiAPI): if not images: raise RuntimeError(f"Response: {data}") images = images[0] if len(images) == 1 else images - yield ImageResponse(images, prompt) + return ImageResponse(images, prompt) + + @classmethod + async def create_async_image_generator( + cls, + model: str, + messages: Messages, + prompt: str = None, + **kwargs + ) -> AsyncResult: + yield await cls.create_async_image(messages[-1]["content"] if prompt is None else prompt, model, **kwargs) -- cgit v1.2.3