fix(workflow): handle special values for process data consistently

- Apply `handle_special_values` to `process_data` in workflow cycle management.
- Improve template processing in `AdvancedPromptTransform` with `VariablePool`.
- Make `system_variables` and `user_inputs` optional in `VariablePool` initialization.
This commit is contained in:
-LAN- 2024-10-14 11:38:00 +08:00
parent 885192db38
commit 03018823d8
3 changed files with 14 additions and 8 deletions

View File

@ -267,6 +267,7 @@ class WorkflowCycleManage:
workflow_node_execution = self._refetch_workflow_node_execution(event.node_execution_id) workflow_node_execution = self._refetch_workflow_node_execution(event.node_execution_id)
inputs = WorkflowEntry.handle_special_values(event.inputs) inputs = WorkflowEntry.handle_special_values(event.inputs)
process_data = WorkflowEntry.handle_special_values(event.process_data)
outputs = WorkflowEntry.handle_special_values(event.outputs) outputs = WorkflowEntry.handle_special_values(event.outputs)
execution_metadata = ( execution_metadata = (
json.dumps(jsonable_encoder(event.execution_metadata)) if event.execution_metadata else None json.dumps(jsonable_encoder(event.execution_metadata)) if event.execution_metadata else None
@ -278,7 +279,7 @@ class WorkflowCycleManage:
{ {
WorkflowNodeExecution.status: WorkflowNodeExecutionStatus.SUCCEEDED.value, WorkflowNodeExecution.status: WorkflowNodeExecutionStatus.SUCCEEDED.value,
WorkflowNodeExecution.inputs: json.dumps(inputs) if inputs else None, WorkflowNodeExecution.inputs: json.dumps(inputs) if inputs else None,
WorkflowNodeExecution.process_data: json.dumps(event.process_data) if event.process_data else None, WorkflowNodeExecution.process_data: json.dumps(process_data) if event.process_data else None,
WorkflowNodeExecution.outputs: json.dumps(outputs) if outputs else None, WorkflowNodeExecution.outputs: json.dumps(outputs) if outputs else None,
WorkflowNodeExecution.execution_metadata: execution_metadata, WorkflowNodeExecution.execution_metadata: execution_metadata,
WorkflowNodeExecution.finished_at: finished_at, WorkflowNodeExecution.finished_at: finished_at,
@ -311,6 +312,7 @@ class WorkflowCycleManage:
workflow_node_execution = self._refetch_workflow_node_execution(event.node_execution_id) workflow_node_execution = self._refetch_workflow_node_execution(event.node_execution_id)
inputs = WorkflowEntry.handle_special_values(event.inputs) inputs = WorkflowEntry.handle_special_values(event.inputs)
process_data = WorkflowEntry.handle_special_values(event.process_data)
outputs = WorkflowEntry.handle_special_values(event.outputs) outputs = WorkflowEntry.handle_special_values(event.outputs)
finished_at = datetime.now(timezone.utc).replace(tzinfo=None) finished_at = datetime.now(timezone.utc).replace(tzinfo=None)
elapsed_time = (finished_at - event.start_at).total_seconds() elapsed_time = (finished_at - event.start_at).total_seconds()
@ -320,7 +322,7 @@ class WorkflowCycleManage:
WorkflowNodeExecution.status: WorkflowNodeExecutionStatus.FAILED.value, WorkflowNodeExecution.status: WorkflowNodeExecutionStatus.FAILED.value,
WorkflowNodeExecution.error: event.error, WorkflowNodeExecution.error: event.error,
WorkflowNodeExecution.inputs: json.dumps(inputs) if inputs else None, WorkflowNodeExecution.inputs: json.dumps(inputs) if inputs else None,
WorkflowNodeExecution.process_data: json.dumps(event.process_data) if event.process_data else None, WorkflowNodeExecution.process_data: json.dumps(process_data) if event.process_data else None,
WorkflowNodeExecution.outputs: json.dumps(outputs) if outputs else None, WorkflowNodeExecution.outputs: json.dumps(outputs) if outputs else None,
WorkflowNodeExecution.finished_at: finished_at, WorkflowNodeExecution.finished_at: finished_at,
WorkflowNodeExecution.elapsed_time: elapsed_time, WorkflowNodeExecution.elapsed_time: elapsed_time,

View File

@ -18,6 +18,7 @@ from core.model_runtime.entities import (
from core.prompt.entities.advanced_prompt_entities import ChatModelMessage, CompletionModelPromptTemplate, MemoryConfig from core.prompt.entities.advanced_prompt_entities import ChatModelMessage, CompletionModelPromptTemplate, MemoryConfig
from core.prompt.prompt_transform import PromptTransform from core.prompt.prompt_transform import PromptTransform
from core.prompt.utils.prompt_template_parser import PromptTemplateParser from core.prompt.utils.prompt_template_parser import PromptTemplateParser
from core.workflow.entities.variable_pool import VariablePool
class AdvancedPromptTransform(PromptTransform): class AdvancedPromptTransform(PromptTransform):
@ -144,10 +145,11 @@ class AdvancedPromptTransform(PromptTransform):
raw_prompt = prompt_item.text raw_prompt = prompt_item.text
if prompt_item.edition_type == "basic" or not prompt_item.edition_type: if prompt_item.edition_type == "basic" or not prompt_item.edition_type:
parser = PromptTemplateParser(template=raw_prompt, with_variable_tmpl=self.with_variable_tmpl) vp = VariablePool()
prompt_inputs = {k: inputs[k] for k in parser.variable_keys if k in inputs} for k, v in inputs.items():
prompt_inputs = self._set_context_variable(context=context, parser=parser, prompt_inputs=prompt_inputs) vp.add(k[1:-1].split("."), v)
prompt = parser.format(prompt_inputs) raw_prompt.replace("{{#context#}}", context or "")
prompt = vp.convert_template(raw_prompt).text
elif prompt_item.edition_type == "jinja2": elif prompt_item.edition_type == "jinja2":
prompt = raw_prompt prompt = raw_prompt
prompt_inputs = inputs prompt_inputs = inputs

View File

@ -48,14 +48,16 @@ class VariablePool(BaseModel):
def __init__( def __init__(
self, self,
*, *,
system_variables: Mapping[SystemVariableKey, Any], system_variables: Mapping[SystemVariableKey, Any] | None = None,
user_inputs: Mapping[str, Any], user_inputs: Mapping[str, Any] | None = None,
environment_variables: Sequence[Variable] | None = None, environment_variables: Sequence[Variable] | None = None,
conversation_variables: Sequence[Variable] | None = None, conversation_variables: Sequence[Variable] | None = None,
**kwargs, **kwargs,
): ):
environment_variables = environment_variables or [] environment_variables = environment_variables or []
conversation_variables = conversation_variables or [] conversation_variables = conversation_variables or []
user_inputs = user_inputs or {}
system_variables = system_variables or {}
super().__init__( super().__init__(
system_variables=system_variables, system_variables=system_variables,