Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/models_provider/base_model_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ def get_model_list(self, model_type, api_key=None, api_base=None, credential=Non
if api_key or credential:
manage = self._get_dynamic_manage_cached(api_key, api_base, credential)
if manage is not None:
return manage.get_model_list_by_model_type(model_type)
if not allow_builtin_fallback:
dynamic_list = manage.get_model_list_by_model_type(model_type)
# 官方实时列表里没有该类型的模型时,按 allow_builtin_fallback 决定是否
# 回退到内置列表;否则返回空列表,交由视图继续走缓存/内置兜底。
if dynamic_list or not allow_builtin_fallback:
return dynamic_list
elif not allow_builtin_fallback:
return []
return self.get_model_info_manage().get_model_list_by_model_type(model_type)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this diff, here are the observations:

Potential Issues

  1. Behavior change / semantic shift: Previously, when manage is not None, the function always returned the dynamic list (possibly empty) immediately. Now it falls through to the builtin list when dynamic_list is empty and allow_builtin_fallback is True. This changes behavior for existing callers and should be verified against intended contract.

  2. Control flow asymmetry between branches:

    • if manage is not None: branch does NOT return [] when not allow_builtin_fallback — instead it returns dynamic_list (which may be empty), effectively matching. That's fine.
    • elif not allow_builtin_fallback: returns [] only when manage is None. But if manage is None and allow_builtin_fallback is True, it silently falls through to builtin. Confirm that's intended — a failing/absent dynamic manage will now silently use builtins even when the caller asked for "no fallback" indirectly via the credential path. Slight inconsistency: not allow_builtin_fallback semantics differ depending on whether manage was constructed.
  3. Comment language/style: The inline comment is in Chinese while the file/answers convention appears consistent — verify project style guide (some repos require English or i18n comments). Also the comment says "交由视图继续走缓存/内置兜底" which describes caller behavior — this couples the helper to view-layer decisions; consider documenting in docstring instead.

  4. Redundant nested check: if dynamic_list or not allow_builtin_fallback: return dynamic_list followed by falling out of the if manage is not None block is correct, but the readability could be improved with an early return [] explicitly for the fallthrough-non-fallback case to make intent obvious.

Optimization / Readability Suggestions

if api_key or credential:
    manage = self._get_dynamic_manage_cached(api_key, api_base, credential)
    if manage is not None:
        dynamic_list = manage.get_model_list_by_model_type(model_type)
        if dynamic_list or not allow_builtin_fallback:
            return dynamic_list
        # else: fall through to builtin list below
    elif not allow_builtin_fallback:
        return []
return self.get_model_info_manage().get_model_list_by_model_type(model_type)
  • Consider extracting builtin = self.get_model_info_manage().get_model_list_by_model_type(model_type) once for reuse.
  • Add/update docstring to describe allow_builtin_fallback precisely (does it apply when manage is None? when dynamic list is empty?).
  • Add unit tests for the four combinations: manage present/absent × allow_builtin_fallback True/False, plus dynamic_list empty/non-empty.

Minor

  • No null-check on the return value of get_model_list_by_model_type — ensure it's guaranteed to be list-like (or short-circuit depends on truthiness; None would behave as empty, likely fine but worth confirming).
  • The diff header shows credential=None truncated; confirm signature unchanged elsewhere.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ def _initialize_model_info():
]
tencent_embedding_model_info = model_info_embedding_list[0]

# STT 默认模型:未选择基础模型时 model_form 依赖该默认凭证表单,避免返回“模型不支持”。
model_info_stt_list = [info for info in model_info_list if info.model_type == ModelTypeConst.STT.name]

