Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: i18n azure llm mode #2095

Merged
merged 1 commit into from
Jan 24, 2025
Merged

fix: i18n azure llm mode #2095

merged 1 commit into from
Jan 24, 2025

Conversation

shaohuzhang1
Copy link
Contributor

fix: i18n azure llm mode

Copy link

f2c-ci-robot bot commented Jan 24, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

f2c-ci-robot bot commented Jan 24, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@@ -53,7 +53,7 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
return False
try:
model = provider.get_model(model_type, model_name, model_credential, **model_params)
model.invoke([HumanMessage(content=_('Hello'))])
model.invoke([HumanMessage(content=gettext('Hello'))])
except Exception as e:
if isinstance(e, AppApiException):
raise e
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code snippet appears to be written using Python with some string localization conventions that differ from modern practices. Here are the minor adjustments and points of improvement I would suggest:

  1. String Localization: The use of gettext function is good practice, but it's recommended to use context managers like with gettext.translation() where applicable if you intend to localize messages dynamically.

  2. Function Parameter Passing: The parameter names for passing parameters to the model's invoke method are inconsistent. You should pass them explicitly instead of using wildcard arguments (**model_params).

Here’s an improved version of your code incorporating these suggestions:

try:
    model = provider.get_model(model_type, model_name, model_credential, content=gettext('Hello'))
except (Exception, AppApiException) as e:
    raise e

This change ensures clarity in how parameters are passed to the model's invoke method and adheres to best practices related to string handling.

@@ -49,7 +49,7 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje

try:
model = provider.get_model(model_type, model_name, model_credential, **model_params)
model.invoke([HumanMessage(content=_('Hello'))])
model.invoke([HumanMessage(content=gettext('Hello'))])
except AppApiException:
raise
except Exception as e:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are no irregularities or major issues with this code snippet based on the provided context.

One minor suggestion is to use f-string formatting within gettext instead of concatenation:

content=f'{{{gettext("Hello")}}}'

This can improve readability.

The rest of the code looks clean and functional for invoking a model with input messages.

@@ -53,7 +53,7 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
return False
try:
model = provider.get_model(model_type, model_name, model_credential, **model_params)
model.invoke([HumanMessage(content=_('Hello'))])
model.invoke([HumanMessage(content=gettext('Hello'))])
except Exception as e:
if isinstance(e, AppApiException):
raise e
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code appears to be using both Python's built-in gettext function from the gettext module and Flask's current_app._translate. This can potentially lead to unexpected behavior if not managed carefully.

Consider removing one of these functions to clarify which translation mechanism is being used. If you intend to use Django's gettext, ensure all strings are marked for translation with _() before they are passed to the model method. Here's an optimized version that uses only one source:

@@ -53,11 +53,10 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje
                     return False
         try:
-            if model.type == 'flask':
-                content = current_app._translate("Hello")
-            else:
-                content = _("Hello")
+            content = translate_phrase("Hello", "en" or "default_language")
+
             model.invoke([HumanMessage(content=content)])
         except AppApiException as e:
             raise e

Make sure to implement a helper function like translate_phrase that fetches the appropriate translated phrase based on your needs (e.g., fetching translations from a database or an external API).

@liuruibin liuruibin merged commit 48dce6c into main Jan 24, 2025
4 of 5 checks passed
@liuruibin liuruibin deleted the pr@main@fix_llm branch January 24, 2025 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants