Skip to content
Closed
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
4 changes: 4 additions & 0 deletions apps/models_provider/base_model_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def classify_dynamic_model_id(model_id: str):
return ModelTypeConst.EMBEDDING
if "rerank" in model_id:
return ModelTypeConst.RERANKER
if "bge" in model_id:
# 智源 BGE 系列(bge-small/bge-m3/bge-large 等)为向量模型,
# 放在 rerank 判断之后,避免 bge-reranker-* 被误判为向量模型。
return ModelTypeConst.EMBEDDING
if any(k in model_id for k in ("whisper", "asr", "transcribe", "paraformer")):
return ModelTypeConst.STT
if any(k in model_id for k in ("tts", "speech", "voice", "cosyvoice", "sambert")):

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.

Here is the check of your code snippet:

1. Potential Issue: Model Classification Ambiguity

  • Problem: The logic relies on string matching ("bge" in model_id). This works for common cases (e.g., bge-m3, bge-large) but may fail if:
    • A non-BGE embedding model happens to contain “bge” in its name (unlikely, but possible in custom/edge cases).
    • A new BGE variant changes naming conventions slightly (e.g., baai-bge vs bge).
  • Suggestion: Consider using a more deterministic mapping or prefix check if model IDs are predictable, e.g., model_id.startswith("bge").

2. Optimization / Robustness Suggestion

  • Order matters: Your comment correctly notes that placing "bge" after "rerank" prevents misclassification of bge-reranker-*. This is a good catch. ✅
  • Consider explicit handling: If you maintain a list of known BGE models, matching against exact prefixes or a curated dict could be safer than substring checks.

3. Code Style / Consistency

  • The use of Chinese comments is fine for internal teams, but ensure consistency across the codebase if internationalization is a concern.
  • Ensure ModelTypeConst imports and types are consistent.

Final Verdict

The change is functionally sound given typical Hugging Face/BAAI model naming conventions. Just ensure no future model violates the assumption that *bge* (excluding rerankers) is always an embedding model.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
f"Verification failed, please check whether the parameters are correct: {e}",
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

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.

Here is an analysis of the code changes and suggestions:

1. Potential Issues & Irregularities

Loss of Context and User-Friendly Messaging

  • Issue: The original code provided a generic, localized message ("Verification failed...") and appended the specific error details. The new code simply returns str(e).
  • Impact:
    • If the exception e does not have a clear, human-readable message (e.g., it's a stack trace fragment or a technical detail like 'key'), the user/API client will see useless information.
    • You lose the ability to distinguish between "valid format but wrong value" vs. "internal system error" in the response string.
  • Internationalization (i18n): You removed the gettext() call. If str(e) contains text from a library that might be localized differently or is in English, you are no longer ensuring consistent localization for the wrapper message. The raw error e may or may not be translated depending on its origin.

Security/Information Leakage

  • Risk: Returning str(e) directly can sometimes leak internal implementation details, paths, or query strings in debugging environments if DEBUG=True or if the exception handling isn't strict. Ensure e is sanitized before exposing it to the end-user.

2. Optimization & Improvement Suggestions

✅ Recommended Approach: Maintain Structure, Ensure Safety

Instead of returning just str(e), consider keeping a structured response while being concise:

if raise_exception:
    # Option A: Keep context + localizable prefix
    msg = gettext("Verification failed: {error}").format(error=str(e))
    raise AppApiException(ValidCode.valid_error.value, msg)

# OR

# Option B: If str(e) is ALWAYS guaranteed to be a perfect user-facing message
raise AppApiException(ValidCode.valid_error.value, str(e))

🔧 Better Practice: Custom Exception Handling

If str(e) varies widely, ensure your exceptions inherit from a base class that guarantees a .message attribute suitable for display:

class ValidationException(Exception):
    def __init__(self, message: str):
        self.message = message  # Guaranteed user-safe
        super().__init__(self.message)

Then in your code:

raise AppApiException(
    ValidCode.valid_error.value,
    e.message  # Clean, pre-formatted, safe
)

3. Summary of Recommendations

Concern Original Code New Code Recommendation
Clarity High ("Verification failed...") Low (depends entirely on e) ⚠️ Prefer retaining context unless e is perfectly formatted.
i18n Uses gettext No gettext wrapper ⚠️ Ensure e itself is localized, or keep the wrapper.
Robustness Consistent format Variable format ✅ Use a custom exception with a .message property.
Conciseness Verbose Concise ✅ Good if e is already a clean, short, user-friendly string.

Final Verdict

The change is risky unless you have strict control over what e represents. If e comes from third-party libraries or deep validation logic, str(e) may be too technical for users.

Safer refactor:

if raise_exception:
    # Truncate if necessary to avoid huge payloads
    error_msg = str(e).strip()[:200] 
    raise AppApiException(
        ValidCode.valid_error.value,
        gettext("Verification failed: {error}").format(error=error_msg)
    )
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

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.

Here is the analysis of the code changes:

Irregularities & Potential Issues

  1. Loss of Context/Error Message Clarity:

    • Before: The error message was "Verification failed, please check whether the parameters are correct: {error}". This provided clear context to the API consumer about why the validation failed generally and included specific details.
    • After: The exception only contains str(e), which is just the raw exception message from the underlying validator. This removes the standardized "Verification failed..." prefix.
    • Impact: Clients relying on specific string matching or expecting a consistent format for user-facing errors may break or provide poor UX (e.g., showing a cryptic technical constraint violation instead of "Please check parameters").
  2. Internationalization (i18n) Regression:

    • The original code used gettext() to translate the static part of the message ("Verification failed...").
    • The new code removes this. While the dynamic str(e) likely wasn't translated, removing gettext breaks internationalization support for the generic error context if that existed in the frontend handling based on the structure.
  3. Security/Information Leakage:

    • Exposing raw str(e) might leak internal implementation details (e.g., database-specific constraints, library internals) that could be helpful to attackers, whereas the previous message wrapped it in a generic validation failure.

Optimization Suggestions

  1. Preserve User-Facing Context, but simplify formatting:
    If the goal was to remove verbose translation strings or fix a bug with gettext formatting, consider keeping a generic, non-translated prefix if possible, or ensure the client-side application handles the raw str(e) gracefully.

    If you must return only str(e), ensure your API consumers know the response body format has changed.

  2. If i18n is required, revert to using gettext but simplify the logic:

    if raise_exception:
        # Ensure translation exists for this message
        raise AppApiException(
            ValidCode.valid_error.value,
            gettext("Verification failed: {}").format(str(e))
        )
  3. If the old message was buggy (e.g., .format() causing issues with certain characters in str(e)):
    Use f-strings or %(x)s formatting which are often safer/more efficient than .format() with concatenated translated strings. However, never drop the context entirely unless intentional.

Conclusion

The change is not purely an optimization; it is a behavioral modification that reduces error message clarity and removes internationalization capabilities.

  • ✅ Acceptable if the product requirements explicitly state that developers/clients should handle raw Python/Validator exceptions directly and no localized/generic messaging is needed.
  • ❌ Regression if user-friendly or standardized API error messages are expected.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False
return True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False
return True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False
return True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: %(error)s")
% {"error": str(e)},
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct"),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct"),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
_("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
return False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ def is_valid(
if raise_exception:
raise AppApiException(
ValidCode.valid_error.value,
gettext("Verification failed, please check whether the parameters are correct: {error}").format(
error=str(e)
),
str(e),
)
else:
return False
Expand Down
Loading
Loading