model_info_vision_list = [
_create_model_info(
"hunyuan-vision",
Expand Down Expand Up @@ -214,6 +217,7 @@ def _initialize_model_info():
.append_model_info_list(model_info_itv_list)
.append_default_model_info(model_info_itv_list[0])
.append_default_model_info(model_info_list[0])
.append_default_model_info(model_info_stt_list[0])
.append_default_model_info(tencent_embedding_model_info)
.build()
)
Expand Down
23 changes: 23 additions & 0 deletions apps/models_provider/tests/test_minimax.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ def test_dynamic_model_list_falls_back_without_or_with_bad_key(self):
self.assertEqual([m["name"] for m in fallback], [m["name"] for m in builtin])
self.assertTrue(len(builtin) > 0)

def test_dynamic_model_list_type_missing_falls_back_to_builtin(self):
"""官方 /models 未返回所选类型(如 MiniMax 的 TTS/TTI)时,get_model_list 须回退到内置列表。"""
from models_provider.base_model_provider import ModelTypeConst

def fake_get(url, headers=None, timeout=None):
resp = Mock()
resp.status_code = 200
# 官方列表只返回对话模型,不含 TTS(复现 Bug 1:语音合成获取模型返回 data: [])。
resp.json.return_value = {"object": "list", "data": [{"id": "MiniMax-M2", "object": "model"}]}
return resp

with patch("models_provider.base_model_provider.requests.get", side_effect=fake_get):
# 实时查询(不带内置兜底)对该类型仍应为空。
live = self.provider.get_model_list("TTS", api_key="type-missing-key", allow_builtin_fallback=False)
# 内置兜底开启时,应返回内置 TTS 模型,而不是空列表。
tts = self.provider.get_model_list("TTS", api_key="type-missing-key")

self.assertEqual(live, [])
builtin_tts = self.provider.get_model_list("TTS")
self.assertEqual([m["name"] for m in tts], [m["name"] for m in builtin_tts])
self.assertTrue(len(tts) > 0)
self.assertEqual(tts[0]["model_type"], ModelTypeConst.TTS.name)

def test_current_catalog_and_default(self):
self.assertEqual(
[model["name"] for model in self.provider.get_model_list("LLM")],
Expand Down
32 changes: 32 additions & 0 deletions apps/models_provider/tests/test_tencent_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
from django.test import SimpleTestCase

from models_provider.impl.tencent_model_provider.tencent_model_provider import TencentModelProvider


class TencentModelProviderTests(SimpleTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.provider = TencentModelProvider()

def test_stt_model_type_is_exposed(self):
model_types = {item.get("value") for item in self.provider.get_model_type_list()}
self.assertIn("STT", model_types)

def test_stt_default_credential_without_model_name(self):
# model_form 在未选择基础模型(model_name 为空)时应返回默认 STT 凭证,
# 否则 create-model 流程会以“模型不支持”报 500。
credential = self.provider.get_model_credential("STT", "")
self.assertIsNotNone(credential)
self.assertTrue(credential.to_form_list())

def test_wand_asr_v1_uses_tokenhub_credential(self):
from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import (
TencentTokenhubSTTModelCredential,
)

credential = self.provider.get_model_credential("STT", "wand-asr-v1")
self.assertIsInstance(credential, TencentTokenhubSTTModelCredential)
fields = {item.get("field") for item in credential.to_form_list()}
self.assertIn("api_key", fields)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, the added tests are syntactically fine and cover the intended Tencent STT credential paths. Main concerns are test isolation, strictness of assertions, and Django test class choice.

Potential issues / suggestions

  1. SimpleTestCase may be too restrictive

    • SimpleTestCase disallows database access.
    • If TencentModelProvider() or credential.to_form_list() touches the DB, cache, settings requiring DB, or network, these tests may fail or behave inconsistently.
    • If no DB access is involved, SimpleTestCase is appropriate. Otherwise use TestCase or mock external dependencies.
  2. Shared provider instance across tests

    • cls.provider = TencentModelProvider() in setUpClass means all tests share one instance.
    • If the provider caches credentials or has mutable state, tests may become order-dependent.
    • Safer:
      def setUp(self):
          self.provider = TencentModelProvider()
    • If the provider is guaranteed stateless, the current approach is acceptable for speed.
  3. Default-credential test is too permissive

    • This only checks that the credential is not None and that its form list is non-empty:
      self.assertIsNotNone(credential)
      self.assertTrue(credential.to_form_list())
    • This could pass even if the wrong default credential is returned.
    • Better to assert the expected credential type and/or expected fields:
      self.assertIsNotNone(credential)
      self.assertIsInstance(credential, ExpectedDefaultSTTCredential)
      fields = {item["field"] for item in credential.to_form_list()}
      self.assertIn("api_key", fields)
    • Adjust to the actual default credential class.
  4. Clarify empty string vs. missing model name

    • The test name says without_model_name, but it passes "", not None.
    • Confirm the real contract. If both "" and None are valid “no model selected” inputs, test both. If only one is valid, rename the test to match, e.g. test_stt_default_credential_with_empty_model_name.
  5. Local import inside test method

    • This import:
      from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import (
          TencentTokenhubSTTModelCredential,
      )
      is fine functionally, but normally belongs at module level.
    • Move it to the top unless it is intentionally local to avoid a circular import. If so, add a short comment explaining why.
  6. Clearer failure for assertIsInstance

    • assertIsInstance(credential, ...) will fail clearly if credential is None, but adding:
      self.assertIsNotNone(credential)
      before it can make the failure reason clearer.
  7. Missing negative coverage

    • Consider adding tests for:
      • unsupported model type,
      • unsupported model name,
      • empty model name for non-STT types.
    • This depends on the expected behavior of get_model_credential.
  8. Minor robustness in form-field extraction

    • {item.get("field") for item in ...} may silently include None if malformed data is returned.
    • Using item["field"] makes malformed entries fail explicitly. This is optional.

Suggested refactor sketch

# -*- coding: utf-8 -*-
from django.test import SimpleTestCase

from models_provider.impl.tencent_model_provider.tencent_model_provider import TencentModelProvider
from models_provider.impl.tencent_model_provider.credential.tokenhub_stt import (
    TencentTokenhubSTTModelCredential,
)


class TencentModelProviderTests(SimpleTestCase):
    def setUp(self):
        self.provider = TencentModelProvider()

    def test_stt_model_type_is_exposed(self):
        model_types = {item.get("value") for item in self.provider.get_model_type_list()}
        self.assertIn("STT", model_types)

    def test_stt_default_credential_with_empty_model_name(self):
        credential = self.provider.get_model_credential("STT", "")
        self.assertIsNotNone(credential)
        # Assert the expected default credential type as well.
        self.assertTrue(credential.to_form_list())

    def test_wand_asr_v1_uses_tokenhub_credential(self):
        credential = self.provider.get_model_credential("STT", "wand-asr-v1")
        self.assertIsNotNone(credential)
        self.assertIsInstance(credential, TencentTokenhubSTTModelCredential)
        fields = {item["field"] for item in credential.to_form_list()}
        self.assertIn("api_key", fields)

No blocking syntax issues were found. The most important checks are whether SimpleTestCase is safe for this provider and whether the default-credential assertion is strong enough.

Loading