Skip to content

fix: Internationalization global variables cannot be retrieved#2148

Merged
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_global_params
Feb 7, 2025
Merged

fix: Internationalization global variables cannot be retrieved#2148
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_global_params

Conversation

@shaohuzhang1
Copy link
Contributor

fix: Internationalization global variables cannot be retrieved

prompt = prompt.replace(globeLabel, globeValue).replace(globeLabelNew, globeValue)
return prompt

def generate_prompt(self, prompt: str):
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 provided Python code snippet has a couple of issues:

  1. Redundant Placeholder Replacement: In the generate_prompt method, the replacement operation for globeLabelNew is identical to that for globeLabel. This redundancy can be simplified.

  2. String Concatenation Efficiency: The string concatenations in the methods could be more efficient if they are constructed directly without intermediate variables.

Here's an optimized version of the code:

def reset_prompt(self, prompt: str):
    for field in self.fields:
        globeLabel = f"全局变量.{field.get('value')}"
        globeValue = f"context.get('global').get({field.get('value', '')}, '')"
        
        # Simplify redundant placeholder replacement
        prompt = prompt.replace(globeLabel, globeValue)

    return prompt

def generate_prompt(self, prompt: str):
    for field in self.fields:
        value = field.get('value')
        if value:
            globeVarName = f"global.{value}"
            prompt += f'{{{globleVarName}}}'
    
    return prompt

Explanation:

  • In reset_prompt, I removed the unnecessary call to replace globeLabelNew.
  • In generate_prompt, I used a list comprehension (though here it's just a simple loop) to construct the final prompt efficiently by appending each globally defined variable in curly braces {} to the original prompt.

These changes make the codes cleaner and slightly more maintainable while maintaining their functionality.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Feb 7, 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.

Details

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.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Feb 7, 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.

Details 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

@shaohuzhang1 shaohuzhang1 merged commit b52b01c into main Feb 7, 2025
4 of 5 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_global_params branch February 7, 2025 06:35
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.

1 participant