LangChain Expression Language (LCEL)
Key Takeaways
This video introduces LangChain's Expression Language (LCEL) and its implementation with OpenAI's LLMs, covering the basics of LCEL, its comparison to traditional methods, and its application in building complex prompt-based systems.
Full Transcript
In this chapter, we're going to be taking a look at line chains expression language. We'll be looking at the runnables, the serializable and parallel of those, the runnable pass through, and essentially how we use LEL in its full capacity. Now, to do that, well, what I want to do is actually start by looking at the traditional approach to building chains in lang chain. So to do that, we're going to go over to the L cell chapter and open that in collab. Okay. So let's come down. We'll do the prerequisites. As before, nothing major in here. The one thing that is new is dock array. That's because later on, as you'll see, we're going to be using this as an example of the parallel capabilities in El Cell. If you want to use Lime Smmith, you just need to add in your Lime Train API key. Okay. And then let's Okay. So now let's dive into the traditional approach to chains in line chain. So the ln chain I think is probably one of the first things introduced in line chain if I'm not wrong. This takes a prompt and feeds it into an LM and that that's it. It you can also you can add like output passing to that as well but that's optional and I don't think we're going to cover it here. So what that might look like is we have for example this prompt template here. Give me a small report on topic. Okay. So that would be our prompt template. We'd set up as we usually do with the prompt templates as we've seen before. We then define our LM. We need our openi API key for this which as usual we would get from platform.opai.com. Then we go ahead. I'm just showing you that you can invoke the LM there. Then we go ahead actually define a output pass. So we do do this. I wasn't sure we did but we would then define our LM chain like this. Okay. So LM chain we add in our prompt adding our LLM adding our passer. Okay. This is the traditional approach. So I would then say okay retrieve augmented generation and what's going to do is it's going to give me a little report back on on rag. Okay, takes a moment but you can see that that's what we get here. We can format that nicely as we usually do and we get okay look we get a nice little report. However, the LM chain is one, it's quite restrictive, right? We have to have like particular parameters that have been predefined as being usable, which is, you know, restrictive and it's also been deprecated. So, you know, it this isn't the standard way of doing this anymore, but we can still use it. However, the preferred method to building this and building anything else really or chains in general in line chain is using also, right? And they're super simple, right? So we just actually take the prompt lm out passer that we had before and then we just chain them together with these pipe operators. So the pipe operator here is saying take what is output from here and input it into here. Take what is output from here and input it into here. It's all it does. It's super simple. So put those together and we invoke it in the same way and we'll get the same output. Okay? And that's what we get. There is actually a slight difference on what we're getting out from there. You can see here we got actually a dictionary but that is pretty much the same. Okay, so we get that and as before we can display that in markdown with this. Okay, so we saw just now that we have this pipe operator here. It's not really standard P Python syntax to use this or at least it's definitely not common. It's it's it's an aberration of the intended use of Python. I think anyway it does it looks cool and when you understand it, I kind of get why they do it because it makes it does make things quite simple in comparison to what it could be otherwise. So, I kind of get it. It's a little bit weird, but it's what they're doing and I'm teaching it else. That's what we're going to learn. So, what is that pipe operator actually doing? Well, it's as I mentioned, it's taking the output from this putting it as input into into what is ever on the right. But how does that actually work? Well, let's actually implement it ourselves without line chain. So we're going to create this class called runnable. This class when we initialize it, it's going to take a function. Okay, so this is literally a Python function. It's going to take that and it's going to essentially turn it into what we would call a runnable in line chain. And what does that actually mean? Well, it doesn't really mean anything. It just means that when you use run the invoke method on it, it's going to call that function in the way that you would have done otherwise. All right. So using just function you know brackets open parameters brackets close it's going to do that but it's also going to add this method this or method. Now this or method in typical Python syntax now this or method is essentially going to take your runnable function the one that you initialize with and it's also going to take an other function. Okay, this other function is actually going to be a runnable, I believe. Yes, it's going to be a runnable just like this. And what it's going to do is it's going to run this runnable based on the output of your current runnable. Okay, that's what this or is going to do. Seems a bit weird maybe, but I'll explain in a moment. We'll see why that works. So I'm going to chain a few functions together using this or method. So first we're just going to turn them all into runnables. Okay. So these are normal functions as you can see normal Python functions. We then turn them into this runnable using our runnable class. Then look what we can do. Right? So we we're going to create a chain that is going to be our runnable chained with another runnable chained with another runnable. Okay, let's see what happens. So we're going to invoke that chain of runnables with three. So what is this going to do? Okay, we start with five. We're going to add five to three. So we'll get eight. Then we're going to subtract five from eight to give us three again. And then we're going to multiply three by five to give us 15. And we can invert that and we get 15. Okay, pretty cool. So that is interesting. How does that relate to the pipe operator? Well, that pipe operator in Python is actually a shortcut for the or method. So, what we've just implemented is the pipe operator. So, we can actually run that now with the pipe operator here and we'll get the same get 15. Right? So, that's that's what line chain is doing like under the hood. That is what that pipe operator is. It's just chaining together these multiple runnables as we'd call them using their own internal ore operator. Okay, which is cool. I I I will give them that. It's is kind of a cool way of doing this. It's creative. I wouldn't have thought about it myself. So yeah, that is a pipe operator. Then we have these runnable things. Okay, so this is a this is different to the runnable I just defined here. This is we define this ourselves. This is not a lang chain thing. We didn't get this from lang chain. Instead, this runnable lambda object here that is actually exactly the same as what we just defined. All right. So what we did here, this runnable, this runnable lambda is the same thing but in lang chain. Okay. So if we use that, okay, we use that to now define three runnables from the functions that we defined earlier, we can actually pair those together now using the the pipe operator. You could also pair them together if you want with the or operator, right? So we could do what we did earlier. We can invoke that. Okay? Or as we were doing originally, we use pipe operator exactly the same. So this runnable lambda from line chain is just what we what we just built with the runnable. Cool. So we have that. Now let's try and do something a little more interesting. We're going to generate a report and we're going to try and edit that report using this this functionality. Okay. So give me a small report about topic. Okay. We'll go through here. We're going to get our report on AI. Okay. So we have this. So you can see that AI is mentioned many times in here. Then we're going to take a very simple function. All right. So I'm just extract fact. This is basically going to take uh what is it? See taking the first. Okay. So we're actually trying to remove the introduction here. I'm not sure if this actually will work as expected, but it's it's fine. Try anyway. But then more importantly, we're going to replace this word. Okay, so we're going to replace an old word with a new word. Our old word is going to be AI. Our new word is going to be Skynet. Okay, so we can wrap both of these functions as runnable lambdas. Okay, we can add those as additional steps inside our entire chain. All right, so we're going to extract, try and remove the introduction, although I think it needs a bit more processing than just splitting here. And now we're going to replace the word. We need that actually to be AI. Run that. Run this. Okay. So now we get artificial intelligence. Skynet refers to the simulation of human intelligent process by machines. And then there uh we have narrow skynet, weak skynet and strong skynet. Applications of skynet. Skynet technologies being applied numerous fields including all these things scary. Despite potential Skynet poses several challenges, systems can perpetrate existing biases. It raises significant privacy concerns. It can be exploited for malicious purposes. Okay, so we have all these, you know, it's just a silly little example. We can see also the introduction didn't work here. The reason for that is because our introduction includes multiple new lines here. So I would actually if I want to remove the introduction, we should remove it from here. I think this is a I I would never actually recommend you do that. Uh because it's not it's not very flexible. It's not very robust, but just so I show you that that is actually working. So let's extract fact runnable. Right. So now we're essentially just removing the introduction. Right? Why would we want to do that? I don't know but it's there just so you can see that we can have multiple of these runnable operations running and they can be whatever you want them to be. Okay, it is worth noting that the inputs to our functions here were all single arguments. Okay, if you have function that is accepting multiple arguments, you can do that. The way that I would probably do it or you can do it in multiple ways. One of the ways that you can do that is actually write your function to accept those four arguments but actually do them through a single argument. So just like a single like X which would be like a dictionary or something and then just unpack them within the function and and use them as needed. That's just one way you can do it. Now we also have these different uh runnable objects that we can use. So here we have runnable parallel and runnable pass through. Kind of self-explanatory to some degree. So let me let me just go through those. So runnable parallel allows you to run multiple runnable instances in parallel. Runnable pass through may less self-explanatory allows us to pass a variable through to the next runnable without modifying it. Okay, so let's see how they would work. So, we're going to come down here and we're going to set up these two dock arrays or these two sources of information. And we're going to need our ln to pull information from both of these sources of information in parallel, which is going to look like this. So, we have these two sources of information, vector store A, vector store B. This is our dock array A and dock array B. These are both going to be fed in as context into our prompt. Then ILM is going to use all of that to answer the question. Okay. So to actually implement that we have our we need an embedding model. So he's opening our embeddings. We have our vectors A, Vur B. They're not, you know, real vector. They're not full-on vectors here. We're just passing in a very small amount of information to both. So we're saying okay we're going to create an in-memory vector store using these two bits of information. So when say half the information is here this would be a relevant piece of information. Then we have the relevant information which is deepseek v3 was released in December 2024. Okay. Then we're going to have some other information in our other vector saw. Again irrelevant piece here and relevant piece here. Okay. The Deep Seek V3LM is a mixture of experts model with 671 billion parameters at its largest. Okay. So based on that, we're also going to build this prompt string. So we're going to pass in both of those contexts into our prompt. Then we're going to ask a question. We don't actually need we don't need that bit. And actually we don't even need that bit. What am I doing? So we just need this. So we have the both the context and we would run them through our prompt template. Okay. So we have our system prompt template which is this. And then we're just going to have okay our question is going to go into here as a user message. Cool. So we have that and then let me make this easier to read. We're going to convert both those stores to retrievers which just means we can retrieve stuff from them. And we're going to use this runnable parallel to run both of these in parallel. Right? So these are being both being run in parallel. But then we're also running our question in parallel because this needs to be essentially passed through this component without us modifying anything. So when we look at this here, it's almost like okay the this section here would be our runnable parallel and these are being run in parallel but also our query is being passed through. So it's almost like there's another line there which is our runnable pass through. Okay, so that's what we're doing here. These are running in parallel. One of them is a pass through. I need to run here. I just realized here we're using the uh deprecated embeddings. Just switch it to this. So line chain open AI. We run that. Run this. Run that. And now this is set up. Okay. So we then put our initial. So this using our runnable parallel and runnable pass through. That is our initial step. We then have our prompt lm now put parser which are being chained together with the usual you know the usual pipe operator. Okay. And now we're going to invoke the question. What architecture does the mod Deepseek released in December use? Okay. So for the LM to answer this question, it's going to need to tell us well it needs the information about the DeepS model that was released in December which we have specified in 1/2 uh here. And then it also needs to know what architecture that model uses which is defined in the other half over here. Okay, so let's run this. Okay, there we go. Deepc V3 model released in December 2024 is a mixture of experts model with 671 billion parameters. Okay, so mixture of experts and this many parameters. Pretty cool. So we've put together our pipeline using LE cell using the pipe operator the runnables specifically we've looked at the runnable parallel runnable pass through and also the runnable lambers. So that's it for this chapter on Elsel and we'll move on to the next
Original Description
This chapter will introduce LangChain's Expression Langauge (LCEL). We'll focus on understanding how LCEL works under the hood and how it is implemented with OpenAI's LLMs.
We'll compare LCEL against the traditional methods. We will build a pipeline where the user inputs a specific topic, and then the LLM looks for and returns a report on the specified topic. This generates a research report for the user.
🔗 Full Course: https://www.aurelio.ai/course/langchain
📌 Article and Code: https://www.aurelio.ai/learn/langchain-lcel
💡 Subscribe for Latest Courses and Tutorials:
https://www.aurelio.ai/subscribe
👋🏼 AI Consulting:
https://aurelio.ai
👾 Discord:
https://discord.gg/c5QtDB9RAP
Twitter: https://twitter.com/jamescalam
LinkedIn: https://www.linkedin.com/in/jamescalam/
#ai #langchain #programming #aiagents
00:00 LangChain Expression Language
00:58 Traditional Chains in LangChain
03:02 LangChain LCEL
03:55 LCEL Pipe Operator
09:09 LangChain RunnableLambda
12:41 LCEL Runnable Parallel and Passthrough
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from James Briggs · James Briggs · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Stoic Philosophy Text Generation with TensorFlow
James Briggs
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
Every New Feature in Python 3.10.0a2
James Briggs
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
How-to use the Kaggle API in Python
James Briggs
Language Generation with OpenAI's GPT-2 in Python
James Briggs
Text Summarization with Google AI's T5 in Python
James Briggs
How-to do Sentiment Analysis with Flair in Python
James Briggs
Python Environment Setup for Machine Learning
James Briggs
Sequential Model - TensorFlow Essentials #1
James Briggs
Functional API - TensorFlow Essentials #2
James Briggs
Training Parameters - TensorFlow Essentials #3
James Briggs
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
Building a PlotLy $GME Chart in Python
James Briggs
How-to Use The Reddit API in Python
James Briggs
How to Build Custom Q&A Transformer Models in Python
James Briggs
How to Build Q&A Models in Python (Transformers)
James Briggs
How-to Decode Outputs From NLP Models (Python)
James Briggs
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
Unicode Normalization for NLP in Python
James Briggs
The NEW Match-Case Statement in Python 3.10
James Briggs
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
How to Build Python Packages for Pip
James Briggs
How-to Structure a Q&A ML App
James Briggs
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
Q&A Document Retrieval With DPR
James Briggs
How to Use Type Annotations in Python
James Briggs
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
Sentence Similarity With Sentence-Transformers in Python
James Briggs
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
NER With Transformers and spaCy (Python)
James Briggs
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
New Features in Python 3.10
James Briggs
Training BERT #5 - Training With BertForPretraining
James Briggs
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
Faiss - Introduction to Similarity Search
James Briggs
Angular App Setup With Material - Stoic Q&A #5
James Briggs
Why are there so many Tokenization methods in HF Transformers?
James Briggs
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
How LSH Random Projection works in search (+Python)
James Briggs
IndexLSH for Fast Similarity Search in Faiss
James Briggs
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
Build NLP Pipelines with HuggingFace Datasets
James Briggs
Composite Indexes and the Faiss Index Factory
James Briggs
More on: LLM Foundations
View skill →Related Reads
📰
📰
📰
📰
Your app has two caches. What if it only needed one?
Dev.to · Dencio
Run a Full JavaScript Website with AxonASP — No Node.js Required
Dev.to · Lucas Guimarães
A complete Laravel API with clean architecture in 30 seconds, tests included
Dev.to · Loic Aron Mbassi Ewolo
Pain point #2: Django’s ORM Performance Problem
Medium · Python
Chapters (6)
LangChain Expression Language
0:58
Traditional Chains in LangChain
3:02
LangChain LCEL
3:55
LCEL Pipe Operator
9:09
LangChain RunnableLambda
12:41
LCEL Runnable Parallel and Passthrough
🎓
Tutor Explanation
DeepCamp AI