langflow-ai / langflow

Langflow is a low-code app builder for RAG and multi-agent AI applications. It’s Python-based and agnostic to any model, API, or database.
http://www.langflow.org
MIT License
31.46k stars 3.9k forks source link

Understanding how the Custom components code is getting executed? #4152

Open deven740 opened 2 hours ago

deven740 commented 2 hours ago

Hi all.

I just wanted to understand how the flow that we create gets executed.

For eg. If I create a custom component in which section the components are getting executed. I know there is a graph class which executes the code. But it would be great If you could point me in which function in the graph class the components are executed?

Thanks in advance.

dosubot[bot] commented 2 hours ago

Hey @deven740 👋, I'm here to assist you with any questions or issues you have. Let's dive into the custom component execution flow together!

Custom components in the graph class are executed in the function associated with the output method specified in the component. For example, in a custom component class, the execution occurs in the method defined in the outputs list. Here's how it works:

Similarly, in another custom component class, the execution might occur in a method like process_inputs:

Therefore, the function where custom components are executed is the one specified in the method attribute of the Output object in the outputs list of the component class [1].

To continue talking to Dosu, mention @dosu.

deven740 commented 2 hours ago

@dosu Where in the graph class the components are getting executed? Can you point me to the method that runs the code?

dosubot[bot] commented 2 hours ago

The components in the graph class are executed in the _run method. This method is responsible for running the graph with the given inputs and components. It processes the graph by updating the vertices with the session ID and handling the inputs and components to produce the outputs. Additionally, the run and arun methods are provided to execute the graph in both synchronous and asynchronous contexts, respectively [1][2].

To continue talking to Dosu, mention @dosu.

deven740 commented 2 hours ago

@dosu Can you help me understand the entire run process?

dosubot[bot] commented 2 hours ago

The run process in the graph class involves several key methods that handle the execution of the graph with the provided inputs and configurations. Here's an overview of the process:

  1. _run Method: This is the core method responsible for executing the graph asynchronously. It takes inputs, input components, output specifications, and other configurations like session ID and streaming options. It validates inputs, updates vertices with input values and session ID, processes the graph, and collects outputs from the graph vertices. It also handles exceptions during execution by logging them and raising a ValueError if needed [1][2].

  2. run Method: This method allows for synchronous execution of the graph. It creates a coroutine using the arun method and runs it in a synchronous context. If there's no running event loop, it uses asyncio.run to execute the coroutine; otherwise, it uses loop.run_until_complete to run it [1].

  3. arun Method: This method is used for asynchronous execution of the graph. While specific details about its implementation are not available, it is typically used to set up the coroutine for the run method to execute [1].

  4. Session IDs: These are used to update certain vertices within the graph to maintain or track the state during execution. The session ID is applied to vertices that require it, ensuring consistency across the execution process [1][3].

  5. Streaming: The stream parameter in the _run method determines whether results should be streamed. If set to False, the method ensures that any asynchronous generators are fully consumed before returning results [1].

This process allows the graph to be executed with flexibility in both synchronous and asynchronous contexts, handling inputs, outputs, and session management efficiently.

To continue talking to Dosu, mention @dosu.

deven740 commented 1 hour ago

@dosu Which method gets the output of components? Is it inside graph class or vertex class?

dosubot[bot] commented 1 hour ago

The method that retrieves the output of components is get_output, and it is located inside the ComponentVertex class, which is a subclass of the Vertex class. Here is the relevant code snippet:

def get_output(self, name: str) -> Output:
    if self._custom_component is None:
        raise NoComponentInstance(self.id)
    return self._custom_component.get_output(name)

[1].

To continue talking to Dosu, mention @dosu.