langflow-ai / langflow

⛓️ Langflow is a visual framework for building multi-agent and RAG applications. It's open-source, Python-powered, fully customizable, LLM and vector store agnostic.
http://www.langflow.org
MIT License
18.48k stars 2.78k forks source link

SequentialChain does not feed output/input variables #1866

Open ronaldo-moco opened 3 weeks ago

ronaldo-moco commented 3 weeks ago

Hello Everyone,

I am trying to connect two LLM chains sequentially. The first one outputs chain1_result. The second one expects chain1_result as a input. I've included one SequentialChain component, linking the two chains. The flow builds succesfully, but when i try to run the chat, it show the message 'Error: Missing some input keys: {'chain1_result'}' Is there something that i'm missing?

dosubot[bot] commented 3 weeks ago

To address the issue of the SequentialChain not correctly passing the chain1_result from the first LLM chain to the second, ensure that the output of the first chain is explicitly mapped as the input to the second chain within the SequentialChain configuration. This involves specifying how the output from the first chain should be used as input for the second chain, which can be achieved through the set_input_mapping and set_output_mapping methods of the SequentialChain.

Here's a general approach to configure the mappings:

from langchain.sequential import SequentialChain

# Assuming chain1's output needs to be chain2's input
sequential_chain = SequentialChain(components=[chain1, chain2])
sequential_chain.set_input_mapping({
    "chain2_input_key": "chain1_output_key"  # Map chain1's output to chain2's input
})
sequential_chain.set_output_mapping({
    "final_output": "chain2_output_key"  # Map chain2's output to a final output key
})

# Execute the sequential chain with initial input
result = sequential_chain.invoke({"chain1_input_key": initial_input_value})

Replace "chain1_output_key", "chain2_input_key", and "chain2_output_key" with the actual keys used in your chains to ensure the output of the first chain is correctly identified and passed as input to the second chain.

This setup should resolve the error message indicating missing input keys by ensuring that the output from the first chain is correctly passed as input to the second chain. If you continue to face issues, it might be worth reviewing the handling of input and output keys within your chains to ensure they align with the expected configurations.

#### Details **Sources** - [src/backend/base/langflow/components/chains/LLMCheckerChain.py](https://github.com/logspace-ai/langflow/blob/dev/src/backend/base/langflow/components/chains/LLMCheckerChain.py) - [src/backend/base/langflow/components/experimental/RunnableExecutor.py](https://github.com/logspace-ai/langflow/blob/dev/src/backend/base/langflow/components/experimental/RunnableExecutor.py) - [src/backend/langflow/processing/base.py](https://github.com/logspace-ai/langflow/blob/dev/src/backend/langflow/processing/base.py) - [src/backend/base/langflow/processing/base.py](https://github.com/logspace-ai/langflow/blob/dev/src/backend/base/langflow/processing/base.py)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

ronaldo-moco commented 3 weeks ago

@dosu It doesnt work as intended, now i get this error message: Error building node SequencialChain: 'SequentialChain' object has no attribute 'set_input_mapping'

dosubot[bot] commented 3 weeks ago

It appears there was a misunderstanding regarding the functionality provided by the SequentialChain class in the LangChain library, as it does not offer set_input_mapping and set_output_mapping methods, leading to the error you encountered. To connect two chains sequentially where the output of the first chain is used as the input for the second, you'll need to manually manage the flow of data between the chains.

Here's a step-by-step guide to manually handle the output from the first chain and use it as input for the second:

  1. Execute the First Chain: Run the first chain independently and capture its output.
  2. Pass Output to Second Chain: Take the output from the first chain and use it as the input for the second chain.

This approach requires you to explicitly manage the output and input keys, ensuring that the output from the first chain is correctly formatted and passed as the expected input for the second chain.

Without specific details on how your chain1 and chain2 are implemented, this general guidance should help you set up a manual sequence between the two chains. If you encounter further issues or if this solution doesn't meet your needs, providing more details about the chain1 and chain2 implementations could help in offering more specific advice.

#### Details **About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

ronaldo-moco commented 3 weeks ago

@ogabrielluiz could you take a look at this? I'm not sure how to proceed since its just standard components...

YamonBot commented 2 days ago

The basic components of Langchain must be initialized within custom components. Elements initialized in other components are not serialized. Due to such issues, I resolved this by binding the values used in the sequential chain to the Document type and initializing them in the final component.

https://github.com/langflow-ai/langflow/issues/867