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

feat: The workflow node cannot obtain values from other nodes, which is set as the default value #1923

Merged
merged 1 commit into from
Dec 27, 2024
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
30 changes: 20 additions & 10 deletions apps/application/flow/workflow_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,33 +693,43 @@ def get_reference_field(self, node_id: str, fields: List[str]):
else:
return self.get_node_by_id(node_id).get_reference_field(fields)

def generate_prompt(self, prompt: str):
"""
格式化生成提示词
@param prompt: 提示词信息
@return: 格式化后的提示词
"""
def get_workflow_content(self):
context = {
'global': self.context,
}

for node in self.node_context:
properties = node.node.properties
context[node.id] = node.context
return context

def reset_prompt(self, prompt: str):
placeholder = "{}"
for node in self.flow.nodes:
properties = node.properties
node_config = properties.get('config')
if node_config is not None:
fields = node_config.get('fields')
if fields is not None:
for field in fields:
globeLabel = f"{properties.get('stepName')}.{field.get('value')}"
globeValue = f"context['{node.id}'].{field.get('value')}"
globeValue = f"context.get('{node.id}',{placeholder}).get('{field.get('value', '')}','')"
prompt = prompt.replace(globeLabel, globeValue)
global_fields = node_config.get('globalFields')
if global_fields is not None:
for field in global_fields:
globeLabel = f"全局变量.{field.get('value')}"
globeValue = f"context['global'].{field.get('value')}"
globeValue = f"context.get('global').get('{field.get('value', '')}','')"
prompt = prompt.replace(globeLabel, globeValue)
context[node.id] = node.context
return prompt

def generate_prompt(self, prompt: str):
"""
格式化生成提示词
@param prompt: 提示词信息
@return: 格式化后的提示词
"""
context = self.get_workflow_content()
prompt = self.reset_prompt(prompt)
prompt_template = PromptTemplate.from_template(prompt, template_format='jinja2')
value = prompt_template.format(context=context)
return value
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 several issues and optimizations to address in the provided code:

Issues:

  1. Method Name Conflicts: The generate_prompt method name seems duplicated with another one that is intended to be used after setting up the workflow content. This should ideally only exist once, or better yet renamed.
  2. Context Merging Logic: The logic for merging contexts from nodes into the context dictionary is mixed within different methods. It would make sense to consolidate this logic into a single place.
  3. Global Variable Handling: The handling of global variables using placeholders ({}) does not ensure that all missing keys return an empty string, but rather raises a TypeError when trying to access them.
  4. Template Replacement Efficiency: Replacing labels multiple times may lead to unnecessary overhead.

Optimization Suggestions:

  1. Single Method Naming: Rename generate_prompt to something more descriptive like format_prompt.
  2. Consolidate Context Merging: Combine the context merging logic into a single function to improve readability and maintainability.
  3. Handle Missing Globals Gracefully: Modify the script to handle missing global variables gracefully without raising errors.

Here's the revised code addressing these points:

@@ -693,33 +693,43 @@ def get_reference_field(self, node_id: str, fields: List[str]):


-    def generate_prompt(self, prompt: str):
+    def format_prompt(self, prompt: str) -> str:
         """
         格式化生成提示词
         @param prompt: 提示词信息
         @return: 格式化后的提示词
         """
+        

This change renames the method slightly for clarity while keeping consistent naming conventions throughout the file.

Expand Down
Loading