Method, device and equipment for enhancing workflow and medium

By encapsulating the workflow as a DAG (Directed Acyclic Graph) structure in Dify and executing the next node asynchronously, the problem of low execution efficiency in existing Dify workflows is solved, achieving multi-threaded parallelism and streaming output, thus improving the applicability of workflows and user experience.

CN119292563AActive Publication Date: 2025-01-10LINEWELL SOFTWARE
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202411126919.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-16
Publication Date
2025-01-10
Estimated Expiration
2044-08-16

AI Technical Summary

Technical Problem

The existing Dify workflow has low execution efficiency, can only meet the needs of a small number of applications, and the general workflow does not support streaming output, resulting in a poor user experience.

Method used

The workflow is encapsulated as a DAG (Directed Acyclic Graph) structure in the front-end page of Dify. The back-end obtains all next nodes based on the current node and executes them asynchronously when there are multiple next nodes. A new DirectStreamEvent event type is added to realize streaming output.

Benefits of technology

It improves workflow execution efficiency, supports multi-threaded parallel processing, is suitable for more application scenarios, and enhances workflow usability and user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119292563A_ABST
    Figure CN119292563A_ABST
Patent Text Reader

Abstract

The invention provides a method, a device, equipment and a medium for enhancing workflow, and the method comprises the steps: packaging the whole process of the workflow into a DAG directed acyclic graph structure on a front-end page of Dify, and submitting the DAG directed acyclic graph structure of the workflow to a rear end; modifying a method for acquiring a next operable node at the back end into a method for acquiring all next nodes from the DAG directed acyclic graph structure of the workflow according to the current node; when the back end executes the workflow, after the current node is executed, if only one next node exists, the next node is directly executed; and if at least two next nodes exist, taking each next node as a sub-workflow, and starting a thread for each sub-workflow for asynchronous execution. The method has the advantages that on one hand, the definition of the workflow is more flexible, on the other hand, the execution efficiency of the workflow can be effectively improved, and the method is suitable for requirements of more application scenes; and streaming output can be better supported.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method, device, equipment and medium for enhancing workflow. Background Art

[0002] Dify is a framework designed specifically for LLM (Large Language Model) application development. It integrates the concepts of Backend as Service and LLMOps, allowing developers to quickly build production-level generative AI applications that are suitable for various application scenarios that require natural language processing and machine learning.

[0003] The existing Dify workflow application will start a thread to asynchronously execute the workflow when running, and its return result is blocked. Although it provides concurrency to a certain extent, because each node of the workflow has only one input line and one output line, the execution efficiency of the workflow is low and can only meet the needs of a small number of applications; at the same time, the existing Dify general workflow does not support streaming output, and only large model nodes of the conversational workflow can stream output, which results in a poor user experience. Summary of the invention

[0004] The technical problem to be solved by the present invention is to provide a method, device, equipment and medium for enhancing workflow, so as to solve the problems that the existing Dify workflow has low execution efficiency, can only meet the needs of a small number of applications, and the general workflow does not support streaming output.

[0005] In a first aspect, the present invention provides a method for enhancing a workflow, the method comprising:

[0006] On the front-end page of Dify, encapsulate the entire workflow process into a DAG directed acyclic graph structure, and submit the DAG directed acyclic graph structure of the workflow to the back-end;

[0007] Modify the backend method for obtaining the next executable node to obtain all next nodes from the DAG structure of the workflow according to the current node;

[0008] When the backend executes the workflow, after executing the current node, if there is only one next node, the next node is executed directly; if there are at least two next nodes, each next node is treated as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution.

[0009] Furthermore, the method further comprises:

[0010] A new event type for implementing streaming output is added in Dify. When a node in the workflow requires streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and when the streaming message event is listened to, the content to be output is streamed to the front-end page of Dify.

[0011] Furthermore, the entire workflow process is encapsulated into a DAG directed acyclic graph structure on the front-end page of Dify, which specifically includes:

[0012] Modify the allowed pointing of the node on the front-end page of Dify so that one node is allowed to point to multiple nodes; create a workflow based on the modified allowed pointing of the node, and when saving the created workflow, encapsulate the entire process of the workflow into a DAG directed acyclic graph structure.

