XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/gpt4free

Add GradientNetwork provider for chat.gradient.network

Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>

f0ea4c5b
copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
提交于

代码差异

2 个文件 +132 -0
Added g4f/Provider/GradientNetwork.py +131 -0
@@ -0,0 +1,131 @@
1 from __future__ import annotations
2
3 import json
4
5 from aiohttp import ClientSession
6
7 from ..typing import AsyncResult, Messages
8 from ..providers.response import Reasoning
9 from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
10
11
12 class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin):
13 """
14 Provider for chat.gradient.network
15 Supports streaming text generation with various Qwen models.
16 """
17 label = "Gradient Network"
18 url = "https://chat.gradient.network"
19 api_endpoint = "https://chat.gradient.network/api/generate"
20
21 working = True
22 needs_auth = False
23 supports_stream = True
24 supports_system_message = True
25 supports_message_history = True
26
27 default_model = "qwen3-235b"
28 models = [
29 default_model,
30 "qwen3-32b",
31 "deepseek-r1-0528",
32 "deepseek-v3-0324",
33 "llama-4-maverick",
34 ]
35 model_aliases = {
36 "qwen-3-235b": "qwen3-235b",
37 "deepseek-r1": "deepseek-r1-0528",
38 "deepseek-v3": "deepseek-v3-0324",
39 }
40
41 @classmethod
42 async def create_async_generator(
43 cls,
44 model: str,
45 messages: Messages,
46 proxy: str = None,
47 temperature: float = None,
48 max_tokens: int = None,
49 enable_thinking: bool = False,
50 **kwargs
51 ) -> AsyncResult:
52 """
53 Create an async generator for streaming chat responses.
54
55 Args:
56 model: The model name to use
57 messages: List of message dictionaries
58 proxy: Optional proxy URL
59 temperature: Optional temperature parameter
60 max_tokens: Optional max tokens parameter
61 enable_thinking: Enable the thinking/analysis channel
62 **kwargs: Additional arguments
63
64 Yields:
65 str: Content chunks from the response
66 Reasoning: Thinking content when enable_thinking is True
67 """
68 model = cls.get_model(model)
69
70 headers = {
71 "Accept": "application/x-ndjson",
72 "Content-Type": "application/json",
73 "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
74 "Origin": cls.url,
75 "Referer": f"{cls.url}/",
76 }
77
78 payload = {
79 "model": model,
80 "messages": messages,
81 }
82
83 if temperature is not None:
84 payload["temperature"] = temperature
85 if max_tokens is not None:
86 payload["max_tokens"] = max_tokens
87 if enable_thinking:
88 payload["enableThinking"] = True
89
90 async with ClientSession(headers=headers) as session:
91 async with session.post(
92 cls.api_endpoint,
93 json=payload,
94 proxy=proxy
95 ) as response:
96 response.raise_for_status()
97
98 async for line_bytes in response.content:
99 if not line_bytes:
100 continue
101
102 line = line_bytes.decode("utf-8").strip()
103 if not line:
104 continue
105
106 try:
107 data = json.loads(line)
108 msg_type = data.get("type")
109
110 if msg_type == "text":
111 # Regular text content
112 content = data.get("data")
113 if content:
114 yield content
115
116 elif msg_type == "thinking":
117 # Thinking/reasoning content
118 content = data.get("data")
119 if content:
120 yield Reasoning(content)
121
122 elif msg_type == "done":
123 # Stream complete
124 break
125
126 # Ignore clusterInfo and blockUpdate messages
127 # as they are for GPU cluster visualization only
128
129 except json.JSONDecodeError:
130 # Skip non-JSON lines
131 continue
Modified g4f/Provider/__init__.py +1 -0
@@ -48,6 +48,7 @@ from .Copilot import Copilot
48 48 from .DeepInfra import DeepInfra
49 49 from .EasyChat import EasyChat
50 50 from .GLM import GLM
51 from .GradientNetwork import GradientNetwork
51 52 from .LambdaChat import LambdaChat
52 53 from .Mintlify import Mintlify
53 54 from .OIVSCodeSer import OIVSCodeSer2, OIVSCodeSer0501