__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Qwen Portal provider profile."""
  2. from typing import Any
  3. from providers import register_provider
  4. from providers.base import ProviderProfile
  5. def _normalize_parts(content: list) -> list | None:
  6. """List content -> list-of-dict parts (str -> text part, image_url dicts copied,
  7. other junk dropped). None when nothing changed (copy-on-write)."""
  8. parts, changed = [], False
  9. for part in content:
  10. if isinstance(part, str):
  11. parts.append({"type": "text", "text": part})
  12. changed = True
  13. elif isinstance(part, dict):
  14. if isinstance(part.get("image_url"), dict):
  15. part = {**part, "image_url": dict(part["image_url"])}
  16. changed = True
  17. parts.append(part)
  18. else:
  19. changed = True
  20. return parts if parts and changed else None
  21. class QwenProfile(ProviderProfile):
  22. """Qwen Portal — message normalization, vl_high_resolution, metadata top-level."""
  23. def prepare_messages(self, messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
  24. """Normalize content to list-of-dicts and inject cache_control on the (first)
  25. system message. Copy-on-write: only touched messages/parts are copied."""
  26. if not messages:
  27. return []
  28. prepared = list(messages)
  29. system_idx: int | None = None
  30. for idx, msg in enumerate(messages):
  31. if not isinstance(msg, dict):
  32. continue
  33. if system_idx is None and msg.get("role") == "system":
  34. system_idx = idx
  35. content = msg.get("content")
  36. if isinstance(content, str):
  37. prepared[idx] = {**msg, "content": [{"type": "text", "text": content}]}
  38. elif isinstance(content, list):
  39. parts = _normalize_parts(content)
  40. if parts is not None:
  41. prepared[idx] = {**msg, "content": parts}
  42. if system_idx is not None:
  43. msg = prepared[system_idx]
  44. content = msg.get("content")
  45. if isinstance(content, list) and content and isinstance(content[-1], dict):
  46. content_copy = list(content)
  47. content_copy[-1] = {**content_copy[-1], "cache_control": {"type": "ephemeral"}}
  48. prepared[system_idx] = {**msg, "content": content_copy}
  49. return prepared
  50. def build_extra_body(self, *, session_id: str | None = None, **context) -> dict[str, Any]:
  51. return {"vl_high_resolution_images": True}
  52. def build_api_kwargs_extras(
  53. self, *, reasoning_config: dict | None = None, qwen_session_metadata: dict | None = None, **context
  54. ) -> tuple[dict[str, Any], dict[str, Any]]:
  55. """Qwen metadata goes to top-level api_kwargs, not extra_body."""
  56. return {}, {"metadata": qwen_session_metadata} if qwen_session_metadata else {}
  57. qwen = QwenProfile(
  58. name="qwen-oauth", aliases=("qwen", "qwen-portal", "qwen-cli"), env_vars=("QWEN_API_KEY",),
  59. base_url="https://portal.qwen.ai/v1", auth_type="oauth_external", default_max_tokens=65536,
  60. )
  61. register_provider(qwen)