langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
45.98k stars 6.48k forks source link

Seeking Suggestions to Optimize Workflow Execution Time for If/Else Logic #8579

Open myoss opened 2 hours ago

myoss commented 2 hours ago

Self Checks

Dify version

0.8.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

I am currently experiencing slow execution in my workflow, where the if/else logic takes 60 milliseconds to execute. What are some good suggestions to reduce this time? Or are there any directions for troubleshooting the issue? SCR-20240920-nchi-2

✔️ Expected Behavior

No response

❌ Actual Behavior

No response

dosubot[bot] commented 2 hours ago

@myoss Hello! I'm here to help you with any bugs, questions, or contributions. Let's work together to solve this issue.

I found a similar closed issue that might be relevant to your problem: Fork Join Parallelism for Workflows. This issue was last updated on September 04, 2024, and it references other related issues like batched data, need multiple branch supported, and Feature Request: Add "Parallel" Execution Capability to the LLM Module. The suggested solution can be found in the 0.8.0-beta1 release notes [1].

To optimize the workflow execution time for if/else logic in your self-hosted Dify version 0.8.2 environment using Docker, consider the following strategies:

  1. Reduce the Number of Conditions: Simplify the conditions in the IfElseNode configuration to reduce computational overhead.
  2. Optimize Variable Pool Access: Ensure efficient access to the variable pool by optimizing how variables are stored and retrieved.
  3. Minimize Database Interactions: Ensure that any real database interactions are minimized or optimized.
  4. Utilize the Code Node for Complex Logic: Offload complex if/else logic to a custom Python or JavaScript script within a code node to streamline the workflow and reduce execution time [2][3].

Here is an example of how you might refactor the IfElseNode configuration to reduce the number of conditions:

node = IfElseNode(
    id=str(uuid.uuid4()),
    graph_init_params=init_params,
    graph=graph,
    graph_runtime_state=GraphRuntimeState(variable_pool=pool, start_at=time.perf_counter()),
    config={
        "id": "if-else",
        "data": {
            "title": "123",
            "type": "if-else",
            "logical_operator": "and",
            "conditions": [
                {
                    "comparison_operator": "contains",
                    "variable_selector": ["start", "array_contains"],
                    "value": "ab",
                },
                {
                    "comparison_operator": "not contains",
                    "variable_selector": ["start", "array_not_contains"],
                    "value": "ab",
                },
                # Remove or combine other conditions as needed
            ],
        },
    },
)

Additionally, ensure efficient variable management:

# Optimize variable pool initialization
pool = VariablePool(
    system_variables={SystemVariableKey.FILES: [], SystemVariableKey.USER_ID: "aaa"},
    user_inputs={}
)
pool.add(["start", "array_contains"], ["ab", "def"])
pool.add(["start", "array_not_contains"], ["ac", "def"])
# Add only necessary variables

For more complex logic, consider using the code node:

def main(variable1: list, variable2: list) -> dict:
    # Custom logic to replace complex if/else conditions
    result = some_complex_logic(variable1, variable2)
    return {
        'result': result
    }

By reducing the complexity of the conditions, ensuring efficient variable management, and leveraging the code node for complex logic, you can potentially reduce the execution time of the if/else logic.

To continue talking to Dosu, mention @dosu.

myoss commented 2 hours ago

image This does not involve complex conditional checks; it simply checks whether the input parameter string is equal to a specific value.