Method, device, equipment and medium for enhancing workflow
By encapsulating the workflow into a DAG directed acyclic graph structure on the Dify front-end page, asynchronously executing multiple next nodes on the back-end, and adding the DirectStreamEvent event type to achieve streaming output, the problems of low execution efficiency and lack of support for streaming output of Dify workflows are solved, thereby improving the execution efficiency and usability of the workflow.
Patent Information
- Application Number
- CN202411126919.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-16
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2044-08-16
AI Technical Summary
The existing Dify workflow has low execution efficiency and can only meet the needs of a small number of applications. The general workflow does not support streaming output and the user experience is poor.
On the Dify front-end page, the workflow is encapsulated into a DAG directed acyclic graph structure, and the allowed pointing of the node is modified to allow it to point to multiple nodes. During execution, the back-end obtains all next nodes based on the DAG directed acyclic graph structure and executes asynchronously when multiple next nodes exist. A new DirectStreamEvent event type is added to implement streaming output.
It improves the execution efficiency of workflows, supports multi-threaded parallelism, is suitable for more application scenarios, improves the usability and user experience of workflows, and supports streaming output.
Smart Images

Figure CN119292563B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method, apparatus, device 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 a Service (BAS) and LLMOps, enabling developers to quickly build production-grade generative AI applications that are applicable to various application scenarios requiring natural language processing and machine learning.
[0003] Existing Dify workflow applications start a thread to asynchronously execute the workflow at runtime, and their return results are blocked. Although this provides a certain degree of concurrency, because each node in the workflow has only one input line and one output line, the workflow execution efficiency 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 in the conversational workflow can stream output, resulting 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 of low execution efficiency of existing Dify workflow, which can only meet the needs of a small number of applications and general workflow does not support streaming output.
[0005] In a first aspect, the present invention provides a method for enhancing workflow, the method comprising:
[0006] On the Dify front-end page, encapsulate the entire workflow process into a DAG structure and submit the DAG structure to the back-end.
[0007] Modify the backend method for obtaining the next runnable node to obtain all next nodes based on the current node from the workflow's DAG structure;
[0008] When the backend executes the workflow, after executing the current node, if there is only one next node, the next node will be executed directly; if there are at least two next nodes, each next node will be treated as a sub-workflow, and a thread will be opened for each sub-workflow to execute asynchronously.
[0009] Furthermore, the method further comprises:
[0010] A new event type for implementing streaming output has been added to 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. When the streaming message event is monitored, the content to be output is streamed to the Dify front-end page.
[0011] Furthermore, the entire workflow process is encapsulated into a DAG structure on the front-end page of Dify, specifically including:
[0012] Modify the allowed pointing of a 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 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 to execute asynchronously. Specifically, the following steps are performed:
[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 open a thread for each sub-workflow for asynchronous execution. The asynchronous execution method adopts a recursive method.
[0016] Furthermore, a new event type for implementing streaming output is added to 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. When the streaming message event is monitored, the content to be output is streamed to the front-end page of Dify. Specifically, the following steps are included:
[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 to the event-driven system for implementing streaming output. This event type is delivered to the nodes in the workflow that need to provide streaming output services through the event publisher. The event type contains the content of each segment that needs to be streamed.
[0019] 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 the generated streaming message event is pushed to the event queue through the event publisher;
[0020] The event listener cyclically retrieves events from the event queue. When it detects that the retrieved event is a streaming message event, it retrieves 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 cyclically retrieves 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 workflow process 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 backend method for obtaining the next runnable node to obtain all next nodes according to the current node from the DAG structure of the workflow;
[0024] The workflow execution module is used to execute the workflow on the back end. 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.
[0025] Furthermore, the device also includes a streaming output module;
[0026] The streaming output module is used to add an event type in Dify for implementing streaming output. 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. When the streaming message event is monitored, the content to be output is streamed to the front-end page of Dify.
[0027] Furthermore, 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 a 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 modified allowed pointing of the 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 to: 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 regarded as a sub-workflow, and a thread is opened for each sub-workflow to execute asynchronously. Specifically including:
[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 open 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 to:
[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 to the event-driven system for implementing streaming output. This event type is delivered to the nodes in the workflow that need to provide streaming output services through the event publisher. The event type contains the content of each segment that needs to be streamed.
[0034] 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 the generated streaming message event is pushed to the event queue through the event publisher;
[0035] The event listener cyclically retrieves events from the event queue. When it detects that the retrieved event is a streaming message event, it retrieves 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 cyclically retrieves 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 implements the method described in the first aspect when the program is executed by a processor.
[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 meet the needs of more application scenarios.
[0040] 2. By modifying the existing Dify, a new event type DirectStreamEvent for implementing streaming output is added to Dify. When a node in the workflow requires streaming output, it is only necessary to encapsulate the content to be output according to the format 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-mentioned technical solution of the present invention, both conversational workflows and general workflows can support node streaming output, that is, large model nodes, tool nodes, http nodes, etc. can all achieve streaming output, which increases the usability of the workflow and significantly improves the user experience.
[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 in accordance with 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 specifically listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] The present invention will be further described below with reference to the accompanying drawings and embodiments.
[0043] Figure 1 This is a flowchart of a method for enhancing workflow in Embodiment 1 of the present invention;
[0044] Figure 2 This is a schematic structural diagram of a device for enhancing workflow in a second embodiment of the present invention;
[0045] Figure 3 This is a schematic structural diagram of an electronic device in a third embodiment 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 with reference to the accompanying drawings and specific implementation methods. Example
[0048] 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.
[0049] The method comprises:
[0050] Step S11: Encapsulate the entire workflow process into a DAG structure on the front-end page of Dify, and submit the DAG structure of the workflow to the back-end so that the back-end executes the workflow according to the DAG structure.
[0051] Step S12: Modify the backend method for obtaining the next executable node to obtain all next nodes from the workflow's DAG structure based on the current node, rather than returning only one node. Because each node in the existing Dify workflow has only one input line and one output line, the existing method for obtaining the next executable node only returns one node at a time. In order to enable the workflow to support multiple parallel threads, the backend method for obtaining the next executable node needs to be modified.
[0052] 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 treated 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.
[0053] In an embodiment of the present invention, encapsulating the entire workflow process into a DAG structure on the front-end page of Dify specifically includes:
[0054] Modify the allowed pointing of a 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.
[0055] Because each node in the existing Dify workflow has only one input and one output, the existing workflow logic needs to be optimized to support multi-threaded parallel execution. To this end, the present invention first modifies the allowed node pointers, allowing a node to point to multiple nodes. Then, based on the modified allowed node pointers, a workflow is created and the entire workflow process is encapsulated into a DAG (Directed Acyclic Graph) structure. This allows subsequent workflow execution to achieve multi-threaded parallel processing based on the DAG structure.
[0056] DAG, short for Directed Acyclic Graph, is a data structure consisting of a set of nodes and directed edges with no cycles (i.e., a path from one node to another through a series of edges never returns to the same node). DAG is widely used in computer science and many other fields. In a DAG, each node represents a task or operation, and directed edges represent dependencies between tasks.
[0057] In an embodiment of the present invention, when the backend executes a 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 treated as a sub-workflow, and a thread is opened for each sub-workflow to execute asynchronously. Specifically, the following steps are performed:
[0058] 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 also means that there may 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.
[0059] 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 open a thread for each sub-workflow to execute asynchronously, that is, the line where each next node is located is treated as a new sub-workflow and executed asynchronously until each line is executed, and then the results are summarized and returned; 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 can be modified to start running from the current node.
[0060] The present invention modifies the allowed pointing of a 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.
[0061] 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:
[0062] Step S2: Add a new event type to Dify for streaming output. 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. When the streaming message event is detected, the content to be output is streamed to the Dify front-end page. It should be noted that there is no specific execution order between step S2 and steps S11-S13 in the present invention; step S2 can be executed independently based on actual needs.
[0063] Furthermore, a new event type for implementing streaming output is added to 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. When the streaming message event is monitored, the content to be output is streamed to the front-end page of Dify. Specifically, the following steps are included:
[0064] Dify defines an event-driven system, which includes three roles: events, event publishers, and event listeners. The event publisher is a method that adds events to the event queue, and the event listener is used to obtain events from the event queue and monitor event types.
[0065] 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 delivered 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 needed and at which specific time point; wherein, the event type DirectStreamEvent includes each segmented content that needs to be streamed output. 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. It is a class in Python that stores information such as the text content and type to be streamed output;
[0066] When a node in the workflow requires 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. 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 message type.
[0067] 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.
[0068] The present invention transforms the existing Dify by adding a new event type DirectStreamEvent in Dify for realizing streaming output. When a node in a workflow requires streaming output, it is only necessary to encapsulate the content to be output according to the format 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-mentioned 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, which increases the usability of the workflow and significantly improves the user experience.
[0069] Based on the same inventive concept, this application also provides a device corresponding to the method in Example 1, see Example 2 for details. Example
[0070] 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.
[0071] The device includes a workflow encapsulation module, a method modification module and a workflow execution module;
[0072] The workflow encapsulation module is used to encapsulate the entire workflow process 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;
[0073] The method modification module is used to modify the backend method for obtaining the next runnable node to obtain all next nodes based on the current node from the DAG structure of the workflow, rather than returning only one node. Because each node in the existing Dify workflow has only one input line and one output line, the existing method for obtaining the next runnable node only returns one node at a time. In order to enable the workflow to support multiple parallel threads, the backend method for obtaining the next runnable node needs to be modified.
[0074] The workflow execution module is used to execute the workflow on the back end. 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 back end will start a thread pool and assign a thread to each next node as a new sub-workflow for asynchronous execution.
[0075] In an embodiment of the present invention, in the workflow encapsulation module, encapsulating the entire workflow process into a DAG structure on the front-end page of Dify specifically includes:
[0076] Modify the allowed pointing of a 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.
[0077] Because each node in the existing Dify workflow has only one input and one output, the existing workflow logic needs to be optimized to support multi-threaded parallel execution. To this end, the present invention first modifies the allowed node pointers, allowing a node to point to multiple nodes. Then, based on the modified allowed node pointers, a workflow is created and the entire workflow process is encapsulated into a DAG (Directed Acyclic Graph) structure. This allows subsequent workflow execution to achieve multi-threaded parallel processing based on the DAG structure.
[0078] DAG, short for Directed Acyclic Graph, is a data structure consisting of a set of nodes and directed edges with no cycles (i.e., a path from one node to another through a series of edges never returns to the same node). DAG is widely used in computer science and many other fields. In a DAG, each node represents a task or operation, and directed edges represent dependencies between tasks.
[0079] In an embodiment of the present invention, the workflow execution module is specifically used to:
[0080] 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 also means that there may 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.
[0081] 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 open a thread for each sub-workflow to execute asynchronously, that is, the line where each next node is located is treated as a new sub-workflow and executed asynchronously until each line is executed, and then the results are summarized and returned; 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 can be modified to start running from the current node.
[0082] The present invention modifies the allowed pointing of a 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.
[0083] 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;
[0084] The streaming output module is used to add a new event type in Dify for implementing streaming output. When a node in the workflow requires streaming output, it encapsulates the content to be output according to the event type to generate a streaming message event. When the streaming message event is detected, the content to be output is streamed to the Dify front-end page. It should be noted that in the present invention, there is no sequential execution order between the streaming output module and the workflow encapsulation module, method modification module, and workflow execution module. The streaming output module can be executed independently according to actual needs.
[0085] Furthermore, the streaming output module is specifically used to:
[0086] Dify defines an event-driven system, which includes three roles: events, event publishers, and event listeners. The event publisher is a method that adds events to the event queue, and the event listener is used to obtain events from the event queue and monitor event types.
[0087] 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 needed and at which specific time point; wherein, the event type includes each segmented content that needs to be streamed output. 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. It is a class in Python that stores information such as the text content and type to be streamed output;
[0088] When a node in the workflow requires 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. 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 message type.
[0089] 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.
[0090] The present invention transforms the existing Dify by adding a new event type DirectStreamEvent in Dify for realizing streaming output. When a node in a workflow requires streaming output, it is only necessary to encapsulate the content to be output according to the format 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-mentioned 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, which increases the usability of the workflow and significantly improves the user experience.
[0091] Based on the same inventive concept, this application provides an electronic device embodiment corresponding to the first embodiment, see the third embodiment for details. Example
[0092] 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.
[0093] Since the electronic device described in this embodiment is the device used to implement the method in Example 1 of this application, based on the method described in Example 1 of this application, those skilled in the art will be able to understand the specific implementation of the electronic device of this embodiment and its various variations. Therefore, how the electronic device implements the method in the embodiment of this application will not be described in detail here. As long as the device used by those skilled in the art to implement the method in the embodiment of this application falls within the scope of protection to be provided by this application.
[0094] Based on the same inventive concept, this application provides a storage medium corresponding to Example 1, see Example 4 for details. Example
[0095] This embodiment provides a computer-readable storage medium, such as Figure 4As 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.
[0096] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, apparatus, or computer program products. Thus, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, 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 magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0097] 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 flowcharts and / or block diagrams, as well as combinations of processes and / or blocks in the flowcharts and / or block diagrams, 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 produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowcharts and / or block diagrams. 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.
[0098] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work 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 The function specified in one or more boxes.
[0099] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device 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.
[0100] Although the specific embodiments of the present invention are described above, those skilled in the art should understand that the specific embodiments described are merely 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 within 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 Dify front-end page, encapsulate the entire workflow process into a DAG structure and submit the DAG structure to the back-end. Modify the backend method for obtaining the next runnable node to obtain all next nodes based on the current node from the workflow's DAG structure; 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 used as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution, specifically including: when the backend executes the workflow, all the next nodes of the current node are obtained, and at the same time, in the method of obtaining the next node, it is determined whether all the previous nodes of the current node have been executed. If not, it is blocked and waited until all the previous nodes are executed; if so, the logic of the current node is executed; it is determined whether the next node of the current node is empty or the end node. If so, the execution process of the workflow is terminated; if not, it continues to determine whether the number of next nodes is 1. If so, after executing the current node, the logic of the next node is directly executed; if not, after executing the current node, each next node is used as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution. The asynchronous execution method adopts a recursive method; A new event type for implementing streaming output is added to 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. When the streaming message event is listened to, the content to be output is streamed to the front-end page of Dify. Specifically, an event-driven system is defined in Dify. The event-driven system includes three roles: events, event publishers, and event listeners. A new event type DirectStreamEvent for implementing streaming output is added to the event-driven system, and the event publisher passes the event type for implementing streaming output to the nodes in the workflow that need to provide streaming. Output service node, where the event type includes the various segmented contents that need 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 cyclically takes events from the event queue, and when it is detected 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 cyclically takes out the content to be output from the generator and streams the content to be output to the front-end page of Dify through SSE.
2. The method for enhancing workflow according to claim 1, characterized in that: The above-mentioned 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 a 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.
3. A device for enhancing workflow, characterized by: 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 workflow process 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 backend method for obtaining the next runnable node to obtain all next nodes according to the current node from the DAG structure of the workflow; The workflow execution module is used for back-end execution of 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 for asynchronous execution, specifically including: when the back-end executes the workflow, all next nodes of the current node are obtained, and at the same time, in the method of obtaining the next node, it is determined whether all previous nodes of the current node have been executed. If not, blocking and waiting until all previous nodes are executed; if so, the logic of the current node is executed; it is determined whether the next node of the current node is empty or the end node, and if so, the execution process of the workflow is terminated; if not, it continues to determine whether the number of next nodes is 1, and if so, after executing the current node, the logic of the next node is directly executed; if not, after executing the current node, each next node is used as a sub-workflow, and a thread is opened for each sub-workflow for asynchronous execution, and the asynchronous execution method adopts a recursive method; The device also includes a streaming output module, which 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. Specifically, it includes: defining an event-driven system in Dify, the event-driven system includes three roles: event, event publisher and event listener; adding an event type DirectStreamEvent for realizing streaming output to the event-driven system, and passing the event type for realizing streaming output to the workflow through the event publisher; A node in the flow needs to provide streaming output services, where the event type includes the various segmented contents that need 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 cyclically takes events from the event queue, and when it is detected 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 cyclically takes out the content to be output from the generator and streams the content to be output to the front-end page of Dify through SSE.
4. The device for enhancing workflow according to claim 3, characterized in that: 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 a 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 modified allowed pointing of the node, and when saving the created workflow, encapsulating the entire workflow process into a DAG directed acyclic graph structure.
5. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the method according to any one of claims 1 to 2 is implemented.
6. 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 2 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