Skip to content

sushantdhumak/LangGraph-Basics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LangGraph-Basics

1. The Simplest Graph

Let's build a simple graph with 3 nodes and one conditional edge.

image


2. A Simple chain in Graph

Let's build up to a simple chain that combines 4 concepts:

  1. Using chat messages as our graph state
  2. Using chat models in graph nodes
  3. Binding tools to our chat model
  4. Executing tool calls in graph nodes

image


3. Route in Graph

Let's build a router, where the chat model routes between a direct response or a tool call based upon the user input. This is an simple example of an agent, where the LLM is directing the control flow either by calling a tool or just responding directly.

Screenshot 2025-02-03 230343


4. Agent in Graph

Let's build an agent using a general ReAct architecture

  1. Act - let the model call specific tools
  2. Observe - pass the tool output back to the model
  3. Reason - let the model reason about the tool output to decide what to do next (e.g., call another tool or just respond directly)

Screenshot 2025-02-06 223826


5. Agent in Graph with Memory

Let's build an agent using a general ReAct architecture with Memory

  1. Act - let the model call specific tools
  2. Observe - pass the tool output back to the model
  3. Reason - let the model reason about the tool output to decide what to do next (e.g., call another tool or just respond directly)

We will extend our agent by introducing memory.

image


6. State Schema in LangGraph

The state schema represents the structure and types of data that our graph will use. All nodes are expected to communicate with that schema.

We will use

  1. TypeDict class from python's typing module.
  2. DataClasses from python
  3. Pydantic - data validation and settings management library using Python type annotations.

7. State Reducers in LangGraph

The reducers, which specify how state updates are performed on specific keys / channels in the state schema.

We will use

  1. Annotated type with reducer function like operator.add.
  2. Annotated type with custom reducer function like reduce_list.
  3. MessagesState
  4. Re-writing and Removal of messages.

8. Multiple Schemas in LangGraph

The multiple schemas are needed when,

  1. Internal nodes may pass information that is not required in the graph's input / output.
  2. We may also want to use different input / output schemas for the graph.

9. Filtering and Trimming messages in LangGraph

Filtering and Trimming messages using,

  1. Remove Message with MessageState
  2. Filtering Messages
  3. Trimming Messages

10. Chatbot with Message Summarization & Checkpoint Memory

Let's create a simple Chatbot with conversation summary. We'll equip that Chatbot with memory, supporting long-running conversations.


11. Chatbot with Message Summarization & External DB Memory

Let's upgrade our Chatbot with conversation summary and external memory (SqliteSaver checkpointer), supporting long-running conversations and chat presistence.


12. Streaming the Output of the Graph (Graph State and Tokens)

  1. .stream and .astream are sync and async methods for streaming back results
  2. Streaming graph state using streaming modes - 'updates' and 'values'
  3. .astream events and each event is a dict with a few keys:
  • event: This is the type of event that is being emitted.
  • name: This is the name of event.
  • data: This is the data associated with the event.
  • metadata: Containslanggraph_node, the node emitting the event.

13. Human-in-the-loop | Breakpoints

For breakpoint, we need to simply compile the graph with interrupt_before=["tools"] where tools is our tools node. This means that the execution will be interrupted before the node tools, which executes the tool call.


14. Human-in-the-loop | Editing State

Using breakpoints to modify the graph state.


Releases

No releases published

Packages

No packages published

Languages