[0013] Furthermore, when the backend executes the workflow, after executing the current node, if there is only one next node, the next node is directly executed; if there are at least two next nodes, each next node is used as a sub-workflow, and a thread is opened for each sub-workflow to execute asynchronously. Specifically, it includes:

[0014] When the backend executes the workflow, it obtains all the next nodes of the current node. At the same time, in the method of obtaining the next node, it determines whether all the previous nodes of the current node have been executed. If not, it blocks and waits until all the previous nodes are executed; if so, it executes the logic of the current node;

[0015] Determine whether the next node of the current node is empty or the end node. If so, end the execution process of the workflow; if not, continue to determine whether the number of next nodes is 1. If so, after executing the current node, directly execute the logic of the next node; if not, after executing the current node, treat each next node as a sub-workflow, and start a thread for each sub-workflow for asynchronous execution. The asynchronous execution method adopts a recursive method.

[0016] Furthermore, the aforementioned adding a new event type in Dify for realizing streaming output, when a node in the workflow needs streaming output, encapsulating the content to be output according to the event type to generate a streaming message event, and streaming outputting the content to be output to the front-end page of Dify when the streaming message event is monitored specifically includes:

[0017] In Dify, an event-driven system is defined. The event-driven system includes three roles: events, event publishers, and event listeners.

[0018] Add an event type DirectStreamEvent for implementing streaming output in the event-driven system, and pass the event type for implementing streaming output to the nodes in the workflow that need to provide streaming output services through the event publisher, where the event type contains the content of each segment that needs to be streamed output;

[0019] When a node in the workflow needs streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and the generated streaming message event is pushed to the event queue through the event publisher;

[0020] The event listener loops to take events from the event queue. When it detects that the event taken out is a streaming message event, it takes out the content to be output from the streaming message event and returns the result through the yield generator. When the interface layer determines that the returned type is a generator, it loops to take out the content to be output from the generator and outputs the content to be output to the front-end page of Dify through SSE streaming.

[0021] In a second aspect, the present invention provides a device for enhancing a workflow, the device comprising a workflow encapsulation module, a method modification module and a workflow execution module;

[0022] The workflow encapsulation module is used to encapsulate the entire process of the workflow into a DAG directed acyclic graph structure on the front-end page of Dify, and submit the DAG directed acyclic graph structure of the workflow to the back-end;

[0023] The method modification module is used to modify the method used by the backend to obtain the next executable node to obtain all next nodes according to the current node from the DAG directed acyclic graph structure of the workflow;

[0024] The workflow execution module is used for back-end workflow execution. After executing the current node, if there is only one next node, the next node is directly executed; if there are at least two next nodes, each next node is used as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution.

[0025] Furthermore, the device also includes a streaming output module;

[0026] The streaming output module is used to add an event type for realizing streaming output in Dify. When a node in the workflow needs streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and when the streaming message event is monitored, the content to be output is streamed to the front-end page of Dify.

[0027] Further, in the workflow encapsulation module, encapsulating the entire workflow process into a DAG directed acyclic graph structure on the front-end page of Dify specifically includes: modifying the allowed pointing of the node on the front-end page of Dify so that one node is allowed to point to multiple nodes; creating a workflow based on the allowed pointing of the modified node, and when saving the created workflow, encapsulating the entire workflow process into a DAG directed acyclic graph structure;

[0028] The workflow execution module is specifically used for: when the backend executes the workflow, after executing the current node, if there is only one next node, then directly execute the next node; if there are at least two next nodes, then each next node is used as a sub-workflow, and a thread is opened for each sub-workflow to asynchronously execute the specific steps:

[0029] When the backend executes the workflow, it obtains all the next nodes of the current node. At the same time, in the method of obtaining the next node, it determines whether all the previous nodes of the current node have been executed. If not, it blocks and waits until all the previous nodes are executed; if so, it executes the logic of the current node;

[0030] Determine whether the next node of the current node is empty or the end node. If so, end the execution process of the workflow; if not, continue to determine whether the number of next nodes is 1. If so, after executing the current node, directly execute the logic of the next node; if not, after executing the current node, treat each next node as a sub-workflow, and start a thread for each sub-workflow for asynchronous execution. The asynchronous execution method adopts a recursive method.

[0031] Furthermore, the streaming output module is specifically used for:

[0032] In Dify, an event-driven system is defined. The event-driven system includes three roles: events, event publishers, and event listeners.

