How to build a reusable chain that takes the output of another chain as a keyword argument for the template? #23847
Replies: 2 comments 1 reply
-
Hey there, @adrenaline681! I'm Dosu, a bot here to assist you with any bugs, questions, or contributions you have in mind. Let's tackle this challenge together while we wait for a human maintainer. To build a reusable chain that takes the output of another chain as a keyword argument for the template, you can use the from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnableParallel, RunnableLambda
from langchain_openai import OpenAI
# Initialize the language model
llm = OpenAI(model="text-davinci-003", temperature=0)
# Define the title generation prompt template
title_template = PromptTemplate.from_template('Generate a title for a blog post about the following topic: {topic}')
generate_title = title_template | llm | StrOutputParser()
# Define the translation prompt template
translate_template = PromptTemplate.from_template('Translate the following text to {language}: {text}')
# Create a custom function to handle the output
def translate_text(inputs):
text = inputs['text']
language = inputs['language']
return translate_template.invoke({'text': text, 'language': language})
# Wrap the custom function in a RunnableLambda
translate_text_runnable = RunnableLambda(translate_text)
# Create the composed chain
composed_chain = (
RunnableParallel({"text": generate_title})
.pipe(translate_text_runnable)
.pipe(llm)
.pipe(StrOutputParser())
)
# Invoke the composed chain
result = composed_chain.invoke({'topic': 'The benefits of exercise', 'language': 'Spanish'})
print(result) In this example:
This approach ensures that the output of one chain is correctly passed as a keyword argument to another template in a reusable manner [1][2][3]. |
Beta Was this translation helpful? Give feedback.
-
This solution doesnt seem to work when I run it. Isnt there an easy way to specify in the dictionary where you want to place the output of the previous step? Something like this:
|
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I'm trying to build reusable chains and I'm having issues trying to set it up so it takes the output of the previous chain as one of the template format arguments.
In this example, I have a chain that generates a title based on a topic and a chain that translates the title into a different language. The topic and the language I want to provide them when I invoke the final chain.
If my 2nd chain only has one single input for the template (because I hardcode the language), everything works perfectly:
But if I want to be able to provide the language when I invoke the final chain then, I dont know how to pass the output to the template dictionary. See the "HOW_TO_PASS_OUTPUT_OF_PREVIOUS_CHAIN" in the code
I also tried to use a lambda function but the itemgetter doesn't seem to work:
If anyone knows the answer I would be much appreciated.
System Info
langchain==0.2.6
langchain-community==0.2.6
langchain-core==0.2.10
langchain-openai==0.1.12
langchain-text-splitters==0.2.2
Windows
Python 3.12
Beta Was this translation helpful? Give feedback.
All reactions