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

XFEstudio/gpt4free

Refactor to use StreamSession in Qwen and Yupp providers

Replaced aiohttp.ClientSession with StreamSession in Qwen.py and Yupp.py for improved session handling. Updated exception and timeout references in Yupp.py to use aiohttp types. Improved default argument handling in StreamSession initialization.

aead0e4b
Ammar <ammar.alkotb@gmail.com>
提交于

代码差异

3 个文件 +15 -11
Modified g4f/Provider/Qwen.py +2 -1
@@ -9,6 +9,7 @@ from time import time
9 9 from typing import Literal, Optional
10 10
11 11 import aiohttp
12 from ..requests.aiohttp import StreamSession
12 13 from ..errors import RateLimitError, ResponseError
13 14 from ..typing import AsyncResult, Messages, MediaListType
14 15 from ..providers.response import JsonConversation, Reasoning, Usage, ImageResponse, FinishReason
@@ -175,7 +176,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
175 176
176 177 prompt = get_last_user_message(messages)
177 178
178 async with aiohttp.ClientSession(headers=headers) as session:
179 async with StreamSession(headers=headers) as session:
179 180 for attempt in range(5):
180 181 try:
181 182 if not cls._midtoken:
Modified g4f/Provider/Yupp.py +5 -6
@@ -6,8 +6,7 @@ import re
6 6 import os
7 7 import asyncio
8 8 import aiohttp
9 from aiohttp import ClientResponseError, ClientTimeout
10
9 from ..requests.aiohttp import StreamSession
11 10 from ..typing import AsyncResult, Messages, Optional, Dict, Any, List
12 11 from ..providers.base_provider import AsyncGeneratorProvider, ProviderModelMixin
13 12 from ..providers.response import Reasoning, PlainTextResponse, PreviewResponse, JsonConversation, ImageResponse, \
@@ -336,7 +335,7 @@ class Yupp(AsyncGeneratorProvider, ProviderModelMixin):
336 335 raise ProviderException("No valid Yupp accounts available")
337 336
338 337 try:
339 async with aiohttp.ClientSession() as session:
338 async with StreamSession() as session:
340 339 turn_id = str(uuid.uuid4())
341 340
342 341
@@ -395,11 +394,11 @@ class Yupp(AsyncGeneratorProvider, ProviderModelMixin):
395 394 log_debug(f"Sending request to: {url}")
396 395 log_debug(f"Payload structure: {type(payload)}, length: {len(str(payload))}")
397 396 _timeout = kwargs.get("timeout")
398 if isinstance(_timeout, ClientTimeout):
397 if isinstance(_timeout, aiohttp.ClientTimeout):
399 398 timeout = _timeout
400 399 else:
401 400 total = float(_timeout) if isinstance(_timeout, (int, float)) else 5 * 60
402 timeout = ClientTimeout(total=total)
401 timeout = aiohttp.ClientTimeout(total=total)
403 402 # Send request
404 403 async with session.post(url, json=payload, headers=headers, proxy=proxy, timeout=timeout) as response:
405 404 response.raise_for_status()
@@ -426,7 +425,7 @@ class Yupp(AsyncGeneratorProvider, ProviderModelMixin):
426 425 else:
427 426 account["error_count"] += 1
428 427 continue
429 except ClientResponseError as e:
428 except aiohttp.ClientResponseError as e:
430 429 log_debug(f"Account ...{account['token'][-4:]} failed: {str(e)}")
431 430 # No Available Yupp credits
432 431 if e.status == 500 and 'Internal Server Error' in e.message:
Modified g4f/requests/aiohttp.py +8 -4
@@ -31,17 +31,21 @@ class StreamResponse(ClientResponse):
31 31 except json.JSONDecodeError:
32 32 continue
33 33
34 class StreamSession():
34 class StreamSession:
35 35 def __init__(
36 36 self,
37 headers: dict = {},
37 headers=None,
38 38 timeout: int = None,
39 39 connector: BaseConnector = None,
40 40 proxy: str = None,
41 proxies: dict = {},
41 proxies=None,
42 42 impersonate = None,
43 43 **kwargs
44 44 ):
45 if proxies is None:
46 proxies = {}
47 if headers is None:
48 headers = {}
45 49 if impersonate:
46 50 headers = {
47 51 **DEFAULT_HEADERS,
@@ -49,7 +53,7 @@ class StreamSession():
49 53 }
50 54 connect = None
51 55 if isinstance(timeout, tuple):
52 connect, timeout = timeout;
56 connect, timeout = timeout
53 57 if timeout is not None:
54 58 timeout = ClientTimeout(timeout, connect)
55 59 if proxy is None: