返回提交历史
Modified
g4f/Provider/Qwen.py
+19
-5
XFEstudio/gpt4free
Qwen: update headers, timestamps, and payloads
Adjust request headers and payload structure for Qwen provider: add 'source' and 'version' headers, disable X-Accel-Buffering (to help fix FAIL_SYS_USER_VALIDATE), and bump 'bx-v'. Consolidate timestamp generation (now) and include timestamps and project_id in new chat and message payloads. Add message 'version' and extra.meta.subChatType, and move top-level timestamp into the message envelope. Improve error handling by raising on non-success JSON responses earlier to avoid consuming the stream and causing streaming issues.
39ed0ef0
代码差异
1 个文件
+19
-5
@@ -316,7 +316,12 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
316
316
'Connection': 'keep-alive',
317
317
'X-Requested-With': 'XMLHttpRequest',
318
318
'Cookie': f'ssxmod_itna={data["ssxmod_itna"]};ssxmod_itna2={data["ssxmod_itna2"]}',
319
'X-Source': 'web'
319
# 'X-Source': 'web',
320
'source': 'web',
321
'version': '0.2.63',
322
# 'timezone': int(time() * 1000),
323
# Fix 'FAIL_SYS_USER_VALIDATE'
324
"X-Accel-Buffering":"no"
320
325
}
321
326
if token:
322
327
headers['Authorization'] = f'Bearer {token}'
@@ -342,7 +347,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
342
347
343
348
req_headers = session.headers.copy()
344
349
req_headers['bx-umidtoken'] = cls._midtoken
345
req_headers['bx-v'] = '2.5.31'
350
req_headers['bx-v'] = '2.5.36'
346
351
# fix error [g4f.errors.CloudflareError:aliyun_waf_aa]
347
352
req_headers["x-request-id"] = str(uuid.uuid4())
348
353
return req_headers
@@ -412,13 +417,15 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
412
417
try:
413
418
req_headers = await cls._get_req_headers(session, proxy=proxy)
414
419
message_id = str(uuid.uuid4())
420
now = int(time() * 1000)
415
421
if conversation is None:
416
422
chat_payload = {
417
423
"title": "New Chat",
418
424
"models": [model_name],
419
425
"chat_mode": "normal",
420
426
"chat_type": chat_type,
421
"timestamp": int(time() * 1000)
427
"timestamp": now,
428
"project_id": ""
422
429
}
423
430
async with session.post(
424
431
f'{cls.url}/api/v2/chats/new', json=chat_payload, headers=req_headers,
@@ -456,6 +463,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
456
463
457
464
msg_payload = {
458
465
"stream": stream,
466
"version": '2.1',
459
467
"incremental_output": stream,
460
468
"chat_id": conversation.chat_id,
461
469
"chat_mode": "normal",
@@ -470,12 +478,15 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
470
478
"content": prompt,
471
479
"user_action": "chat",
472
480
"files": files,
481
"timestamp": now,
473
482
"models": [model_name],
474
483
"chat_type": chat_type,
475
484
"feature_config": feature_config,
485
"extra": {"meta": {"subChatType": chat_type}},
476
486
"sub_chat_type": chat_type
477
487
}
478
]
488
],
489
"timestamp": now
479
490
}
480
491
481
492
if aspect_ratio:
@@ -489,7 +500,10 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
489
500
await cls.raise_for_status(resp)
490
501
if resp.headers.get("content-type", "").startswith("application/json"):
491
502
resp_json = await resp.json()
492
if resp_json.get("success") is False or resp_json.get("data", {}).get("code") or 'FAIL_SYS_USER_VALIDATE' in resp_json.get("ret", []):
503
if resp_json.get("success") is False or resp_json.get("data", {}).get("code"):
504
raise RuntimeError(f"Response: {resp_json}")
505
else:
506
# cant stream resp after `resp_json = await resp.json()`, so it stick
493
507
raise RuntimeError(f"Response: {resp_json}")
494
508
# args["cookies"] = merge_cookies(args.get("cookies"), resp)
495
509
thinking_started = False