[0033] Add an event type DirectStreamEvent for implementing streaming output in the event-driven system, and pass the event type for implementing streaming output to the nodes in the workflow that need to provide streaming output services through the event publisher, where the event type contains the content of each segment that needs to be streamed output;

[0034] When a node in the workflow needs streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and the generated streaming message event is pushed to the event queue through the event publisher;

[0035] The event listener loops to take events from the event queue. When it detects that the event taken out is a streaming message event, it takes out the content to be output from the streaming message event and returns the result through the yield generator. When the interface layer determines that the returned type is a generator, it loops to take out the content to be output from the generator and outputs the content to be output to the front-end page of Dify through SSE streaming.

[0036] In a third aspect, the present invention provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method described in the first aspect when executing the program.

[0037] In a fourth aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in the first aspect.

[0038] One or more technical solutions provided in the embodiments of the present invention have at least the following technical effects or advantages:

[0039] 1. By modifying the allowed pointing of the node on the front-end page of Dify, a node is allowed to point to multiple nodes, and a workflow is created based on the allowed pointing of the modified node, and the entire process of the workflow is encapsulated into a DAG directed acyclic graph structure and submitted to the back end; at the same time, when the back end executes the workflow, when it is determined that there are multiple previous nodes of the current node, it will block and wait for all previous nodes to be executed; and when it is determined that there are multiple next nodes of the current node, each next node will be used as a sub-workflow, and a thread will be opened for each sub-workflow to execute asynchronously. By adopting the above technical solution of the present invention, Dify's workflow can support multi-line parallelism, which can make the definition of the workflow more flexible on the one hand, and can effectively improve the execution efficiency of the workflow on the other hand, and is applicable to the needs of more application scenarios.

[0040] 2. By transforming the existing Dify, a new event type DirectStreamEvent for realizing streaming output is added in Dify. When a node in a workflow needs streaming output, it is only necessary to encapsulate the content to be output according to the format content of DirectStreamEvent to generate a streaming message event. When the event listener listens to the streaming message event, the content to be output can be streamed to the front-end page of Dify through SSE. By adopting the above technical solution of the present invention, both conversational workflows and general workflows can support the streaming output of nodes, that is, large model nodes, tool nodes, http nodes, etc. can all realize streaming output, the availability of the workflow is higher, and the use experience can be significantly improved.

[0041] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] The present invention will be further described below in conjunction with embodiments with reference to the accompanying drawings.

[0043] Figure 1 This is a flowchart of an execution of a method for enhancing a workflow in Embodiment 1 of the present invention;

[0044] Figure 2 This is a schematic diagram of the structure of a device for enhancing workflow in Embodiment 2 of the present invention;

[0045] Figure 3 This is a schematic diagram of the structure of an electronic device in Embodiment 3 of the present invention;

[0046] Figure 4 Schematic diagram of the structure of the medium in the fourth embodiment of the present invention. DETAILED DESCRIPTION

[0047] In order to better understand the technical solution of the present invention, the technical solution of the present invention will be described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0048] Embodiment 1

[0049] This embodiment provides a method for enhancing workflow, such as Figure 1 As shown, the method is used to enhance and optimize the existing Dify workflow system to improve the performance of the workflow system.

[0050] The method comprises:

[0051] Step S11: encapsulate the entire workflow into a DAG directed acyclic graph structure on the front-end page of Dify, and submit the DAG directed acyclic graph structure of the workflow to the back-end, so that the back-end executes the workflow according to the DAG directed acyclic graph structure;

[0052] Step S12, modify the backend method for obtaining the next executable node to obtain all next nodes from the DAG structure of the workflow according to the current node, instead of returning only one node; because each node of the workflow in the existing Dify has only one input line and one output line, the existing method for obtaining the next executable node only returns one node each time. In order to enable the workflow to support multi-line parallelism, the backend method for obtaining the next executable node needs to be modified;

[0053] Step S13, when the backend executes the workflow, after executing the current node, if there is only one next node, the next node is executed directly; if there are at least two next nodes, each next node is regarded as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution, that is, when there are multiple next nodes, the backend will start a thread pool and assign a thread to each next node as a new sub-workflow for asynchronous execution.

