Test yourself under real exam conditions: 50 timed questions, 60 on the clock, pass mark 70%%. Instant score with a full review of everything you got wrong. Free — no account needed.
Exam details
LangChain is an open-source Python framework for building applications powered by large language models (LLMs). It provides modular components for prompts, chains, agents, memory, and retrieval to compose complex LLM workflows.
pip install langchain langchain-openai
LCEL is a declarative syntax for composing LangChain components using the pipe operator (|). It creates RunnableSequence chains that support streaming, batching, and async out of the box.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
chain = (
ChatPromptTemplate.from_template("Explain {topic} simply.")
| ChatOpenAI(model="gpt-4o")
| StrOutputParser()
)
result = chain.invoke({"topic": "recursion"})
Use ChatPromptTemplate.from_messages() with a list of (role, template) tuples. Variables are wrapped in curly braces.
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful {role}."),
("human", "{question}"),
])
messages = prompt.invoke({
"role": "tutor",
"question": "What is a linked list?"
})
Instantiate the model class and call .invoke() with a list of messages or a prompt value. The model returns an AIMessage.
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
llm = ChatOpenAI(model="gpt-4o", temperature=0)
response = llm.invoke([
HumanMessage(content="What is LangChain?")
])
print(response.content)
Call .stream() instead of .invoke() on any Runnable. It returns an iterator of chunks you can process incrementally.
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
for chunk in llm.stream("Tell me a joke"):
print(chunk.content, end="", flush=True)