LangGraph - Controllability with Map Reduce

LangChain · Intermediate ·🤖 AI Agents & Automation ·1y ago

Key Takeaways

LangGraph utilizes Map Reduce for parallelization and aggregation of results, enabling efficient task decomposition and parallel processing, with the send API allowing for arbitrary routing of tasks to multiple nodes. This is demonstrated through examples such as generating jokes and processing large logs.

Full Transcript

hey this is Lance Lang chain we've been talking through some more advanced concepts in Lang graph and I want to talk today about map ruce so the intuition behind map ruce is that you can take a task break it up into smaller units do them all concurrently and then aggregate the results so like what's a practical example of this let's say a graph that I want to process on some large number of logs you know could be us interactions or traces or something like that I can take those logs and group them into batches and each batch in parallel and then somehow aggregate the result into like maybe a final summary so that's like kind of one General example of this type of pattern now let's give like a toy example to just motivate this and show this working let's say I want to generate jokes I get a topic from a user I generate a list of related joke subjects from that topic because I want to generate like a bunch of jokes to produce some diversity the quote unquote map step here is I generate a joke for each subject and I can do that all in parallel so have some list of subject let's say 10 subjects produce a joke for each of them I paralyze that operation that's like my map step then the reduced step is from that resulting list of jokes could lapse that into a single best joke so that's like kind of my reduced example and here's like what that could look like as a kind of control flow here so you know here's like an example graph I have a topic I have some node called generate topics uh that will kind of fan that out to into a set of subjects okay so this is like looking at my overall State here so it has some list of subjects take that topic from the user break that out into some list of subjects that I want to generate jokes for now here's where a very nice trick comes into play Within Lang graph I can use the send API to basically take each of those subjects and send them to an independent node to generate a joke this is like my map step now this is a subtle Point that's actually very useful and worth reflecting on a little bit because if I tried to lay this out as a graph ahead of time what if I want to change the number of topics that I or subjects that I generate like let's say this is 10 or five or three I don't want to have to manually create all these edges in My Graph so the send is really nice because it will automatically do this for you and we're going to show that in code very shortly but that's the main intuition send allows you to arbitrarily route a list in this case of subject X to some number of generate joke in this case generate joke nodes um each one of those nodes will generate a joke each one of those notes has its own little state so basically the you know the subject that I send over via the send API gets written to the state of each of this generate joke note of each one it generates a joke now here's the other nice thing those jokes just get appended to My overall State jokes list so there we go and then I finalize and do my reduced step to get the best joke so that's overall control flow now this Falls within the overall kind of like line graph controlability features we talked about parallelization previously uh this is you know map ruce which is another kind of mode of advanced controlability again we just talked about this we're going to use a send API which is very convenient we're going to show that right now so let's copy over some code so here is just like a really simple kind I'm setting up my prompts so this is going to generate my subjects in this case I'll be I'll say generate between two and five you know subjects related to the overall topic here's a joke prompt generated joke for each subject and then here's the reduce prompt you know get the best one okay so this is just some boiler plate I'll use anthropic for this now here's where I'm going to set my overall State we talked about this before so I have a topic from the user I have a list of subject that I generate I have the jokes that I'm going to be appending to from my map step and then I have my final selected joke as my reduced step so I'll go ahead and kick that off so kind of done part of this graph Okay cool so let's kind of go down a little bit here and this is where I'm going to kind of build out some of the other pieces so we talked about let's actually go up to the diagram so you can see it we kind of talked about here um I'm going to send from that generate topics which is right here this list of topics I'm going to send each of those to generate joke now generate joke is going to have its own little State this joke stay I can Define it independently this is the subject that I'm going to get from that send operation okay so this is basically what I'm going to map to this generate joke node with its own little state it receives the subject from the send API and it generates a joke now it returns that the joke basically I return a list and I write that to the overall State jokes now if I go and look at my overall State what I can see is what's really nice here this jokes is a list with a reducer add so this reducer means that I will basically append to that list as all of my mapped generate joke nodes right to it so that's really nice so basically I have this overall State Jokes which is a list that I can basically append to from all my independent uh generate joke nodes in my map step that's all it's happening here this is where send uh API is called uh so basically you can see I'm going to send to generate joke uh I'm going to send each subject from my subjects and state which came from the generate topics right so generate topics wrote these subjects to State continue to jokes basically reads from those subjects sends each of those to generate joke where the joke is generated and it is written to jokes each of those jokes is just appended as a list of jokes and then I pick the best joke that's it draw My Graph out cool so you can see this conditional Edge basically represents that send operation this is actually very nice I didn't have to enumerate oh I have to create n specific edges to n generate joke nodes this handles that that kind of map step for me so this is extremely convenient uh because obviously it'd be very painful to have to like create in this case maybe five or three different generate joke nodes with each of the edges defined explicitly by me the user right I don't want to have to do all that work so this handles it all for you now let's just kick this off you can run let's generate chokes about animals so there's my topics cool it runs four times you to joke about each one it picks the best joke and we're done that's it so again map produce is a really General convenient way to parallelize operations they can be used for things like parallel log processing uh really anything that you can break up into a bunch of subtasks and just do them all concurrently it's a really nice way to speed up operation and what's really convenient about this is I don't have to kind of enumerate all those uh kind of Connections in my Graphics explicitly like let's say I had you know a large number of logs I wanted to break those up into 20 different steps I definitely don't want to have to create a graph that has 20 edges to you know 20 different uh you know perform analysis nodes so what's super convenient is this send API let automates all that for me so this is really useful I encourage you to play with it so just just want to interject briefly to show that lsmith interacts really nicely with Lang graph so again we have our graph um we have generated topics we have gone ahead and generated a joke for each topic and we've run over a dued step to pick the best one now we can go over to Lang Smith and look at this so here's basically the graph laid out with each step here so generate topics we can go ahead and look at the LM call in here so generate com separate list of to topics again related to animals here's our topic list then we can actually go into each joke step and investigate the generation itself so again we can kind of look at anthropic called generate joke about penguins and then we go ahead and generate the joke so you know we can look at each one of these these uh particular steps and again this is kind of the map step where we generate the topic list we map that out generate a joke for each topic and these are all those individual joke steps right here and then again we pick the best joke so we can look at the LM call here so this is pretty cool below we here's a bunch of jokes about animals select the best one return the idea of the best here's the four jokes here's the best one and then we take that ID and you know use it to fish out the final joke so again it's actually really nice to go into Langs withth and investigate what's happening under the hood with Lang graph uh and this is just a good example of it showing the topic generation step the map step where we fan out produce four jokes one for each topic and then the reduce step where we actually boil those down into a selected best joke and it's actually really nice to be able to look at each step in detail

