mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-10 11:26:52 +08:00
Feat/environment variables in workflow (#6515)
Co-authored-by: JzoNg <jzongcode@gmail.com>
This commit is contained in:
@@ -3,12 +3,46 @@ from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
from core.workflow.entities.variable_entities import VariableSelector
|
||||
from core.workflow.entities.variable_pool import VariablePool
|
||||
|
||||
REGEX = re.compile(r"\{\{(#[a-zA-Z0-9_]{1,50}(\.[a-zA-Z_][a-zA-Z0-9_]{0,29}){1,10}#)\}\}")
|
||||
REGEX = re.compile(r'\{\{(#[a-zA-Z0-9_]{1,50}(\.[a-zA-Z_][a-zA-Z0-9_]{0,29}){1,10}#)\}\}')
|
||||
|
||||
|
||||
def parse_mixed_template(*, template: str, variable_pool: VariablePool) -> str:
|
||||
"""
|
||||
This is an alternative to the VariableTemplateParser class,
|
||||
offering the same functionality but with better readability and ease of use.
|
||||
"""
|
||||
variable_keys = [match[0] for match in re.findall(REGEX, template)]
|
||||
variable_keys = list(set(variable_keys))
|
||||
|
||||
# This key_selector is a tuple of (key, selector) where selector is a list of keys
|
||||
# e.g. ('#node_id.query.name#', ['node_id', 'query', 'name'])
|
||||
key_selectors = filter(
|
||||
lambda t: len(t[1]) >= 2,
|
||||
((key, selector.replace('#', '').split('.')) for key, selector in zip(variable_keys, variable_keys)),
|
||||
)
|
||||
inputs = {key: variable_pool.get_any(selector) for key, selector in key_selectors}
|
||||
|
||||
def replacer(match):
|
||||
key = match.group(1)
|
||||
# return original matched string if key not found
|
||||
value = inputs.get(key, match.group(0))
|
||||
if value is None:
|
||||
value = ''
|
||||
value = str(value)
|
||||
# remove template variables if required
|
||||
return re.sub(REGEX, r'{\1}', value)
|
||||
|
||||
result = re.sub(REGEX, replacer, template)
|
||||
result = re.sub(r'<\|.*?\|>', '', result)
|
||||
return result
|
||||
|
||||
|
||||
class VariableTemplateParser:
|
||||
"""
|
||||
!NOTE: Consider to use the new `segments` module instead of this class.
|
||||
|
||||
A class for parsing and manipulating template variables in a string.
|
||||
|
||||
Rules:
|
||||
@@ -72,14 +106,11 @@ class VariableTemplateParser:
|
||||
if len(split_result) < 2:
|
||||
continue
|
||||
|
||||
variable_selectors.append(VariableSelector(
|
||||
variable=variable_key,
|
||||
value_selector=split_result
|
||||
))
|
||||
variable_selectors.append(VariableSelector(variable=variable_key, value_selector=split_result))
|
||||
|
||||
return variable_selectors
|
||||
|
||||
def format(self, inputs: Mapping[str, Any], remove_template_variables: bool = True) -> str:
|
||||
def format(self, inputs: Mapping[str, Any]) -> str:
|
||||
"""
|
||||
Formats the template string by replacing the template variables with their corresponding values.
|
||||
|
||||
@@ -90,6 +121,7 @@ class VariableTemplateParser:
|
||||
Returns:
|
||||
The formatted string with template variables replaced by their values.
|
||||
"""
|
||||
|
||||
def replacer(match):
|
||||
key = match.group(1)
|
||||
value = inputs.get(key, match.group(0)) # return original matched string if key not found
|
||||
@@ -99,11 +131,9 @@ class VariableTemplateParser:
|
||||
# convert the value to string
|
||||
if isinstance(value, list | dict | bool | int | float):
|
||||
value = str(value)
|
||||
|
||||
|
||||
# remove template variables if required
|
||||
if remove_template_variables:
|
||||
return VariableTemplateParser.remove_template_variables(value)
|
||||
return value
|
||||
return VariableTemplateParser.remove_template_variables(value)
|
||||
|
||||
prompt = re.sub(REGEX, replacer, self.template)
|
||||
return re.sub(r'<\|.*?\|>', '', prompt)
|
||||
|
||||
Reference in New Issue
Block a user