chat_catalog.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """Admit only chat models into shared chat catalogs.
  2. Generation models are identified by the capability type or name shape the
  3. catalog already publishes. Missing, malformed, or unknown metadata fails open.
  4. No single model id is special-cased.
  5. """
  6. from __future__ import annotations
  7. import re
  8. from typing import Any
  9. # Structured catalog types that mean "this row is not a chat completion".
  10. _GENERATION_TYPES = frozenset({
  11. "image",
  12. "image_generation",
  13. "image-generation",
  14. "image-gen",
  15. "video",
  16. "video_generation",
  17. "video-generation",
  18. "video-gen",
  19. })
  20. _GENERATION_OUTPUTS = frozenset({"image", "video"})
  21. _CHAT_OUTPUTS = frozenset({"text", "chat"})
  22. # Separator-qualified generation tokens, plus the generation id tokens the
  23. # DeepInfra chat catalog already excludes. ``vision`` / ``vl`` are not tokens
  24. # here — those remain chat models.
  25. _GENERATION_ID_RE = re.compile(
  26. r"(?i)(?:^|[/_.-])(?:"
  27. r"image(?:[/_.-]gen(?:eration)?)?|"
  28. r"video(?:[/_.-]gen(?:eration)?)?|"
  29. r"text[/_.-]to[/_.-](?:image|video)|"
  30. r"stable-diffusion|sdxl|flux"
  31. r")(?=$|[/_.-])"
  32. )
  33. # Ids a live catalog item already proved are generation-only, even when the
  34. # id itself has no generation token. Process-local: the proof came from this
  35. # process's catalog parse.
  36. _seen_generation_ids: set[str] = set()
  37. def note_generation_model(model_id: Any) -> None:
  38. key = str(model_id or "").strip().lower()
  39. if key:
  40. _seen_generation_ids.add(key)
  41. def model_id_is_generation(model_id: Any) -> bool:
  42. """Whether *model_id* names an image/video generation surface."""
  43. return bool(_GENERATION_ID_RE.search(str(model_id or "").strip()))
  44. def _tag_list_is_generation(tags: Any) -> bool:
  45. if not isinstance(tags, list):
  46. return False
  47. return any(str(tag or "").strip().lower() in _GENERATION_TYPES for tag in tags)
  48. def _output_modalities_are_generation(outputs: Any) -> bool:
  49. if not isinstance(outputs, list) or not outputs:
  50. return False
  51. kinds = {str(item or "").strip().lower() for item in outputs if str(item or "").strip()}
  52. return bool(kinds & _GENERATION_OUTPUTS) and not bool(kinds & _CHAT_OUTPUTS)
  53. def catalog_item_is_generation(item: Any) -> bool:
  54. """Fail open unless this catalog row's own type or name proves it is generation-only."""
  55. if not isinstance(item, dict):
  56. return False
  57. if model_id_is_generation(item.get("id")):
  58. return True
  59. capabilities = item.get("capabilities")
  60. if isinstance(capabilities, dict):
  61. model_type = str(capabilities.get("type") or "").strip().lower()
  62. if model_type in _GENERATION_TYPES:
  63. return True
  64. if _tag_list_is_generation(capabilities.get("tags")):
  65. return True
  66. top_type = item.get("type")
  67. if isinstance(top_type, str) and top_type.strip().lower() in _GENERATION_TYPES:
  68. return True
  69. if _tag_list_is_generation(item.get("tags")):
  70. return True
  71. architecture = item.get("architecture")
  72. if isinstance(architecture, dict) and _output_modalities_are_generation(architecture.get("output_modalities")):
  73. return True
  74. metadata = item.get("metadata")
  75. if isinstance(metadata, dict) and _tag_list_is_generation(metadata.get("tags")):
  76. return True
  77. return False
  78. def note_catalog_item(item: Any) -> bool:
  79. """Remember a generation row and report whether it must leave the chat catalog."""
  80. if not catalog_item_is_generation(item):
  81. return False
  82. if isinstance(item, dict):
  83. note_generation_model(item.get("id"))
  84. return True
  85. def is_known_non_chat_model(model_id: Any) -> bool:
  86. """True when the id's name shape, or a catalog item already parsed, says it is not chat."""
  87. mid = str(model_id or "").strip()
  88. return model_id_is_generation(mid) or mid.lower() in _seen_generation_ids
  89. def without_generation_models(models):
  90. """Drop generation ids, preserving a list subclass (curated-fallback marker)."""
  91. if not models:
  92. return models
  93. kept = [model for model in models if not is_known_non_chat_model(model)]
  94. if len(kept) == len(list(models)):
  95. return models
  96. try:
  97. return type(models)(kept)
  98. except TypeError:
  99. return kept
  100. def chat_catalog_ids(items: Any) -> list[str]:
  101. """Model ids from a ``/models`` payload, minus generation rows."""
  102. if not isinstance(items, list):
  103. return []
  104. ids: list[str] = []
  105. for item in items:
  106. if not isinstance(item, dict) or "id" not in item:
  107. continue
  108. if note_catalog_item(item):
  109. continue
  110. ids.append(item["id"])
  111. return ids