Original Description

Map-reduce operations are essential for efficient task decomposition and parallel processing. This approach involves breaking a task into smaller sub-tasks, processing each sub-task in parallel, and aggregating the results across all of the completed sub-tasks. This video covers LangGraph's native support for Map-reduce operations. Documentation: https://langchain-ai.github.io/langgraph/how-tos/map-reduce/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LangChain · LangChain · 0 of 60

← Previous Next →
1 Chat With Your Documents Using LangChain + JavaScript
Chat With Your Documents Using LangChain + JavaScript
LangChain
2 LangChain SQL Webinar
LangChain SQL Webinar
LangChain
3 LangChain "OpenAI functions" Webinar
LangChain "OpenAI functions" Webinar
LangChain
4 LangSmith Launch
LangSmith Launch
LangChain
5 LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain
6 LangChain Expression Language
LangChain Expression Language
LangChain
7 Building LLM applications with LangChain with Lance
Building LLM applications with LangChain with Lance
LangChain
8 Benchmarking Question/Answering Over CSV Data
Benchmarking Question/Answering Over CSV Data
LangChain
9 LangChain "RAG Evaluation" Webinar
LangChain "RAG Evaluation" Webinar
LangChain
10 Fine-tuning in Your Voice Webinar
Fine-tuning in Your Voice Webinar
LangChain
11 Tabular Data Retrieval
Tabular Data Retrieval
LangChain
12 Building an LLM Application with Audio by AssemblyAI
Building an LLM Application with Audio by AssemblyAI
LangChain
13 Superagent Deepdive Webinar
Superagent Deepdive Webinar
LangChain
14 Lessons from Deploying LLMs with LangSmith
Lessons from Deploying LLMs with LangSmith
LangChain
15 Shortwave Assistant Deepdive Webinar
Shortwave Assistant Deepdive Webinar
LangChain
16 Cognitive Architectures for Language Agents
Cognitive Architectures for Language Agents
LangChain
17 Effectively Building with LLMs in the Browser with Jacob
Effectively Building with LLMs in the Browser with Jacob
LangChain
18 Data Privacy for LLMs
Data Privacy for LLMs
LangChain
19 "Theory of Mind" Webinar with Plastic Labs
"Theory of Mind" Webinar with Plastic Labs
LangChain
20 LangChain Templates
LangChain Templates
LangChain
21 Using Natural Language to Query Postgres with Jacob
Using Natural Language to Query Postgres with Jacob
LangChain
22 Building a Research Assistant from Scratch
Building a Research Assistant from Scratch
LangChain
23 Benchmarking RAG over LangChain Docs
Benchmarking RAG over LangChain Docs
LangChain
24 Skeleton-of-Thought: Building a New Template from Scratch
Skeleton-of-Thought: Building a New Template from Scratch
LangChain
25 Benchmarking Methods for Semi-Structured RAG
Benchmarking Methods for Semi-Structured RAG
LangChain
26 LangSmith Highlights: Getting Started
LangSmith Highlights: Getting Started
LangChain
27 LangSmith Highlights: Debugging
LangSmith Highlights: Debugging
LangChain
28 LangSmith Highlights: Datasets
LangSmith Highlights: Datasets
LangChain
29 LangSmith Highlights: Evaluation
LangSmith Highlights: Evaluation
LangChain
30 LangSmith Highlights: Human Annotation
LangSmith Highlights: Human Annotation
LangChain
31 LangSmith Highlights: Monitoring
LangSmith Highlights: Monitoring
LangChain
32 LangSmith Highlights: Hub
LangSmith Highlights: Hub
LangChain
33 SQL Research Assistant
SQL Research Assistant
LangChain
34 Getting Started with Multi-Modal LLMs
Getting Started with Multi-Modal LLMs
LangChain
35 Build a Full Stack RAG App With TypeScript
Build a Full Stack RAG App With TypeScript
LangChain
36 Auto-Prompt Builder (with Hosted LangServe)
Auto-Prompt Builder (with Hosted LangServe)
LangChain
37 LangChain v0.1.0 Launch: Introduction
LangChain v0.1.0 Launch: Introduction
LangChain
38 LangChain v0.1.0 Launch: Observability
LangChain v0.1.0 Launch: Observability
LangChain
39 LangChain v0.1.0 Launch: Integrations
LangChain v0.1.0 Launch: Integrations
LangChain
40 LangChain v0.1.0 Launch: Composability
LangChain v0.1.0 Launch: Composability
LangChain
41 LangChain v0.1.0 Launch: Streaming
LangChain v0.1.0 Launch: Streaming
LangChain
42 LangChain v0.1.0 Launch: Output Parsing
LangChain v0.1.0 Launch: Output Parsing
LangChain
43 LangChain v0.1.0 Launch: Retrieval
LangChain v0.1.0 Launch: Retrieval
LangChain
44 LangChain v0.1.0 Launch: Agents
LangChain v0.1.0 Launch: Agents
LangChain
45 Build and Deploy a RAG app with Pinecone Serverless
Build and Deploy a RAG app with Pinecone Serverless
LangChain
46 Hosted LangServe + LangChain Templates
Hosted LangServe + LangChain Templates
LangChain
47 LangGraph: Intro
LangGraph: Intro
LangChain
48 LangGraph: Agent Executor
LangGraph: Agent Executor
LangChain
49 LangGraph: Chat Agent Executor
LangGraph: Chat Agent Executor
LangChain
50 LangGraph: Human-in-the-Loop
LangGraph: Human-in-the-Loop
LangChain
51 LangGraph: Dynamically Returning a Tool Output Directly
LangGraph: Dynamically Returning a Tool Output Directly
LangChain
52 LangGraph: Respond in a Specific Format
LangGraph: Respond in a Specific Format
LangChain
53 LangGraph: Managing Agent Steps
LangGraph: Managing Agent Steps
LangChain
54 LangGraph: Force-Calling a Tool
LangGraph: Force-Calling a Tool
LangChain
55 LangGraph: Multi-Agent Workflows
LangGraph: Multi-Agent Workflows
LangChain
56 Streaming Events: Introducing a new `stream_events` method
Streaming Events: Introducing a new `stream_events` method
LangChain
57 Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
LangChain
58 OpenGPTs
OpenGPTs
LangChain
59 Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
LangChain
60 LangGraph: Persistence
LangGraph: Persistence
LangChain

