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: Internationalization global variables cannot be retrieved #2148

Merged
merged 1 commit into from
Feb 7, 2025
Merged
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
fix: Internationalization global variables cannot be retrieved
  • Loading branch information
shaohuzhang1 committed Feb 7, 2025
commit 05cccf1109dca023d62b50ed3aebf411fdb21cf0
2 changes: 1 addition & 1 deletion apps/application/flow/workflow_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ def reset_prompt(self, prompt: str):
globeLabel = f"全局变量.{field.get('value')}"
globeLabelNew = f"global.{field.get('value')}"
globeValue = f"context.get('global').get('{field.get('value', '')}','')"
prompt = prompt.replace(globeLabel, globeValue).replace(globeLabelNew, globeLabel)
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.

Expand Down