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

XFEstudio/gpt4free

Use StreamSession for proper line-by-line NDJSON parsing

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

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

代码差异

1 个文件 +5 -11
Modified g4f/Provider/GradientNetwork.py +5 -11
@@ -2,10 +2,9 @@ from __future__ import annotations
2 2
3 3 import json
4 4
5 from aiohttp import ClientSession
6
7 5 from ..typing import AsyncResult, Messages
8 6 from ..providers.response import Reasoning
7 from ..requests import StreamSession
9 8 from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
10 9
11 10
@@ -58,7 +57,7 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin):
58 57 proxy: Optional proxy URL
59 58 temperature: Optional temperature parameter
60 59 max_tokens: Optional max tokens parameter
61 enable_thinking: Enable the thinking/analysis channel
60 enable_thinking: Enable the thinking/analysis channel (maps to enableThinking in API)
62 61 **kwargs: Additional arguments
63 62
64 63 Yields:
@@ -87,19 +86,14 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin):
87 86 if enable_thinking:
88 87 payload["enableThinking"] = True
89 88
90 async with ClientSession(headers=headers) as session:
89 async with StreamSession(headers=headers, proxy=proxy) as session:
91 90 async with session.post(
92 91 cls.api_endpoint,
93 92 json=payload,
94 proxy=proxy
95 93 ) as response:
96 94 response.raise_for_status()
97 95
98 async for line_bytes in response.content:
99 if not line_bytes:
100 continue
101
102 line = line_bytes.decode("utf-8").strip()
96 async for line in response.iter_lines():
103 97 if not line:
104 98 continue
105 99
@@ -127,5 +121,5 @@ class GradientNetwork(AsyncGeneratorProvider, ProviderModelMixin):
127 121 # as they are for GPU cluster visualization only
128 122
129 123 except json.JSONDecodeError:
130 # Skip non-JSON lines
124 # Skip non-JSON lines (may be partial data or empty)
131 125 continue