0
0
LangChainframework~20 mins

Pipe operator for chain composition in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LangChain Pipe Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this LangChain pipe composition?

Consider two simple chains composed using the pipe operator. What will be the final output after running the composed chain?

LangChain
from langchain.chains import LLMChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
chain1 = LLMChain(llm=llm, prompt="Tell me a joke about {topic}.")
chain2 = LLMChain(llm=llm, prompt="Explain why this joke is funny: {text}")
composed_chain = chain1 | chain2

result = composed_chain.run({"topic": "cats"})
AAn error because the pipe operator is not supported for LLMChain objects.
BA string explaining why the joke about cats is funny, generated by chain2.
CA dictionary with keys 'joke' and 'explanation' containing outputs from both chains.
DA string containing the joke about cats, generated by chain1.
Attempts:
2 left
💡 Hint

Remember that the pipe operator sends the output of the first chain as input to the second chain.

📝 Syntax
intermediate
1:30remaining
Which option correctly composes two chains using the pipe operator?

Given two LangChain chains, which code snippet correctly composes them using the pipe operator?

LangChain
chain1 = LLMChain(...)
chain2 = LLMChain(...)
Acomposed = chain1 & chain2
Bcomposed = chain1 >> chain2
Ccomposed = chain1 + chain2
Dcomposed = chain1 | chain2
Attempts:
2 left
💡 Hint

The pipe operator is represented by a vertical bar.

🔧 Debug
advanced
2:00remaining
Why does this pipe composition raise an error?

Given the following code, why does the pipe composition raise a TypeError?

LangChain
from langchain.chains import LLMChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
chain1 = LLMChain(llm=llm, prompt="Say hello to {name}.")
chain2 = "Not a chain object"
composed = chain1 | chain2
ABecause chain2 is not a chain object and cannot be composed with chain1 using the pipe operator.
BBecause the pipe operator requires both chains to have the same prompt template.
CBecause the OpenAI LLM instance is not initialized properly.
DBecause the pipe operator is deprecated in LangChain.
Attempts:
2 left
💡 Hint

Check the type of objects used with the pipe operator.

state_output
advanced
2:00remaining
What is the value of the final output after chaining with pipe operator?

Given these two chains, what will be the final output after running the composed chain with input {"topic": "space"}?

LangChain
from langchain.chains import LLMChain
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
chain1 = LLMChain(llm=llm, prompt="Give a fun fact about {topic}.")
chain2 = LLMChain(llm=llm, prompt="Summarize this fact: {text}")
composed = chain1 | chain2

output = composed.run({"topic": "space"})
AA dictionary containing both the fun fact and its summary.
BThe original fun fact string about space generated by chain1.
CA summary string of the fun fact about space generated by chain2.
DAn error because the input keys do not match the expected keys in chain2.
Attempts:
2 left
💡 Hint

Remember how the output of chain1 is passed as input to chain2.

🧠 Conceptual
expert
2:30remaining
Which statement best describes the behavior of the pipe operator in LangChain chain composition?

Choose the most accurate description of how the pipe operator works when composing two chains in LangChain.

AIt connects two chains so the output of the first chain becomes the input of the second chain, enabling sequential processing.
BIt merges the prompts of both chains into a single prompt executed by one chain.
CIt runs both chains in parallel and combines their outputs into a list.
DIt swaps the input and output keys of the chains before running them.
Attempts:
2 left
💡 Hint

Think about how data flows between chains when using the pipe operator.