Chapter 01
basics
LangChain Academy
Welcome to LangChain Academy!
Context
At LangChain, we aim to make it easy to build LLM applications. One type of LLM application you can build is an agent. There’s a lot of excitement around building agents because they can automate a wide range of tasks that were previously impossible.
In practice though, it is incredibly difficult to build systems that reliably execute on these tasks. As we’ve worked with our users to put agents into production, we’ve learned that more control is often necessary. You might need an agent to always call a specific tool first or use different prompts based on its state.
To tackle this problem, we’ve built LangGraph — a framework for building agent and multi-agent applications. Separate from the LangChain package, LangGraph’s core design philosophy is to help developers add better precision and control into agent workflows, suitable for the complexity of real-world systems.
Course Structure
The course is structured as a set of modules, with each module focused on a particular theme related to LangGraph. You will see a folder for each module, which contains a series of notebooks. A video will accompany each notebook to help walk through the concepts, but the notebooks are also stand-alone, meaning that they contain explanations and can be viewed independently of the videos. Each module folder also contains a studio folder, which contains a set of graphs that can be loaded into LangSmith Studio, our IDE for building LangGraph applications.
Setup
Before you begin, please follow the instructions in the README to create an environment and install dependencies.
Chat models
In this course, we'll use Chat Models, which take a sequence of messages as input and return messages as output. LangChain supports many models via third-party integrations. By default, the course will use ChatOpenAI because it is both popular and performant. As noted, please ensure that you have an OPENAI_API_KEY.
Let's check that your OPENAI_API_KEY is set and, if not, you will be asked to enter it.
%%capture --no-stderr
%pip install --quiet -U langchain_openai langchain_core langchain_community langchain-tavilyimport os, getpass
def _set_env(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"{var}: ")
_set_env("OPENAI_API_KEY")Here is a useful how-to for all the things that you can do with chat models, but we'll show a few highlights below. If you've run pip install -r requirements.txt as noted in the README, then you've installed the langchain-openai package. With this, we can instantiate our ChatOpenAI model object. You can see pricing for various models here. The notebooks will default to gpt-4o because it offers a good balance of quality, price, and speed, but you can also opt for the lower-priced gpt-3.5 series or more recent models.
There are a few standard parameters that we can set with chat models. Two of the most common are:
model: the name of the modeltemperature: the sampling temperature
Temperature controls the randomness or creativity of the model's output where low temperature (close to 0) is more deterministic and focused outputs. This is good for tasks requiring accuracy or factual responses. High temperature (close to 1) is good for creative tasks or generating varied responses.
from langchain_openai import ChatOpenAI
gpt4o_chat = ChatOpenAI(model="gpt-4o", temperature=0)
gpt35_chat = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)Chat models in LangChain have a number of default methods. For the most part, we'll be using:
And, as mentioned, chat models take messages as input. Messages have a role (that describes who is saying the message) and a content property. We'll be talking a lot more about this later, but here let's just show the basics.
from langchain_core.messages import HumanMessage
# Create a message
msg = HumanMessage(content="Hello world", name="Lance")
# Message list
messages = [msg]
# Invoke the model with a list of messages
gpt4o_chat.invoke(messages)Output
AIMessage(content='Hello! How can I assist you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 9, 'prompt_tokens': 11, 'total_tokens': 20, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_provider': 'openai', 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_eb3c3cb84d', 'id': 'chatcmpl-CSWGCGbWLCLdHUAOh9pkNFWl1MWf4', 'service_tier': 'default', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--529fb9c7-f813-47a7-9662-dea715d4be28-0', usage_metadata={'input_tokens': 11, 'output_tokens': 9, 'total_tokens': 20, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})We get an AIMessage response. Also, note that we can just invoke a chat model with a string. When a string is passed in as input, it is converted to a HumanMessage and then passed to the underlying model.
gpt4o_chat.invoke("hello world")Output
AIMessage(content='Hello! How can I assist you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 9, 'prompt_tokens': 9, 'total_tokens': 18, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_provider': 'openai', 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_cbf1785567', 'id': 'chatcmpl-CSWGCXlVYTEWoHAFm3GXIIgUO1gCb', 'service_tier': 'default', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--43b070a6-7676-4aa1-984c-c0cd43d45c1e-0', usage_metadata={'input_tokens': 9, 'output_tokens': 9, 'total_tokens': 18, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})gpt35_chat.invoke("hello world")Output
AIMessage(content='Hello! How can I assist you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 9, 'prompt_tokens': 9, 'total_tokens': 18, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_provider': 'openai', 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'id': 'chatcmpl-CSWGDY5KePqihRcWDX3IGEZfSoyld', 'service_tier': 'default', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--f7963c03-f8b2-4eba-bc7f-4d58520fa661-0', usage_metadata={'input_tokens': 9, 'output_tokens': 9, 'total_tokens': 18, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})The interface is consistent across all chat models and models are typically initialized once at the start up each notebooks.
So, you can easily switch between models without changing the downstream code if you have strong preference for another provider.
Search Tools
You'll also see Tavily in the README, which is a search engine optimized for LLMs and RAG, aimed at efficient, quick, and persistent search results. As mentioned, it's easy to sign up and offers a generous free tier. Some lessons (in Module 4) will use Tavily by default but, of course, other search tools can be used if you want to modify the code for yourself.
_set_env("TAVILY_API_KEY")from langchain_tavily import TavilySearch # updated at 1.0
tavily_search = TavilySearch(max_results=3)
data = tavily_search.invoke({"query": "What is LangGraph?"})
search_docs = data.get("results", data)search_docsOutput
[{'url': 'https://www.datacamp.com/tutorial/langgraph-tutorial',
'title': 'LangGraph Tutorial: What Is LangGraph and How to Use It?',
'content': 'LangGraph is a library within the LangChain ecosystem that provides a framework for defining, coordinating, and executing multiple LLM agents (or chains) in a structured and efficient manner. By managing the flow of data and the sequence of operations, LangGraph allows developers to focus on the high-level logic of their applications rather than the intricacies of agent coordination. Whether you need a chatbot that can handle various types of user requests or a multi-agent system that performs complex tasks, LangGraph provides the tools to build exactly what you need. LangGraph significantly simplifies the development of complex LLM applications by providing a structured framework for managing state and coordinating agent interactions.',
'score': 0.9581988,
'raw_content': None},
{'url': 'https://www.geeksforgeeks.org/machine-learning/what-is-langgraph/',
'title': 'What is LangGraph? - GeeksforGeeks',
'content': 'LangGraph is an open-source framework built by LangChain that streamlines the creation and management of AI agent workflows. By treating workflows as interconnected nodes and edges, LangGraph offers a scalable, transparent and developer-friendly way to design advanced AI systems ranging from simple chatbots to multi-agent system. * ****Enhanced decision-making:**** Models relationships between nodes, enabling AI agents to learn from past actions and feedback. * ****langgraph:**** Framework for building graph-based AI workflows. * Build the workflow graph using LangGraph, adding nodes for classification and response, connecting them with edges and compiling the app. * Send each input through the workflow graph and returns the bot’s response, either a greeting or an AI-powered answer.',
'score': 0.95067275,
'raw_content': None},
{'url': 'https://langchain-ai.github.io/langgraph/concepts/why-langgraph/',
'title': 'Learn LangGraph basics - Overview',
'content': 'Trusted by companies shaping the future of agents— including Klarna, Replit, Elastic, and more— LangGraph is a low-level orchestration framework and runtime for building, managing, and deploying long-running, stateful agents. LangGraph is very low-level, and focused entirely on agent **orchestration**. Before using LangGraph, we recommend you familiarize yourself with some of the components used to build agents, starting with models and tools. LangGraph is focused on the underlying capabilities important for agent orchestration: durable execution, streaming, human-in-the-loop, and more. LangGraph provides low-level supporting infrastructure for *any* long-running, stateful workflow or agent. While LangGraph can be used standalone, it also integrates seamlessly with any LangChain product, giving developers a full suite of tools for building agents. Contains agent abstractions built on top of LangGraph.',
'score': 0.94911116,
'raw_content': None}]