[0054] In an embodiment of the present invention, encapsulating the entire workflow process into a DAG directed acyclic graph structure on the front-end page of Dify specifically includes:

[0055] Modify the allowed pointing of the node on the front-end page of Dify so that one node is allowed to point to multiple nodes; create a workflow based on the modified allowed pointing of the node, and when saving the created workflow, encapsulate the entire process of the workflow into a DAG directed acyclic graph structure.

[0056] Since each node of the existing Dify workflow has only one input line and one output line, in order to enable the workflow to support multi-line parallelism, the logic of the existing workflow needs to be optimized. To this end, the present invention first modifies the allowed pointing of the node so that one node is allowed to point to multiple nodes; then, the workflow is created based on the modified allowed pointing of the node, and the entire process of the workflow is encapsulated into a DAG directed acyclic graph structure, so that multi-line parallel processing can be achieved according to the DAG directed acyclic graph structure when the workflow is subsequently executed.

[0057] DAG stands for directed acyclic graph, which is a data structure consisting of a set of nodes and directed edges without loops (i.e., there is no situation where a path starts from a node and passes through a series of edges and eventually returns to the same node). DAG is widely used in computer science and many other fields. In a DAG directed acyclic graph, each node represents a task or operation, and directed edges represent dependencies between tasks.

[0058] In an embodiment of the present invention, when the backend executes the workflow, after executing the current node, if there is only one next node, the next node is directly executed; if there are at least two next nodes, each next node is used as a sub-workflow, and a thread is opened for each sub-workflow to execute asynchronously. Specifically, the following steps are included:

[0059] When the backend executes the workflow, it obtains all the next nodes of the current node. At the same time, in the method of obtaining the next node, it determines whether all the previous nodes of the current node have been executed. If not, it blocks and waits until all the previous nodes are executed. If so, it executes the logic of the current node, that is, generates a node instance according to the node type of the current node. Because in the specific implementation, when there are multiple outgoing lines, it means that there may also be multiple incoming lines. Therefore, in the method of obtaining the next node, it is also necessary to determine whether there are multiple previous nodes of the current node. If there are multiple, it is necessary to block and wait until all the previous nodes are executed before executing the logic of the current node, that is, it is necessary to wait until all the previous nodes are executed before executing the logic of the current node.

[0060] Determine whether the next node of the current node is empty or the end node. If so, end the execution process of the workflow; if not, continue to determine whether the number of the next node is 1. If so, directly execute the logic of the next node after executing the current node; if not, after executing the current node, treat each next node as a sub-workflow, and open a thread for each sub-workflow to execute asynchronously, that is, treat the line where each next node is located as a new sub-workflow for asynchronous execution, until each line is executed, and then summarize and return the results; among them, the asynchronous execution method adopts a recursive method, that is, the recursive method is used to call the logic of the workflow operation, and it only needs to be modified to start running from the current node.

[0061] The present invention modifies the allowed pointing of the node on the front-end page of Dify, so that a node is allowed to point to multiple nodes, and creates a workflow based on the allowed pointing of the modified node, and encapsulates the entire process of the workflow into a DAG directed acyclic graph structure and submits it to the back end; at the same time, when the back end executes the workflow, when it is determined that there are multiple previous nodes of the current node, it will block and wait for all previous nodes to be executed; and when it is determined that there are multiple next nodes of the current node, each next node will be used as a sub-workflow, and a thread will be opened for each sub-workflow to execute asynchronously. By adopting the above technical solution of the present invention, Dify's workflow can support multi-line parallelism, which can make the definition of the workflow more flexible on the one hand, and can effectively improve the execution efficiency of the workflow on the other hand, and is applicable to the needs of more application scenarios.

[0062] In some embodiments of the present invention, in order to enable all nodes in the workflow to respond to the front-end page in a streaming manner and improve the user experience, the method further includes:

[0063] Step S2: Add an event type for stream output in Dify. When a node in the workflow needs stream output, encapsulate the content to be output according to the event type to generate a stream message event, and stream the content to be output to the front-end page of Dify when the stream message event is monitored. It should be noted that there is no sequential execution order between step S2 and steps S11-S13 in the present invention, and step S2 can be executed independently according to actual needs.

[0064] Furthermore, a new event type for implementing streaming output is added in Dify. When a node in a workflow needs streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and when the streaming message event is monitored, the content to be output is streamed to the front-end page of Dify. Specifically, it includes:

