1
00:00:04,000 --> 00:00:10,000
Hello, and welcome to this video on
the LangChain LCEL Chaining Method.

2
00:00:10,000 --> 00:00:12,800
After watching this video, you'll be able to

3
00:00:12,800 --> 00:00:19,100
Describe how to build flexible, composable chains using
LangChain's modern approach to prompt engineering.

4
00:00:19,100 --> 00:00:22,079
Structure prompts effectively using templates.

5
00:00:22,079 --> 00:00:25,840
Connect components using the pipe
operator to streamline workflows.

6
00:00:25,840 --> 00:00:29,959
And develop reusable patterns for a variety of AI applications.

7
00:00:29,959 --> 00:00:34,860
LangChain Expression Language (or LCEL)
is a pattern for building LangChain applications

8
00:00:34,860 --> 00:00:38,080
that utilizes the pipe (|) operator to connect components.

9
00:00:38,080 --> 00:00:41,559
This approach ensures a clean, readable
flow of data from input to output.

10
00:00:41,559 --> 00:00:46,119
LangChain has evolved significantly, and this
video will focus on the newer, recommended

11
00:00:46,119 --> 00:00:50,479
LCEL pattern rather than the traditional LLM chain approach.

12
00:00:50,479 --> 00:00:55,759
This modern method provides better composability,
clearer visualization of data flow, and greater

13
00:00:55,759 --> 00:00:58,560
flexibility when constructing complex chains.

14
00:00:58,560 --> 00:01:02,000
To create a typical LCEL pattern, you need to

15
00:01:02,000 --> 00:01:05,040
Define a template with variables and curly braces

16
00:01:05,040 --> 00:01:12,220
Create a prompt template instance. Build a chain using the pipe
operator to connect components. Invoke the chain with input values.

17
00:01:12,220 --> 00:01:15,919
Let's see this in action with a concrete example.

18
00:01:15,919 --> 00:01:19,839
In LangChain, runnables serve as an interface
and building blocks that connect different

19
00:01:19,839 --> 00:01:24,680
components like LLMs, retrievers, and tools into a pipeline.

20
00:01:24,680 --> 00:01:27,860
There are two main runnable composition primitives.

21
00:01:27,860 --> 00:01:34,919
Runnable sequence chains components sequentially,
passing the output from one component as input to the next.

22
00:01:34,919 --> 00:01:39,699
RunnableParallel runs multiple components
concurrently while using the same input for each.

23
00:01:39,699 --> 00:01:43,779
However, LCEL provides elegant syntax shortcuts.

24
00:01:43,779 --> 00:01:48,260
For example, instead of using runnable sequence,
the same sequential chain can be created by

25
00:01:48,260 --> 00:01:54,099
simply connecting runnable 1 and runnable 2 with a
pipe, making the structure more readable and intuitive.

26
00:01:54,099 --> 00:01:58,099
LCEL also handles type coercion automatically.

27
00:01:58,099 --> 00:02:01,339
This means it converts regular code into runnable components.

28
00:02:01,339 --> 00:02:06,819
When you use a dictionary, it becomes a runnable
parallel, which runs multiple tasks simultaneously.

29
00:02:06,819 --> 00:02:11,460
When you use a function, it becomes a
RunnableLambda, which transforms inputs.

30
00:02:11,460 --> 00:02:15,759
This happens behind the scenes, so you don't
have to handle the conversion manually.

31
00:02:15,759 --> 00:02:20,660
For example, in this code, the pipe operator
combines the prompt templates with the LLM.

32
00:02:20,660 --> 00:02:26,619
The dictionary structure creates a RunnableParallel
which processes all three tasks simultaneously.

33
00:02:26,619 --> 00:02:31,179
Each task receives the same input, text, but processes it differently.

34
00:02:31,179 --> 00:02:34,479
When you run this, it automatically becomes a RunnableParallel.

35
00:02:34,479 --> 00:02:41,259
The result will contain three keys, summary, translation, and
sentiment, each with the output from the respective LLM call.

36
00:02:41,259 --> 00:02:45,039
Let's see LCEL in action by creating a simple chain.

37
00:02:45,039 --> 00:02:49,020
This code demonstrates how components
can be connected using the pipe operator.

38
00:02:49,020 --> 00:02:53,259
The RunnableLambda in this chain wraps the
format_prompt function, transforming it into

39
00:02:53,259 --> 00:02:55,860
a runnable component that LangChain can work with.

40
00:02:55,860 --> 00:03:00,220
When the chain runs, RunnableLambda takes
the input dictionary, containing adjective

41
00:03:00,220 --> 00:03:04,059
and content keys, passes this dictionary to the format_prompt function.

42
00:03:04,059 --> 00:03:07,059
The function formats the prompt template with these variables.

43
00:03:07,059 --> 00:03:10,699
The formatted prompt is then passed to the next component, the LLM.

44
00:03:10,699 --> 00:03:14,660
The pipe operator creates a sequence by
connecting runnable components together.

45
00:03:14,660 --> 00:03:19,419
In this joke chain, first, the RunnableLambda
formats the prompt with variables.

46
00:03:19,419 --> 00:03:23,339
The pipe operator passes the formatted prompt to the LLM.

47
00:03:23,339 --> 00:03:27,460
Another pipe passes the LLM's response to the StrOutputParser.

48
00:03:27,460 --> 00:03:32,899
We've covered the essentials of LCEL, from
its benefits and composition primitives to

49
00:03:32,899 --> 00:03:36,020
building practical chains using the pipe operator.

50
00:03:36,020 --> 00:03:40,339
Keep in mind that LCEL is best suited for simpler orchestration tasks.

51
00:03:40,339 --> 00:03:46,979
For more complex workflows, consider using LangGraph
while still leveraging LCEL within individual nodes.

52
00:03:46,979 --> 00:03:51,380
As you develop your own applications, take
advantage of LCEL's strengths, including

53
00:03:51,380 --> 00:03:57,339
parallel execution, async support, simplified
streaming, and automatic tracing.

54
00:03:57,339 --> 00:04:02,059
These capabilities enhance both the power
and maintainability of your applications.

55
00:04:02,059 --> 00:04:03,300
In this video, you learned that

56
00:04:03,300 --> 00:04:09,059
LCEL pattern structures workflows use
the pipe operator for clear data flow.

57
00:04:09,059 --> 00:04:13,020
Prompts are defined using templates
with variables and curly braces.

58
00:04:13,020 --> 00:04:16,540
Components can be linked using
RunnableSequence for sequential execution.

59
00:04:16,540 --> 00:04:20,700
RunnableParallel allows multiple components
to run concurrently with the same input.

60
00:04:20,700 --> 00:04:26,420
LCEL provides a more concise syntax by replacing
RunnableSequence with the pipe operator.

61
00:04:26,420 --> 00:04:31,380
Type coercion in LCEL automatically converts functions
and dictionaries into compatible components.