LangChain helps you build smart apps that use language models easily. It organizes parts so you can connect and control them well.
LangChain architecture overview
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
LangChain
from langchain import LLMChain, PromptTemplate, OpenAI # Create a prompt template prompt = PromptTemplate(input_variables=["topic"], template="Write a short story about {topic}.") # Create a language model instance llm = OpenAI(temperature=0.7) # Combine prompt and model into a chain story_chain = LLMChain(llm=llm, prompt=prompt) # Run the chain result = story_chain.run({"topic": "a friendly robot"}) print(result)
LangChain uses chains to connect prompts and models.
Each part has a clear role: prompt templates create questions, LLMs answer, chains link them.
Examples
LangChain
from langchain import LLMChain, PromptTemplate, OpenAI prompt = PromptTemplate(input_variables=["name"], template="Say hello to {name}.") llm = OpenAI(temperature=0) chain = LLMChain(llm=llm, prompt=prompt) print(chain.run({"name": "Alice"}))
LangChain
from langchain import ConversationChain, OpenAI llm = OpenAI(temperature=0) conversation = ConversationChain(llm=llm) print(conversation.predict(input="Hi!"))
Sample Program
This program asks the language model to share a fun fact about an animal you choose. It shows how LangChain connects prompts and models simply.
LangChain
from langchain import LLMChain, PromptTemplate, OpenAI # Define a prompt template with a variable prompt = PromptTemplate(input_variables=["animal"], template="Describe a fun fact about a {animal}.") # Create an OpenAI language model instance llm = OpenAI(temperature=0) # Create a chain that links the prompt and the model fact_chain = LLMChain(llm=llm, prompt=prompt) # Run the chain with input output = fact_chain.run({"animal": "dolphin"}) print(output)
Important Notes
LangChain breaks down language tasks into parts you can connect and reuse.
Chains can be simple or complex, depending on your app needs.
Using prompt templates helps keep your questions clear and easy to change.
Summary
LangChain organizes language model apps into parts called chains.
Prompt templates create questions, LLMs answer, and chains link them.
This makes building smart language apps easier and clearer.
Practice
1. What is the main purpose of a chain in LangChain architecture?
easy
Solution
Step 1: Understand the role of chains
Chains connect parts like prompt templates and language models to form a workflow.Step 2: Identify the main purpose
Chains help organize and link these steps to build smart language apps easily.Final Answer:
To link different steps like prompts and LLMs to build language apps -> Option AQuick Check:
Chains link steps = D [OK]
Hint: Chains connect prompts and models to build apps [OK]
Common Mistakes:
- Thinking chains store data permanently
- Confusing chains with UI components
- Assuming chains train models
2. Which of the following is the correct way to create a prompt template in LangChain?
easy
Solution
Step 1: Recall PromptTemplate syntax
PromptTemplate requires a named argument 'template' with the string pattern.Step 2: Match syntax to options
Only PromptTemplate(template="Hello, {name}!") uses PromptTemplate(template="...") correctly.Final Answer:
PromptTemplate(template="Hello, {name}!") -> Option CQuick Check:
Named 'template' argument = A [OK]
Hint: Use named 'template' argument to create PromptTemplate [OK]
Common Mistakes:
- Passing template string without argument name
- Using non-existent create() or new() methods
- Confusing positional and keyword arguments
3. Given this code snippet, what will be the output?
from langchain import PromptTemplate, LLMChain
prompt = PromptTemplate(template="What is the capital of {country}?")
chain = LLMChain(prompt=prompt)
result = chain.run(country="France")
print(result)medium
Solution
Step 1: Analyze the code components
The chain is created with a prompt but no LLM (language model) is provided.Step 2: Understand LangChain requirements
LLMChain needs an LLM to generate answers; missing it causes an error.Final Answer:
An error because LLM is missing -> Option BQuick Check:
LLM missing causes error = B [OK]
Hint: LLMChain needs an LLM instance to run [OK]
Common Mistakes:
- Assuming chain.run returns prompt text
- Expecting output without LLM
- Confusing prompt template with output
4. Identify the error in this LangChain code snippet:
from langchain import PromptTemplate, LLMChain
prompt = PromptTemplate(template="Say hello to {name}")
chain = LLMChain(prompt=prompt, llm=None)
output = chain.run(name="Alice")
print(output)medium
Solution
Step 1: Check PromptTemplate usage
PromptTemplate is correctly created with a template string.Step 2: Check LLMChain initialization
LLMChain requires a valid LLM object; passing None causes failure.Final Answer:
LLMChain requires a valid LLM, not None -> Option AQuick Check:
LLM must be valid, not None = A [OK]
Hint: LLMChain needs a real LLM instance, not None [OK]
Common Mistakes:
- Thinking run() can't take arguments
- Assuming PromptTemplate syntax is wrong
- Missing imports but not causing this error
5. You want to build a LangChain app that asks a user's name, then uses an LLM to greet them. Which architecture correctly links these parts?
hard
Solution
Step 1: Understand LangChain app structure
PromptTemplate creates the question, LLMChain links prompt and LLM to generate answers.Step 2: Identify correct linking
Create a PromptTemplate for the question, then an LLMChain with that prompt and an LLM, then run the chain with user input correctly creates prompt, then LLMChain with prompt and LLM, then runs with input.Final Answer:
Create a PromptTemplate for the question, then an LLMChain with that prompt and an LLM, then run the chain with user input -> Option DQuick Check:
Prompt + LLM in chain = C [OK]
Hint: Chain = prompt + LLM + run with input [OK]
Common Mistakes:
- Trying to run prompt alone without chain
- Using LLM without prompt or chain
- Skipping linking steps in LangChain