This video teaches how to utilize LangGraph's Map Reduce functionality for parallelizing and aggregating results in tasks such as joke generation and log processing, highlighting the importance of controlability and parallelization in efficient task processing. By understanding how to apply Map Reduce and the send API in LangGraph, viewers can enhance their skills in designing and implementing autonomous workflows and multi-agent systems. The video demonstrates practical applications and provide

Key Takeaways
  1. Generate a list of subjects related to a topic
  2. Generate a joke for each subject in parallel using Map Reduce
  3. Aggregate the results to get the best joke
  4. Kick off the map reduce process
  5. Run the map reduce process to generate jokes about animals
  6. Use LangGraph's send API to automate graph creation
  7. Visualize graph with LSmith to investigate steps
  8. Map topic generation and joke creation using MapReduce
💡 The use of Map Reduce in LangGraph allows for efficient parallelization and aggregation of results, making it a powerful tool for tasks that require processing large amounts of data or generating content in parallel.

Related Reads

📰
AI Agents and Open-Source Bounties: Revolutionizing Software Development Through Collaboration
Learn how AI agents and open-source bounties are transforming software development through collaborative efforts, and discover ways to apply these concepts to your own projects
Dev.to AI
📰
Gate Agent Evals by Severity, Not a Flat Pass-Rate
Learn to gate agent evals by severity for more nuanced deploy decisions, rather than relying on a flat pass-rate
Dev.to AI
📰
Beyond the chat window: Designing trustworthy AI assistants for public services
Design trustworthy AI assistants for public services by considering user experience and transparency
Medium · AI
📰
What Is Vibe Video? The New Way to Create Films by Talking to AI
Learn how to create films by talking to AI using Vibe Video and Claude, a capable AI agent
Medium · AI
Up next
AI Agents Are Starting to Talk to Each Other... Without Us.
PlivoAI
Watch →