[0065] In Dify, an event-driven system is defined. The event-driven system includes three roles: events, event publishers, and event listeners. Among them, the event publisher is a method that can add events to the event queue, and the event listener is used to obtain events from the event queue and listen to event types.

[0066] A new event type DirectStreamEvent for implementing streaming output is added to the event-driven system, and the event type for implementing streaming output is passed to the node in the workflow that needs to provide streaming output service through the event publisher, so that the business service of the node can customize whether streaming output is required and at which specific time point; wherein, the event type DirectStreamEvent contains each segmented content that needs to be streamed output, and by designing the event type to contain multiple segmented contents that need to be streamed output, the content to be output can be split into multiple segmented contents for processing during specific implementation, thereby improving processing efficiency; it should be noted that: DirectStreamEvent in the present invention represents an event type that needs streaming output, which is a class in Python, which stores information such as the text content and type to be streamed output;

[0067] When a node in the workflow needs streaming output, the content to be output is encapsulated according to the event type (i.e., the format content of DirectStreamEvent) to generate a streaming message event, and the generated streaming message event is pushed to the event queue through the event publisher; the streaming message event contains information such as the node ID, the generated content, and the type of message;

[0068] The event listener loops to take events from the event queue. When it detects that the event taken out is a streaming message event, it takes out the content to be output from the streaming message event and returns the result through the yield generator. Yield is the usage of generators in Python, which can return part of the results before the business logic is executed. When the interface layer determines that the returned type is a generator, it loops to take out the content to be output from the generator and outputs the content to be output to the front-end page of Dify through SSE streaming.

[0069] The present invention transforms the existing Dify and adds a new event type DirectStreamEvent in Dify for realizing streaming output. When a node in a workflow needs streaming output, it only needs to encapsulate the content to be output according to the format content of DirectStreamEvent to generate a streaming message event, and when the event listener listens to the streaming message event, the content to be output can be streamed to the front-end page of Dify through SSE. By adopting the above technical solution of the present invention, both conversational workflows and general workflows can support the streaming output of nodes, that is, large model nodes, tool nodes, http nodes, etc. can all realize streaming output, the availability of the workflow is higher, and the use experience can be significantly improved.

[0070] Based on the same inventive concept, the present application also provides a device corresponding to the method in Example 1, see Example 2 for details.

[0071] Embodiment 2

[0072] In this embodiment, a device for enhancing workflow is provided, such as Figure 2 As shown, the device is used to enhance and optimize the existing Dify workflow system to improve the performance of the workflow system.

[0073] The device includes a workflow encapsulation module, a method modification module and a workflow execution module;

[0074] The workflow encapsulation module is used to encapsulate the entire process of the workflow into a DAG directed acyclic graph structure on the front-end page of Dify, and submit the DAG directed acyclic graph structure of the workflow to the back-end, so that the back-end executes the workflow according to the DAG directed acyclic graph structure;

[0075] The method modification module is used to modify the method used by the backend to obtain the next executable node, so that all next nodes are obtained from the DAG structure of the workflow according to the current node, instead of returning only one node. Because each node of the workflow in the existing Dify has only one input line and one output line, the existing method for obtaining the next executable node only returns one node each time. In order to enable the workflow to support multi-line parallelism, the method used by the backend to obtain the next executable node needs to be modified.

[0076] The workflow execution module is used for back-end workflow execution. After executing the current node, if there is only one next node, the next node is executed directly; if there are at least two next nodes, each next node is used as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution, that is, when there are multiple next nodes, the back-end will start a thread pool and assign a thread to each next node as a new sub-workflow for asynchronous execution.

[0077] In an embodiment of the present invention, in the workflow encapsulation module, encapsulating the entire workflow process into a DAG directed acyclic graph structure on the front-end page of Dify specifically includes:

[0078] Modify the allowed pointing of the node on the front-end page of Dify so that one node is allowed to point to multiple nodes; create a workflow based on the modified allowed pointing of the node, and when saving the created workflow, encapsulate the entire process of the workflow into a DAG directed acyclic graph structure.

