Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by MartianWayfarer333

How can I combine outputs from multiple Langchain prompts using SequentialChain?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm using Langchain with a SequentialChain composed of several LLMChains to prompt for various details about a person. My goal is to combine the outputs from each prompt into a single, coherent result.

For example, when I pass 'Dhoni' as input, I expect the output to be something like "Mahendra Singh Dhoni and the date of birth is July 7, 1981. He is from India". However, the current output only provides the date of birth, e.g., "The date of birth of Mahendra Singh Dhoni is July 7, 1981."

Below is my code:

PYTHON
import os from constent import openai_key from langchain_community.llms import OpenAI import streamlit as st from langchain import PromptTemplate from langchain.chains import LLMChain from langchain.chains import SequentialChain os.environ["OPENAI_API_KEY"] = openai_key st.title('Search about person') input_text = st.text_input("Search about the person") llm = OpenAI(temperature = 1.0) ## Prompt Engineering. prompt1 = PromptTemplate( input_variables = ["name"], ## place holder for storing the text template = "Give the full name of {name}" ) chain1 = LLMChain(llm = llm, prompt = prompt1, verbose = True, output_key = 'person') prompt2 = PromptTemplate( input_variables = ["person"], ## place holder for storing the text template = "Give the name of the country that {person} belongs to?" ) chain2 = LLMChain(llm = llm, prompt = prompt2, verbose = True, output_key = 'country') prompt3 = PromptTemplate( input_variables = ["person"], template = "What is the date of birth of {person}?" ) chain3 = LLMChain(llm = llm, prompt = prompt3, verbose = True, output_key = 'dob') combined_chain = SequentialChain(chains=[chain1,chain2, chain3],input_variables = ["name"], output_variables=["dob"], verbose = True) #chain2 = LLMChain(llm = llm, prompt = prompt1, verbose = True) if input: st.write(combined_chain.run({"name": input_text}))

How can I adjust my setup to combine outputs from each prompt into a single formatted response as expected?

0

Comments 0

Answers 2

Views 33

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Below is one way to modify your chain so that you collect the outputs from all three chains and then combine them into your desired result. The key idea is to update the SequentialChain's output_variables to include all intermediate outputs (i.e., person, country, and dob) and then format the final result.

For example, you can modify your code as follows:

PYTHON
import os from constent import openai_key from langchain_community.llms import OpenAI import streamlit as st from langchain import PromptTemplate from langchain.chains import LLMChain, SequentialChain os.environ["OPENAI_API_KEY"] = openai_key st.title('Search about person') input_text = st.text_input("Search about the person") llm = OpenAI(temperature=1.0) # Prompt for full name prompt1 = PromptTemplate( input_variables=["name"], # placeholder for storing the text template="Give the full name of {name}" ) chain1 = LLMChain(llm=llm, prompt=prompt1, verbose=True, output_key='person') # Prompt for country name prompt2 = PromptTemplate( input_variables=["person"], # placeholder for storing the text template="Give the name of the country that {person} belongs to?" ) chain2 = LLMChain(llm=llm, prompt=prompt2, verbose=True, output_key='country') # Prompt for date of birth prompt3 = PromptTemplate( input_variables=["person"], template="What is the date of birth of {person}?" ) chain3 = LLMChain(llm=llm, prompt=prompt3, verbose=True, output_key='dob') # Modify the SequentialChain to output all results combined_chain = SequentialChain( chains=[chain1, chain2, chain3], input_variables=["name"], output_variables=["person", "country", "dob"], verbose=True ) if input_text: results = combined_chain.run({"name": input_text}) # Format the final output as desired full_output = f"{results['person']} and the date of birth is {results['dob']}. He is from {results['country']}" st.write(full_output)

Explanation

  • Step 1: We defined three separate prompts and their corresponding chains:
    • chain1 extracts the full name.
    • chain2 determines the country for that person.
    • chain3 retrieves the date of birth.
  • Step 2: We set up the SequentialChain to return three output variables: "person", "country", and "dob".
  • Step 3: After running the chain, we manually format the results to combine all outputs into your final desired string.

With these modifications, if you pass "Dhoni" as input, the final result should closely match your expected output.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by NeptunianAdventurer597 1 month ago

0

We can use another chain to combine the outputs from the existing chains. For your given example, we can store person, country and date of birth from the existing chains. Then, we can add another chain that is instructed to combine all these information together.

PYTHON
from langchain_community.llms import OpenAI from langchain import PromptTemplate from langchain.chains import LLMChain from langchain.chains import SequentialChain input_text = "Mahendra Singh" llm = OpenAI(temperature = 1.0) ## Prompt Engineering. prompt1 = PromptTemplate( input_variables = ["name"], ## place holder for storing the text template = "Give the full name of {name}" ) chain1 = LLMChain(llm = llm, prompt = prompt1, verbose = True, output_key = 'person') prompt2 = PromptTemplate( input_variables = ["person"], ## place holder for storing the text template = "Give the name of the country that {person} belongs to?" ) chain2 = LLMChain(llm = llm, prompt = prompt2, verbose = True, output_key = 'country') prompt3 = PromptTemplate( input_variables = ["person"], template = "What is the date of birth of {person}?" ) chain3 = LLMChain(llm = llm, prompt = prompt3, verbose = True, output_key = 'dob') prompt4 = PromptTemplate( input_variables = ["person", "country", "dob"], template = "Merge these information {person}, {country}, {dob} to create a meaningful sentence." ) chain4 = LLMChain(llm = llm, prompt = prompt4, verbose = True, output_key = 'summary') combined_chain = SequentialChain(chains=[chain1,chain2, chain3, chain4],input_variables = ["name"], output_variables=["summary"], verbose = True) #chain2 = LLMChain(llm = llm, prompt = prompt1, verbose = True) if input_text: print(combined_chain.run({"name": input_text}))

Input Mahendra Singh

Output Mahendra Singh Dhoni, born on July 7, 1981, is from India.

No comments yet.

Discussion

No comments yet.