Bird
Raised Fist0
LangChainframework~20 mins

What is LangChain - Practice Questions & Exercises

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
Challenge - 5 Problems
🎖️
LangChain Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary purpose of LangChain?
LangChain is a framework designed to help developers build applications that use large language models (LLMs). What is its main goal?
ATo simplify connecting language models with other data sources and tools
BTo create graphical user interfaces for mobile apps
CTo manage databases and SQL queries efficiently
DTo optimize network protocols for faster internet
Attempts:
2 left
💡 Hint
Think about how LangChain helps combine language models with other parts of an app.
component_behavior
intermediate
2:00remaining
How does LangChain handle external data sources?
In LangChain, which component is responsible for connecting language models to external data like documents or APIs?
AAgents
BChains
CMemory
DCallbacks
Attempts:
2 left
💡 Hint
This component sequences calls and manages data flow.
📝 Syntax
advanced
3:00remaining
What is the correct way to create a simple LangChain chain with an LLM?
Given the following code snippets, which one correctly creates a chain that uses an OpenAI LLM to generate text?
LangChain
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

prompt = PromptTemplate(template="Say hello to {name}.", input_variables=["name"])
llm = OpenAI()
chain = ???
ALLMChain(llm=llm)
BLLMChain(prompt=prompt, llm=llm)
CLLMChain(llm=llm, prompt=prompt)
DLLMChain(prompt=prompt)
Attempts:
2 left
💡 Hint
Check the required parameters for LLMChain constructor.
state_output
advanced
3:00remaining
What output does this LangChain code produce?
Consider this LangChain code snippet that uses a prompt template and an OpenAI LLM to greet a user named 'Alice'. What is the expected output?
LangChain
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

prompt = PromptTemplate(template="Say hello to {name}.", input_variables=["name"])
llm = OpenAI(temperature=0)
chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run(name="Alice")
print(result)
A"Hello Alice."
B"Say hello to Alice."
C"Hi Alice!"
D"Hello, Alice!"
Attempts:
2 left
💡 Hint
The LLM generates a polite greeting based on the prompt.
🔧 Debug
expert
4:00remaining
Why does this LangChain agent code raise an error?
This code tries to create an agent with tools but raises a TypeError. What is the cause?
LangChain
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI

llm = OpenAI()
tools = [Tool(name="Search", func=lambda x: "Found: " + x, description="Search tool")]
agent = initialize_agent(tools, llm)
result = agent.run("Find cats")
Ainitialize_agent requires a third argument specifying the agent type
BThe lambda function is missing a return statement
CTool objects must be created with a class method, not directly
DOpenAI LLM must be initialized with an API key argument
Attempts:
2 left
💡 Hint
Check the parameters required by initialize_agent function.

Practice

(1/5)
1. What is the main purpose of LangChain?
easy
A. To create databases for storing large text files
B. To design user interfaces for mobile apps
C. To help build applications that use language models easily
D. To compile programming languages into machine code

Solution

  1. Step 1: Understand LangChain's role

    LangChain is designed to help developers build apps that use language models.
  2. Step 2: Compare options

    Only To help build applications that use language models easily matches this purpose; others describe unrelated tasks.
  3. Final Answer:

    To help build applications that use language models easily -> Option C
  4. Quick Check:

    LangChain purpose = build language model apps [OK]
Hint: Remember LangChain connects language models to apps [OK]
Common Mistakes:
  • Confusing LangChain with database tools
  • Thinking LangChain is for UI design
  • Assuming LangChain compiles code
2. Which of the following is the correct way to describe a 'chain' in LangChain?
easy
A. A database table storing user inputs
B. A single prompt sent directly to a language model
C. A programming language used to write LangChain
D. A sequence of steps connecting models, prompts, and tools

Solution

  1. Step 1: Define 'chain' in LangChain context

    A chain is a workflow linking models, prompts, and tools in order.
  2. Step 2: Eliminate incorrect options

    Options A, B, and D do not describe a chain correctly.
  3. Final Answer:

    A sequence of steps connecting models, prompts, and tools -> Option D
  4. Quick Check:

    Chain = workflow steps [OK]
Hint: Chains link multiple steps in LangChain workflows [OK]
Common Mistakes:
  • Thinking a chain is just one prompt
  • Confusing chains with databases
  • Believing chain is a programming language
3. Given this LangChain code snippet, what will be the output?
from langchain import PromptTemplate, LLMChain, OpenAI
prompt = PromptTemplate(template="Translate '{text}' to French.", input_variables=["text"])
llm = OpenAI(temperature=0)
chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run(text="Hello")
print(result)
medium
A. Hello
B. Error: Missing API key
C. Bonjour
D. Translate 'Hello' to French.

Solution

  1. Step 1: Analyze the code's function

    The code sets up a prompt to translate text to French using OpenAI model.
  2. Step 2: Consider runtime environment

    Without an API key set for OpenAI, the code will raise an error.
  3. Final Answer:

    Error: Missing API key -> Option B
  4. Quick Check:

    OpenAI needs API key to run [OK]
Hint: OpenAI calls require API keys or error occurs [OK]
Common Mistakes:
  • Assuming output is translated text without API setup
  • Thinking code prints original text
  • Ignoring API key requirement
4. Identify the error in this LangChain code snippet:
from langchain import PromptTemplate, LLMChain
prompt = PromptTemplate(template="Say hello to {name}.", input_variables=["name"])
chain = LLMChain(prompt=prompt)
result = chain.run(name="Alice")
print(result)
medium
A. LLMChain missing llm argument
B. No error, code runs fine
C. Incorrect method name 'run' instead of 'execute'
D. Missing input_variables list in PromptTemplate

Solution

  1. Step 1: Check PromptTemplate usage

    PromptTemplate requires input_variables list; it's missing here (but not fatal).
  2. Step 2: Check LLMChain initialization

    LLMChain requires an llm (language model) argument, which is missing.
  3. Final Answer:

    LLMChain missing llm argument -> Option A
  4. Quick Check:

    LLMChain needs llm parameter [OK]
Hint: LLMChain always needs an llm argument [OK]
Common Mistakes:
  • Ignoring missing llm argument
  • Confusing method names
  • Overlooking input_variables requirement
5. You want to build a chatbot using LangChain that answers questions and also fetches current weather data. Which approach best uses LangChain's features?
medium
A. Create a chain that connects a language model with a weather API tool
B. Use LangChain only for the weather API calls, ignoring language models
C. Write separate scripts for chatbot and weather, no chaining needed
D. Use LangChain to store weather data in a database

Solution

  1. Step 1: Understand LangChain's chaining ability

    LangChain can connect language models with external tools in a chain.
  2. Step 2: Match use case to chaining

    Combining chatbot (language model) with weather API in one chain fits LangChain's design.
  3. Final Answer:

    Create a chain that connects a language model with a weather API tool -> Option A
  4. Quick Check:

    LangChain chains link models and tools [OK]
Hint: Chains combine models and tools for smart apps [OK]
Common Mistakes:
  • Using LangChain only for API calls without models
  • Separating chatbot and weather logic unnecessarily
  • Misusing LangChain as a database