[0079] Since each node of the existing Dify workflow has only one input line and one output line, in order to enable the workflow to support multi-line parallelism, the logic of the existing workflow needs to be optimized. To this end, the present invention first modifies the allowed pointing of the node so that one node is allowed to point to multiple nodes; then, the workflow is created based on the modified allowed pointing of the node, and the entire process of the workflow is encapsulated into a DAG directed acyclic graph structure, so that when the workflow is subsequently executed, multi-line parallel processing can be achieved according to the DAG directed acyclic graph structure.

[0080] DAG stands for directed acyclic graph, which is a data structure consisting of a set of nodes and directed edges without loops (i.e., there is no situation where a path starts from a node and passes through a series of edges and eventually returns to the same node). DAG is widely used in computer science and many other fields. In a DAG directed acyclic graph, each node represents a task or operation, and directed edges represent dependencies between tasks.

[0081] In an embodiment of the present invention, the workflow execution module is specifically used to:

[0082] When the backend executes the workflow, it obtains all the next nodes of the current node. At the same time, in the method of obtaining the next node, it determines whether all the previous nodes of the current node have been executed. If not, it blocks and waits until all the previous nodes are executed. If so, it executes the logic of the current node, that is, generates a node instance according to the node type of the current node. Because in the specific implementation, when there are multiple outgoing lines, it means that there may also be multiple incoming lines. Therefore, in the method of obtaining the next node, it is also necessary to determine whether there are multiple previous nodes of the current node. If there are multiple, it is necessary to block and wait until all the previous nodes are executed before executing the logic of the current node, that is, it is necessary to wait until all the previous nodes are executed before executing the logic of the current node.

[0083] Determine whether the next node of the current node is empty or the end node. If so, end the execution process of the workflow; if not, continue to determine whether the number of the next node is 1. If so, directly execute the logic of the next node after executing the current node; if not, after executing the current node, treat each next node as a sub-workflow, and open a thread for each sub-workflow to execute asynchronously, that is, treat the line where each next node is located as a new sub-workflow for asynchronous execution, until each line is executed, and then summarize and return the results; among them, the asynchronous execution method adopts a recursive method, that is, the recursive method is used to call the logic of the workflow operation, and it only needs to be modified to start running from the current node.

[0084] The present invention modifies the allowed pointing of the node on the front-end page of Dify, so that a node is allowed to point to multiple nodes, and creates a workflow based on the allowed pointing of the modified node, and encapsulates the entire process of the workflow into a DAG directed acyclic graph structure and submits it to the back end; at the same time, when the back end executes the workflow, when it is determined that there are multiple previous nodes of the current node, it will block and wait for all previous nodes to be executed; and when it is determined that there are multiple next nodes of the current node, each next node will be used as a sub-workflow, and a thread will be opened for each sub-workflow to execute asynchronously. By adopting the above technical solution of the present invention, Dify's workflow can support multi-line parallelism, which can make the definition of the workflow more flexible on the one hand, and can effectively improve the execution efficiency of the workflow on the other hand, and is applicable to the needs of more application scenarios.

[0085] In some embodiments of the present invention, in order to enable all nodes in the workflow to respond to the front-end page in a streaming manner and improve the user experience, the device further includes a streaming output module;

[0086] The streaming output module is used to add an event type for implementing streaming output in Dify. When a node in the workflow needs streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and when the streaming message event is monitored, the content to be output is streamed to the front-end page of Dify. It should be noted that there is no sequential execution order between the streaming output module and the workflow encapsulation module, the method modification module and the workflow execution module in the present invention, and the streaming output module can be executed independently according to actual needs.

[0087] Furthermore, the streaming output module is specifically used for:

[0088] In Dify, an event-driven system is defined. The event-driven system includes three roles: events, event publishers, and event listeners. Among them, the event publisher is a method that can add events to the event queue, and the event listener is used to obtain events from the event queue and listen to event types.

[0089] A new event type DirectStreamEvent for implementing streaming output is added to the event-driven system, and the event type for implementing streaming output is passed to the node in the workflow that needs to provide streaming output service through the event publisher, so that the business service of the node can customize whether streaming output is required and at which specific time point; wherein, the event type includes each segmented content that needs to be streamed output, and by designing the event type to include multiple segmented contents that need to be streamed output, the content to be output can be split into multiple segmented contents for processing during specific implementation, thereby improving processing efficiency; it should be noted that: DirectStreamEvent in the present invention represents an event type that needs streaming output, which is a class in Python, which stores information such as the text content and type to be streamed output;

