Brief Summary
This crash course covers building agentic AI applications using LangGraph. It is divided into three parts, focusing on fundamental techniques, advanced concepts, and end-to-end projects. The course covers building chatbots, integrating tools, adding memory, incorporating human feedback, using streaming techniques, and building MCP from scratch.
- Fundamental techniques for building agentic AI applications
- Advanced LangGraph concepts like multi-agent communication and debugging
- End-to-end projects focusing on LMOS pipelines and deployment techniques
Introduction And Agenda
The crash course on building agentic AI applications with LangGraph is divided into three parts, each approximately two to three hours long. Part one covers fundamental techniques such as building chatbots, integrating single and multiple tools, adding memory, incorporating human feedback, using streaming, and building MCP from scratch. It also discusses states, graphs, nodes, and edges, using the Graph API. Part two focuses on advanced LangGraph concepts like multi-agent workflows, multistate management, functional APIs, and debugging in LangGraph Studio. Part three involves building end-to-end projects, focusing on LMOS pipelines, deployment techniques, evaluation metrics, and using open-source tools like MLflow and Grafana.
Langgraph Projects Structure
The presenter starts with an empty folder as the project workspace and opens a command prompt to access the working directory. VS Code is used for coding. Creating an environment is essential for any project. The UV package manager, written in Rust, is used for its speed compared to Conda. UV is 10 to 100 times faster than pip and replaces tools like pip, pip tools, poetry, and virtualenv. Installation commands for Mac OS, Linux, and Windows are provided, as well as a pip install option. The project repository is initialized using uv init
, creating files like .gitignore
, python-version
, main.py
, and pyproject.toml
. The pyproject.toml
file contains project information and Python version requirements.
Building Blocks Of LAnggraph
LangGraph has three main components: edges, nodes, and state. Edges define the flow of information between nodes. Nodes perform specific operations or tasks. State maintains the context and variables accessible to all nodes in the graph. An example workflow is converting YouTube videos to blog posts, involving transcript extraction, title generation, and content creation. LLMs are used for content generation. LangGraph offers Graph API and Functional API, with Graph API being easier. A graph structure is created with nodes for start, transcript generation, title generation, and content generation, connected by edges. The start node takes a YouTube URL as input, and the transcript node extracts the transcript using a library like YT loader. The title generator node uses an LLM and prompt to generate a title from the transcript. The content generator node uses the title and transcript to generate blog content. State is used to store variables like transcript and title, accessible by any node in the graph, making it a state graph.
Building a Basic Chatbot
To build a basic chatbot using LangGraph, import necessary libraries such as annotated
from typing
for adding context-specific metadata to types, and TypeDict
from typing_extensions
. Import StateGraph
, start
, and end
from langgraph.graph
, and add_messages
from langgraph.graph.message
. The add_messages
function is a reducer used to append messages to a list in the state, maintaining conversation history. A State
class is created using TypeDict
to define the structure of the state, including a messages
variable annotated to use add_messages
for appending messages to a list. A GraphBuilder
is initialized using StateGraph
and the State
class. The code imports OS, loads environment variables using python-dotenv
, and initializes load_dotenv
. The Groq API key is retrieved from the environment variables. The code initializes a chat model using ChatGroq
from langchain_groq
with a specified model name. A node definition for the chatbot is created, which takes the state as input and returns messages by invoking the LLM with the state's messages. The graph is built by adding a node for the chatbot, edges from start to the chatbot and from the chatbot to end, and then compiling the graph. The graph is visualized using IPython.display
.
Building Chatbot With Tools
The goal is to integrate external tools into a chatbot using LangGraph. The chatbot, equipped with an LLM and prompt, cannot provide live information such as recent AI news. To address this, external tools like the Tavily API, a web search API, are integrated. The chatbot is designed to recognize when it needs to make a tool call. A tool node is introduced into the graph, allowing the chatbot to access multiple tools, including Tavily and custom tools like add and subtract functions. The LLM within the chatbot is bound to these tools, using docstrings to understand the inputs and arguments required for each tool. The process involves creating custom functions, a tool node, and a tool condition to determine when to make a tool call.
ReACT Agent Architecture
The discussion shifts to the ReAct agent architecture, which enhances the interaction between the LLM and tools. In ReAct, the LLM breaks down complex inputs into multiple steps, using tools iteratively and summarizing the output at the end. The key terms in ReAct are Act, Observe, and Reason. Act involves the LLM making a tool call. Observe is when the LLM processes the output of the tool. Reason is the LLM's decision-making process on what to do next. To implement ReAct, the graph is modified so that the tool node sends its output back to the tool calling LLM instead of directly to the end node. This allows the LLM to decide whether to make another tool call or to finalize the response.
Adding Memory In Langgraph
LangGraph has a feature to add memory, solving the problem of persistent checkpointing. Memory is used to recall previous interactions. To add memory, import MemorySaver
from langgraph.checkpoint.memory
and initialize it. The memory object is added during graph compilation using the checkpoint parameter. A thread ID is created to relate to a specific session. A configuration is created with a configurable key and a unique thread ID. The graph's invoke method is used with the messages and configuration. The memory saver stores the information, allowing the chatbot to recall previous interactions based on the thread ID.
Streaming In Langgraph
The video discusses streaming techniques in LangGraph, focusing on the stream
and astream
methods, which have values
and updates
parameters. The key difference between update
and value
modes in streaming is how messages are displayed. In update
mode, only the most recent message is displayed, whereas in value
mode, all messages are appended and displayed. The astream_events
method provides more detailed information for debugging, showing different events during the streaming process.
Human Feedback In the loop
The video explains how to incorporate human feedback into a LangGraph workflow. This involves creating a custom tool for human assistance, which interrupts the workflow to request input. The code uses libraries like command
and interrupt
to facilitate this. A function is defined to request assistance from a human, including a docstring for the LLM to understand the tool. The workflow is interrupted, and the human provides feedback, which is then fed back into the graph to resume execution.
MCP Server Scratch Implementation
The video explains how to build an MCP server from scratch and integrate it with an application using LangChain and LangGraph. The architecture involves MCP servers, MCP clients, and an app. The MCP server provides tools, context, and prompts to the client. The MCP client maintains a one-to-one connection with the server inside the host app. The process involves creating an MCP server with tools like addition, multiplication, and a weather call API. The communication happens through a client that uses a transport protocol, such as HTDIO or HTTP. The video demonstrates how to create MCP servers using fastMCP and how to implement different transport mechanisms.