diff --git a/g4f/Provider/GradientNetwork.py b/g4f/Provider/GradientNetwork.py index 2a7f20a8..679adfb2 100644 --- a/g4f/Provider/GradientNetwork.py +++ b/g4f/Provider/GradientNetwork.py @@ -3,7 +3,7 @@ from __future__ import annotations import json from ..typing import AsyncResult, Messages -from ..providers.response import Reasoning +from ..providers.response import Reasoning, JsonResponse from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider, ProviderModelMixin @@ -23,7 +23,7 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin): supports_system_message = True supports_message_history = True - default_model = "Qwen3 235B" + default_model = "GPT OSS 120B" models = [ default_model, "GPT OSS 120B", @@ -40,9 +40,7 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin): model: str, messages: Messages, proxy: str = None, - temperature: float = None, - max_tokens: int = None, - enable_thinking: bool = False, + enable_thinking: bool = True, **kwargs ) -> AsyncResult: """ @@ -52,8 +50,6 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin): model: The model name to use messages: List of message dictionaries proxy: Optional proxy URL - temperature: Optional temperature parameter - max_tokens: Optional max tokens parameter enable_thinking: Enable the thinking/analysis channel (maps to enableThinking in API) **kwargs: Additional arguments @@ -66,24 +62,18 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin): headers = { "Accept": "application/x-ndjson", "Content-Type": "application/json", - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", "Origin": cls.url, "Referer": f"{cls.url}/", } payload = { + "clusterMode": "nvidia" if "GPT OSS" in model else "hybrid", "model": model, "messages": messages, } - - if temperature is not None: - payload["temperature"] = temperature - if max_tokens is not None: - payload["max_tokens"] = max_tokens if enable_thinking: payload["enableThinking"] = enable_thinking - - async with StreamSession(headers=headers, proxy=proxy) as session: + async with StreamSession(headers=headers, proxy=proxy, impersonate="chrome") as session: async with session.post( cls.api_endpoint, json=payload, @@ -96,6 +86,7 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin): try: data = json.loads(line) + yield JsonResponse.from_dict(data) msg_type = data.get("type") if msg_type == "reply": @@ -113,4 +104,4 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin): except json.JSONDecodeError: # Skip non-JSON lines (may be partial data or empty) - continue + raise diff --git a/g4f/Provider/ItalyGPT.py b/g4f/Provider/ItalyGPT.py index e13fc0ee..43e9eba0 100644 --- a/g4f/Provider/ItalyGPT.py +++ b/g4f/Provider/ItalyGPT.py @@ -1,5 +1,6 @@ from .base_provider import AsyncGeneratorProvider, ProviderModelMixin from ..typing import AsyncResult, Messages +from ..requests import DEFAULT_HEADERS from aiohttp import ClientSession class ItalyGPT(AsyncGeneratorProvider, ProviderModelMixin): @@ -23,10 +24,10 @@ class ItalyGPT(AsyncGeneratorProvider, ProviderModelMixin): ) -> AsyncResult: model = cls.get_model(model) headers = { + **DEFAULT_HEADERS, "content-type": "application/json", "origin": "https://italygpt.it", "referer": "https://italygpt.it/", - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" } payload = { "messages": messages, @@ -34,12 +35,12 @@ class ItalyGPT(AsyncGeneratorProvider, ProviderModelMixin): } async with ClientSession() as session: async with session.post( - f"{cls.url}/api/chat/", + f"{cls.url}/api/chat", json=payload, headers=headers, proxy=proxy, ) as resp: resp.raise_for_status() - async for chunk in resp.content: + async for chunk in resp.content.iter_any(): if chunk: yield chunk.decode() diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py index ea51e380..1f97f9ce 100644 --- a/g4f/Provider/__init__.py +++ b/g4f/Provider/__init__.py @@ -49,6 +49,7 @@ from .DeepInfra import DeepInfra from .EasyChat import EasyChat from .GLM import GLM from .GradientNetwork import GradientNetwork +from .ItalyGPT import ItalyGPT from .LambdaChat import LambdaChat from .Mintlify import Mintlify from .OIVSCodeSer import OIVSCodeSer2, OIVSCodeSer0501 diff --git a/g4f/Provider/hf_space/BAAI_Ling.py b/g4f/Provider/hf_space/BAAI_Ling.py index dbd1fbb2..cff02dd0 100644 --- a/g4f/Provider/hf_space/BAAI_Ling.py +++ b/g4f/Provider/hf_space/BAAI_Ling.py @@ -8,12 +8,12 @@ from ...typing import AsyncResult, Messages from ...providers.response import JsonConversation from ...requests.raise_for_status import raise_for_status from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin -from ..helper import format_prompt, get_last_user_message +from ..helper import format_prompt, get_last_user_message, get_system_prompt from ... import debug class BAAI_Ling(AsyncGeneratorProvider, ProviderModelMixin): - label = "BAAI Ling" - url = "https://instspace-ling-playground.hf.space" + label = "Ling & Ring Playground" + url = "https://cafe3310-ling-playground.hf.space" api_endpoint = f"{url}/gradio_api/queue/join" working = True @@ -25,7 +25,7 @@ class BAAI_Ling(AsyncGeneratorProvider, ProviderModelMixin): model_aliases = { "ling": default_model, } - models = [default_model] + models = ['ling-mini-2.0', 'ling-1t', 'ling-flash-2.0', 'ring-1t', 'ring-flash-2.0', 'ring-mini-2.0'] @classmethod async def create_async_generator( @@ -40,6 +40,7 @@ class BAAI_Ling(AsyncGeneratorProvider, ProviderModelMixin): if is_new_conversation: conversation = JsonConversation(session_hash=str(uuid.uuid4()).replace('-', '')[:12]) + model = cls.get_model(model) prompt = format_prompt(messages) if is_new_conversation else get_last_user_message(messages) headers = { @@ -52,10 +53,21 @@ class BAAI_Ling(AsyncGeneratorProvider, ProviderModelMixin): } payload = { - "data": [prompt], + "data": [ + prompt, + [ + [ + None, + "Hello! I'm Ling. Try selecting a scenario and a message example below to get started." + ] + ], + get_system_prompt(messages), + 1, + model + ], "event_data": None, - "fn_index": 0, - "trigger_id": 5, + "fn_index": 11, + "trigger_id": 14, "session_hash": conversation.session_hash } @@ -79,27 +91,22 @@ class BAAI_Ling(AsyncGeneratorProvider, ProviderModelMixin): if decoded_line.startswith('data: '): try: json_data = json.loads(decoded_line[6:]) - if json_data.get('msg') == 'process_generating': if 'output' in json_data and 'data' in json_data['output']: output_data = json_data['output']['data'] if output_data and len(output_data) > 0: - text = output_data[0] - if isinstance(text, str) and text.startswith(full_response): - yield text[len(full_response):] - full_response = text - elif isinstance(text, str): - yield text - full_response = text + parts = output_data[0][0] + if len(parts) == 2: + new_text = output_data[0][1].pop() + full_response += new_text + yield new_text + if len(parts) > 2: + new_text = parts[2] + full_response += new_text + yield new_text elif json_data.get('msg') == 'process_completed': - if 'output' in json_data and 'data' in json_data['output']: - output_data = json_data['output']['data'] - if output_data and len(output_data) > 0: - final_text = output_data[0] - if isinstance(final_text, str) and len(final_text) > len(full_response): - yield final_text[len(full_response):] - break + break except json.JSONDecodeError: debug.log("Could not parse JSON:", decoded_line)