[0090] When a node in the workflow needs streaming output, the content to be output is encapsulated according to the event type (i.e., the format content of DirectStreamEvent) to generate a streaming message event, and the generated streaming message event is pushed to the event queue through the event publisher; the streaming message event contains information such as the node ID, the generated content, and the type of message;

[0091] The event listener loops to take events from the event queue. When it detects that the event taken out is a streaming message event, it takes out the content to be output from the streaming message event and returns the result through the yield generator. Yield is the usage of generators in Python, which can return part of the results before the business logic is executed. When the interface layer determines that the returned type is a generator, it loops to take out the content to be output from the generator and outputs the content to be output to the front-end page of Dify through SSE streaming.

[0092] The present invention transforms the existing Dify and adds a new event type DirectStreamEvent in Dify for realizing streaming output. When a node in a workflow needs streaming output, it only needs to encapsulate the content to be output according to the format content of DirectStreamEvent to generate a streaming message event, and when the event listener listens to the streaming message event, the content to be output can be streamed to the front-end page of Dify through SSE. By adopting the above technical solution of the present invention, both conversational workflows and general workflows can support the streaming output of nodes, that is, large model nodes, tool nodes, http nodes, etc. can all realize streaming output, the availability of the workflow is higher, and the use experience can be significantly improved.

[0093] Based on the same inventive concept, the present application provides an electronic device embodiment corresponding to the first embodiment, see the third embodiment for details.

[0094] Embodiment 3

[0095] This embodiment provides an electronic device, such as Figure 3 As shown, it includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, any implementation method in the first embodiment can be implemented.

[0096] Since the electronic device introduced in this embodiment is a device used to implement the method in the first embodiment of the present application, based on the method introduced in the first embodiment of the present application, a person skilled in the art can understand the specific implementation of the electronic device of the present embodiment and its various variations, so how the electronic device implements the method in the embodiment of the present application is not described in detail here. As long as a person skilled in the art implements the device used by the method in the embodiment of the present application, it belongs to the scope of protection of the present application.

[0097] Based on the same inventive concept, the present application provides a storage medium corresponding to the first embodiment, see the fourth embodiment for details.

[0098] Embodiment 4

[0099] This embodiment provides a computer-readable storage medium, such as Figure 4 As shown, a computer program is stored thereon, and when the computer program is executed by a processor, any implementation method in Example 1 can be implemented.

[0100] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, devices, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0101] The present invention is described with reference to flowcharts and / or block diagrams of methods, apparatuses, and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as a combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the process in the flowchart. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0102] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0103] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0104] Although the specific implementation modes of the present invention are described above, those skilled in the art should understand that the specific implementation modes described are only illustrative and are not intended to limit the scope of the present invention. Equivalent modifications and changes made by those skilled in the art in accordance with the spirit of the present invention should be included in the scope of protection of the claims of the present invention.

Claims

1. A method for enhancing workflow, characterized in that: The method comprises: On the front-end page of Dify, encapsulate the entire workflow process into a DAG directed acyclic graph structure, and submit the DAG directed acyclic graph structure of the workflow to the back-end; Modify the backend method for obtaining the next executable node to obtain all next nodes from the DAG structure of the workflow according to the current node; When the backend executes the workflow, after executing the current node, if there is only one next node, the next node is executed directly; if there are at least two next nodes, each next node is treated as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution.

2. A method for enhancing workflow according to claim 1, characterized in that: The method further comprises: A new event type for implementing streaming output is added in Dify. When a node in the workflow requires streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and when the streaming message event is listened to, the content to be output is streamed to the front-end page of Dify.

3. A method for enhancing workflow according to claim 1, characterized in that: The encapsulation of the entire workflow process into a DAG directed acyclic graph structure on the front-end page of Dify specifically includes: Modify the allowed pointing of the node on the front-end page of Dify so that one node is allowed to point to multiple nodes; create a workflow based on the modified allowed pointing of the node, and when saving the created workflow, encapsulate the entire process of the workflow into a DAG directed acyclic graph structure.

