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

XFEstudio/gpt4free

Add thinking_mode and feature_config handling

Introduce a thinking_mode option and build a conditional feature_config payload for Qwen requests: when reasoning is enabled the config includes auto_thinking, thinking_mode, thinking_enabled, output_schema, research_mode and auto_search; otherwise it provides a minimal config with thinking_budget. Also include minor cleanup and formatting/PEP8 fixes (spacing around defaults, blank-line adjustments, and reflowed long request lines) to improve readability.

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

代码差异

1 个文件 +44 -34
Modified g4f/Provider/Qwen.py +44 -34
@@ -32,6 +32,7 @@ except ImportError:
32 32 has_curl_cffi = False
33 33 try:
34 34 import zendriver as nodriver
35
35 36 has_nodriver = True
36 37 except ImportError:
37 38 has_nodriver = False
@@ -264,6 +265,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
264 265 @classmethod
265 266 async def get_args(cls, proxy, **kwargs):
266 267 grecaptcha = []
268
267 269 async def callback(page: nodriver.Tab):
268 270 while not await page.evaluate('window.__baxia__ && window.__baxia__.getFYModule'):
269 271 await asyncio.sleep(1)
@@ -274,6 +276,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
274 276 grecaptcha.append(captcha)
275 277 else:
276 278 raise Exception(captcha)
279
277 280 args = await get_args_from_nodriver(cls.url, proxy=proxy, callback=callback)
278 281
279 282 return args, next(iter(grecaptcha))
@@ -288,7 +291,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
288 291 raise CloudflareError(message or html)
289 292
290 293 @classmethod
291 def _get_headers(cls, token = None):
294 def _get_headers(cls, token=None):
292 295 data = generate_cookies()
293 296 # args,ua = await cls.get_args(proxy, **kwargs)
294 297 headers = {
@@ -309,7 +312,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
309 312 if token:
310 313 headers['Authorization'] = f'Bearer {token}'
311 314 return headers
312
315
313 316 @classmethod
314 317 async def _get_req_headers(cls, session, proxy=None):
315 318 if not cls._midtoken:
@@ -344,28 +347,28 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
344 347 "timestamp": int(time() * 1000)
345 348 }
346 349 async with session.post(
347 f'{cls.url}/api/v2/chats/new', json=chat_payload,
348 headers=await cls._get_req_headers(session, proxy=kwargs.get("proxy")),
349 proxy=kwargs.get("proxy")
350 f'{cls.url}/api/v2/chats/new', json=chat_payload,
351 headers=await cls._get_req_headers(session, proxy=kwargs.get("proxy")),
352 proxy=kwargs.get("proxy")
350 353 ) as resp:
351 354 await cls.raise_for_status(resp)
352 355 return await resp.json()
353 356
354 357 @classmethod
355 358 async def create_async_generator(
356 cls,
357 model: str,
358 messages: Messages,
359 media: MediaListType = None,
360 conversation: JsonConversation = None,
361 proxy: str = None,
362 stream: bool = True,
363 reasoning_effort: Optional[Literal["low", "medium", "high"]] = "medium",
364 chat_type: Literal[
365 "t2t", "search", "artifacts", "web_dev", "deep_research", "t2i", "image_edit", "t2v"
366 ] = "t2t",
367 aspect_ratio: Optional[Literal["1:1", "4:3", "3:4", "16:9", "9:16"]] = None,
368 **kwargs
359 cls,
360 model: str,
361 messages: Messages,
362 media: MediaListType = None,
363 conversation: JsonConversation = None,
364 proxy: str = None,
365 stream: bool = True,
366 reasoning_effort: Optional[Literal["low", "medium", "high"]] = "medium",
367 chat_type: Literal[
368 "t2t", "search", "artifacts", "web_dev", "deep_research", "t2i", "image_edit", "t2v"
369 ] = "t2t",
370 aspect_ratio: Optional[Literal["1:1", "4:3", "3:4", "16:9", "9:16"]] = None,
371 **kwargs
369 372 ) -> AsyncResult:
370 373 """
371 374 chat_type:
@@ -381,6 +384,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
381 384 model_name = cls.get_model(model)
382 385 prompt = get_last_user_message(messages)
383 386 enable_thinking = reasoning_effort in ("medium", "high")
387 thinking_mode: Literal["Auto", "Thinking", "Fast"] = kwargs.get("thinking_mode", "Auto")
384 388 timeout = kwargs.get("timeout") or 5 * 60
385 389 token = kwargs.get("token")
386 390 async with StreamSession(headers=cls._get_headers(token)) as session:
@@ -404,8 +408,8 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
404 408 "timestamp": int(time() * 1000)
405 409 }
406 410 async with session.post(
407 f'{cls.url}/api/v2/chats/new', json=chat_payload, headers=req_headers,
408 proxy=proxy
411 f'{cls.url}/api/v2/chats/new', json=chat_payload, headers=req_headers,
412 proxy=proxy
409 413 ) as resp:
410 414 await cls.raise_for_status(resp)
411 415 data = await resp.json()
@@ -422,6 +426,21 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
422 426 files = await cls.prepare_files(media, session=session,
423 427 headers=req_headers)
424 428
429 feature_config = {
430 "auto_thinking": "Auto" == thinking_mode,
431 "thinking_mode": thinking_mode,
432 # "thinking_format": "summary",
433 "thinking_enabled": enable_thinking,
434 "output_schema": "phase",
435 # "instructions": None,
436 "research_mode": "normal",
437 "auto_search": True
438 } if enable_thinking else {
439 "thinking_enabled": enable_thinking,
440 "output_schema": "phase",
441 "thinking_budget": 81920
442 }
443
425 444 msg_payload = {
426 445 "stream": stream,
427 446 "incremental_output": stream,
@@ -440,28 +459,19 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin):
440 459 "files": files,
441 460 "models": [model_name],
442 461 "chat_type": chat_type,
443 "feature_config": {
444 "thinking_enabled": enable_thinking,
445 "output_schema": "phase",
446 "thinking_budget": 81920
447 },
462 "feature_config": feature_config,
448 463 "sub_chat_type": chat_type
449 464 }
450 465 ]
451 466 }
452 if enable_thinking:
453 msg_payload["messages"][0]["feature_config"] = {
454 "thinking_enabled": True,
455 "output_schema": "phase",
456 "thinking_budget": 81920
457 }
467
458 468 if aspect_ratio:
459 469 msg_payload["size"] = aspect_ratio
460 470
461 471 async with session.post(
462 f'{cls.url}/api/v2/chat/completions?chat_id={conversation.chat_id}',
463 json=msg_payload,
464 headers=req_headers, proxy=proxy, timeout=timeout, cookies=conversation.cookies
472 f'{cls.url}/api/v2/chat/completions?chat_id={conversation.chat_id}',
473 json=msg_payload,
474 headers=req_headers, proxy=proxy, timeout=timeout, cookies=conversation.cookies
465 475 ) as resp:
466 476 await cls.raise_for_status(resp)
467 477 if resp.headers.get("content-type", "").startswith("application/json"):