Bird
Raised Fist0
LangChainframework~10 mins

What is a chain in LangChain - Interactive Quiz & Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple chain that calls a language model.

LangChain
from langchain.chains import [1]
from langchain.llms import OpenAI

llm = OpenAI()
chain = [1](llm=llm)
result = chain.run("Hello!")
Drag options to blanks, or click blank then click option'
ALLMChain
BSimpleChain
CChainList
DLangChain
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class that does not exist in LangChain.
Confusing chain classes with other utilities.
2fill in blank
medium

Complete the code to import the base class for chains in LangChain.

LangChain
from langchain.chains import [1]
Drag options to blanks, or click blank then click option'
AChainBase
BChainable
CChain
DBaseChain
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'BaseChain' instead of 'Chain'.
Guessing class names without checking documentation.
3fill in blank
hard

Fix the error in the code to create a chain that uses a prompt template.

LangChain
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import [1]

llm = OpenAI()
prompt = [1](template="Say hello to {name}.")
chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run(name="Alice")
Drag options to blanks, or click blank then click option'
AChainPrompt
BPromptChain
CTemplatePrompt
DPromptTemplate
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names for prompt templates.
Confusing prompt classes with chain classes.
4fill in blank
hard

Fill both blanks to create a chain that uses a prompt template and runs with input.

LangChain
from langchain.chains import [1]
from langchain.prompts import [2]

chain = [1](llm=llm, prompt=[2](template="Hello, {user}!") )
result = chain.run(user="Bob")
Drag options to blanks, or click blank then click option'
ALLMChain
BPromptTemplate
CSimpleChain
DChainBase
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up chain and prompt class names.
Using classes that do not exist in LangChain.
5fill in blank
hard

Fill all three blanks to create a chain that uses a prompt template, runs with input, and prints the result.

LangChain
from langchain.chains import [1]
from langchain.prompts import [2]
from langchain.llms import OpenAI

llm = OpenAI()
prompt = [2](template="Good morning, {name}!")
chain = [1](llm=llm, prompt=prompt)
result = chain.run(name="Eve")
print(result)
Drag options to blanks, or click blank then click option'
ALLMChain
BPromptTemplate
CSimpleChain
DChainBase
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong class names for chain or prompt.
Not matching variable names correctly.

Practice

(1/5)
1. What is a chain in LangChain?
easy
A. A single prompt sent to a language model
B. A database used to store language model outputs
C. A sequence of steps linking language model calls to perform a task
D. A tool to visualize language model responses

Solution

  1. Step 1: Understand the purpose of a chain

    A chain connects multiple language model calls and prompts to complete a task step-by-step.
  2. Step 2: Compare options

    Only A sequence of steps linking language model calls to perform a task describes this linking of steps. The other options describe unrelated concepts.
  3. Final Answer:

    A sequence of steps linking language model calls to perform a task -> Option C
  4. Quick Check:

    Chain = linked steps for tasks [OK]
Hint: Chains link multiple steps to solve tasks [OK]
Common Mistakes:
  • Thinking a chain is just one prompt
  • Confusing chains with data storage
  • Assuming chains are visualization tools
2. Which of the following is the correct way to create a simple chain in LangChain?
easy
A. chain = LLMChain(llm=llm, prompt=prompt)
B. chain = Chain(llm, prompt)
C. chain = create_chain(llm, prompt)
D. chain = LLMChain(prompt)

Solution

  1. Step 1: Recall LangChain syntax for creating a simple chain

    The correct syntax uses named parameters like llm= and prompt= when creating an LLMChain.
  2. Step 2: Check each option

    chain = LLMChain(llm=llm, prompt=prompt) matches the correct syntax. The other options use incorrect function or class names or miss the llm parameter.
  3. Final Answer:

    chain = LLMChain(llm=llm, prompt=prompt) -> Option A
  4. Quick Check:

    Use named parameters for LLMChain [OK]
Hint: Use named parameters when creating chains [OK]
Common Mistakes:
  • Omitting required parameters
  • Using wrong class or function names
  • Passing parameters without names
3. Given this code snippet, what will be the output of result?
from langchain.chains import LLMChain
llm = SomeLLM()
prompt = "Translate English to French: {text}"
chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run({"text": "Hello"})
medium
A. "Bonjour"
B. "Hello"
C. An error because of missing input
D. "Translate English to French: Hello"

Solution

  1. Step 1: Understand what the chain does

    The chain uses the prompt to translate English text to French by calling the language model with the input text.
  2. Step 2: Analyze the input and expected output

    The input text is "Hello", so the chain should return the French translation "Bonjour".
  3. Final Answer:

    "Bonjour" -> Option A
  4. Quick Check:

    Chain translates input text correctly [OK]
Hint: Chain output matches prompt task with input [OK]
Common Mistakes:
  • Expecting the original text as output
  • Confusing prompt string with output
  • Assuming missing input causes error
4. Identify the error in this LangChain code snippet:
from langchain.chains import LLMChain
llm = SomeLLM()
prompt = "Summarize: {text}"
chain = LLMChain(llm=llm)
result = chain.run({"text": "This is a long article."})
medium
A. Calling run() without arguments
B. Incorrect input dictionary key
C. Using LLMChain instead of ComplexChain
D. Missing prompt parameter when creating the chain

Solution

  1. Step 1: Check chain creation parameters

    The chain is created without the required prompt parameter, which is necessary for the chain to work.
  2. Step 2: Verify input and method calls

    The input dictionary key matches the prompt placeholder, and run() is called with arguments, so no error there.
  3. Final Answer:

    Missing prompt parameter when creating the chain -> Option D
  4. Quick Check:

    Prompt is required when creating a chain [OK]
Hint: Always provide prompt when creating a chain [OK]
Common Mistakes:
  • Forgetting to pass prompt parameter
  • Assuming input keys can be arbitrary
  • Calling run() without inputs
5. You want to build a LangChain that first translates English text to French, then summarizes the French text. Which approach correctly uses chains to achieve this?
hard
A. Call the language model twice manually without chains
B. Create two chains: one for translation, one for summarization, then link them sequentially
C. Use a single chain with a prompt that asks for translation and summary at once
D. Create a chain that only summarizes English text directly

Solution

  1. Step 1: Understand chaining multiple tasks

    To perform two steps in order, create separate chains for each task and link them so output of first is input to second.
  2. Step 2: Evaluate options for chaining

    Create two chains: one for translation, one for summarization, then link them sequentially correctly describes linking two chains sequentially. Use a single chain with a prompt that asks for translation and summary at once tries to do both in one prompt, which is less modular. Call the language model twice manually without chains skips chains. Create a chain that only summarizes English text directly misses translation step.
  3. Final Answer:

    Create two chains: one for translation, one for summarization, then link them sequentially -> Option B
  4. Quick Check:

    Use multiple linked chains for multi-step tasks [OK]
Hint: Link chains sequentially for multi-step tasks [OK]
Common Mistakes:
  • Trying to do multiple tasks in one prompt
  • Not linking chain outputs properly
  • Skipping chain usage for multi-step flows