4. The method for enhancing workflow according to claim 1, characterized in that: When the backend executes the workflow, after executing the current node, if there is only one next node, the next node is directly executed; if there are at least two next nodes, each next node is used as a sub-workflow, and a thread is opened for each sub-workflow to execute asynchronously. Specifically, it includes: When the backend executes the workflow, it obtains all the next nodes of the current node. At the same time, in the method of obtaining the next node, it determines whether all the previous nodes of the current node have been executed. If not, it blocks and waits until all the previous nodes are executed; if so, it executes the logic of the current node; Determine whether the next node of the current node is empty or the end node. If so, end the execution process of the workflow; if not, continue to determine whether the number of next nodes is 1. If so, after executing the current node, directly execute the logic of the next node; if not, after executing the current node, treat each next node as a sub-workflow, and start a thread for each sub-workflow for asynchronous execution. The asynchronous execution method adopts a recursive method.

5. A method for enhancing workflow according to claim 2, characterized in that: The above-mentioned adding a new event type in Dify for realizing streaming output, when a node in the workflow needs streaming output, encapsulating the content to be output according to the event type to generate a streaming message event, and streaming outputting the content to be output to the front-end page of Dify when the streaming message event is monitored specifically includes: In Dify, an event-driven system is defined. The event-driven system includes three roles: events, event publishers, and event listeners. Add an event type DirectStreamEvent for implementing streaming output in the event-driven system, and pass the event type for implementing streaming output to the nodes in the workflow that need to provide streaming output services through the event publisher, where the event type contains the content of each segment that needs to be streamed output; When a node in the workflow needs streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and the generated streaming message event is pushed to the event queue through the event publisher; The event listener loops to take events from the event queue. When it detects that the event taken out is a streaming message event, it takes out the content to be output from the streaming message event and returns the result through the yield generator. When the interface layer determines that the returned type is a generator, it loops to take out the content to be output from the generator and outputs the content to be output to the front-end page of Dify through SSE streaming.

6. A device for enhancing workflow, characterized in that: The device includes a workflow encapsulation module, a method modification module and a workflow execution module; The workflow encapsulation module is used to encapsulate the entire process of the workflow into a DAG directed acyclic graph structure on the front-end page of Dify, and submit the DAG directed acyclic graph structure of the workflow to the back-end; The method modification module is used to modify the method used by the backend to obtain the next executable node to obtain all next nodes according to the current node from the DAG directed acyclic graph structure of the workflow; The workflow execution module is used for back-end workflow execution. After executing the current node, if there is only one next node, the next node is directly executed; if there are at least two next nodes, each next node is used as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution.

7. The device for enhancing workflow according to claim 6, characterized in that: The device also includes a streaming output module; The streaming output module is used to add an event type for realizing streaming output in Dify. When a node in the workflow needs streaming output, the content to be output is encapsulated according to the event type to generate a streaming message event, and when the streaming message event is monitored, the content to be output is streamed to the front-end page of Dify.

8. The device for enhancing workflow according to claim 6, characterized in that: In the workflow encapsulation module, the encapsulation of the entire workflow process into a DAG directed acyclic graph structure on the front-end page of Dify specifically includes: modifying the allowed pointing of the node on the front-end page of Dify so that one node is allowed to point to multiple nodes; creating a workflow based on the allowed pointing of the modified node, and when saving the created workflow, encapsulating the entire workflow process into a DAG directed acyclic graph structure; The workflow execution module is specifically used for: when the backend executes the workflow, obtaining all the next nodes of the current node, and in the method of obtaining the next node, determining whether all the previous nodes of the current node have been executed, if not, blocking and waiting until all the previous nodes are executed; if yes, executing the logic of the current node; Determine whether the next node of the current node is empty or the end node. If so, end the execution process of the workflow; if not, continue to determine whether the number of next nodes is 1. If so, after executing the current node, directly execute the logic of the next node; if not, after executing the current node, treat each next node as a sub-workflow, and start a thread for each sub-workflow for asynchronous execution. The asynchronous execution method adopts a recursive method.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method according to any one of claims 1 to 5 is implemented.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.

Citation Information

Patent Citations

  • Workflow engine management method, system and device and storage medium

    CN111898892A

  • Working method of workflow engine, server, medium and equipment

    CN112712335A

  • Directed acyclic graph workflow engine cycle scheduling method, device and equipment

    CN117610320A

  • Computer device, method and apparatus for scheduling service process

    WO2015070705A1