LangChain Runnables & LCEL Full Guide | Structuring AI Workflows | Agentic AI Tutorial @SCALER

SCALER · Beginner ·🔧 Backend Engineering ·5mo ago

Key Takeaways

LangChain Runnables and LangChain Expression Language (LCEL) are used to compose, chain, and control complex AI workflows in a clean and scalable manner. The video explains how to use LangChain Runnables and LCEL to structure AI workflows, including creating runnables, chaining them together, and using prompt templates.

Full Transcript

Hello everyone, welcome to this particular video. We are going to talk about a concept which is very important and it is so important that the name of the library which is lang chain has been kept on this particular concept. So today we are going to talk about the concept of chains. I know that we have already done it in the last particular video but in this particular video I'm going to teach you the backbone of all these chains and that particular backbone is known as runnable. So we will talk about why the creators of lang chain created this concept which is runnable which was initially not the part of the lang chain community or the lang chain library or as a framework. So we will start the discussion with the understanding about why the creators of the lang chain has to introduce the concept of runnables and what are the different type of runnables which are available in the lang chain framework. Uh it is going to be a mix of theoretical plus practical video and I believe you guys are going to enjoy it and you are going to really love it because the kind of content which we have put in in this particular video is is unmatchable. But before we get started, make sure you subscribe to the channel so you don't miss the rest of the Agentic AI playlist. And if you are serious about building a strong career in AI, check out Scala's advanced AI and machine learning program co-certified with IIT ROI CEC. It's designed to help you master the skill needed for the real world AI role and get you ready for that. For more information, check the link in the description box. Also, tell us in the comment what other AI topics you would like us to cover. So, let's get started. What exactly are the runnables and why we need them? So let's talk about runnables. Now to understand runnables we have to go back little bit the evolution of this library which is lang chain. So when the creators of lang chain were working on this library, they identified some use cases and these use cases were basically the projects or I can say applications of multiple components of the lang chain and the very popular application is the is the rag application. ation is the rag application and uh in the very first video I have already talked about what rag is though we will also be talking in detail about each component of rag in my next video but let's understand in a very small detail about what exactly rag is so as all of you know that LLMs they are trained on humongous amount of the open-source data But these LLMs are not aware about your content or your context. So let's say if you have a PDF file and if you want your LLM to answer your questions based on this PDF file, your LLM will not be able to do so. And another way to get it done is you provide this entire PDF file to the LLM. But the problem is if you provide the entire PDF file, there will be a problem of limitation regarding the context window size. Right? So in nutshell we have to think about a way through which I can provide my context to my LLM and I will be able to talk to my PDF using this particular LLM and that is where the concept of rack comes into picture. So what we are going to do essentially is we are going to first of all get this PDF file loaded to our system. Then we are going to create a smaller chunks of this PDF file because I cannot provide the entire PDF file to my LLM. I can only provide a portion of it. So let's say these are the pages of my PDF as of now. So let's say page one, page two, page three, page four. So essentially we are doing the split task over here. Once this is loaded, then we have to convert this PDF page into a representative number because your LLM is a mathematical model and it cannot understand the text. It can understand only the numbers. So we have to convert each page into a vector. Let's call it as v_sub_1, v_ub_2, v_ub3 and then v4 and so on. So these are known as the word embeddings and as of now they'll be known as the page embeddings. Now embeddings are basically the numerical representation of the entire text that is present in this page into a representative number which is not only representing this text which is the part of this page into a number also capturing the context and this is known as the semantic context semantic context. So once we have generated the embeddings now I need the user query. So here we have the user query. So once a user provides a query I need some kind of system which is known as a driver. I will write it as R which can take this particular user query and can search for the closest match. So this is another component that I would need which can take the user query and can search for the closest match of the document. So which particular embedding which essentially means which particular page might have the answer to this question. Now let's say this vector number two is the closest match of the user query. So retriever will return me this vector and I will provide this particular page which is page number two together with the user query user query to my LLM. And now since the LLM has the question which is the user query as well as the context which is the page it will be able to answer back to the user. Right? So this is what rag is. This is what the entire uh workflow of rag systems are. Now the question is why we are talking about it. The reason we are talking about it is because when the creators of lang chain were working on this particular library and the entire framework, they identified that they have multiple systems which are getting connected to create the entire AI based application. This is system number one or component number one which is the loader. This is component number two which is the splitter. This is component number three which is the embedding generator. This is component number four which is the retriever. This is component number five u which is prompt generator. This is component number six which is your LLM. So you can see that in this entire uh workflow of rag we have connected six components in a very specific order. So the lang chain people and the creators of langchain they identified such use cases. So what they thought why not to create a readymate function so that people can quickly use those functions and can get their task done. So they created multiple functions which were essentially the chains and when I say chain I basically means a workflow workflow in a very specific order. So what they did they identified all these use cases they created readym made functions and made it available on their particular library which was lang chain. Now there was a problem. The problem was that they kept on creating so many functions that the entire list of these readymate functions was blown up. So there were more than 100 100 plus functions that they created on various use cases and this increased the size of their entire library because all these functions were getting preserved in the lang chain database or the lang chain entire library data. Also, it became very difficult for someone to learn the entire library because if you want to use a specific function which can do a specific task based on your use case, you need to be aware about all these 100 plus functions first of all. So, it was very difficult for someone to actually understand what these functions are and how many such functions are. Additionally, what is the use case of each function? Another problem was if let's say I need to connect one function with another function. It was yet another trouble for a developer because the function that was written was written in a very different or a very specific way that was not compatible to easily connect with another function. So there were series of problems. Let me show you what I mean by this. So I will not be able to run this code because these codes are now depreciated. So I can just show you what I'm trying to say. So this is code number one and I have written this code will not work. And here you can see you are getting a blue line because in lang chain library uh the support of chains are now depreciated. So you don't have llm chains and these prompts are now the part of langchain core. So that is the reason you are getting another yellow color line. But let's say we have created a prompt template and this is chain number one. This LLM chain the task of LLM chain is to just take the LLM take the prompt and do the sequential task. So what I mean by sequential task? So this LLM chain what it will do if I represent this in a visual format. It will take the prompt from the user. it will provide it to the LLM and it will generate the response. So here you can see that this entire workflow is something that we have already implemented using the concept of the pipe operator. But if we have to do it in the year 2023, we can use the LLM chains. Now LM chain is basically the readymade function which was available for the community to use it whenever they have this kind of sequential task. So they can create a dynamic prompt by taking input from the user. This dynamic prompt will be generated with this prompt template based on the user input. Then it will be passed to the LLM and then the response will be generated. Yet another function that is available and was available was the let me just pull it out from my notebook. Uh let me add a code. This was simple sequential chain. So simple sequential chain provides you the flexibility of adding multiple chains. Let's say I have this particular prompt and this particular chain is taking this prompt which is generating one signopsis for this particular topic. I have another chain based on this prompt which is writing the review of the particular signopsis and then using the simple sequential chain we are connecting this particular chain over here with this particular chain and then we are running it. So like this there were multiple functions that they created. So this is just one or two example that I was able to pull out. uh for example the LLM chain and then the sequential chain but like this they created 100 plus functions and that was the major problem so they thought let's try to remove this particular problem they cannot go about creating endless number of functions and they cannot go about creating uh the problem for the developer community. So they thought let's try to simplify this problem. But when they started simplifying this problem they end up in another problem. So when they looked at various components of lang chain. So what were the components of langchain? Let's say I would talk about the model component. Let's talk about the prompt component. Let's talk about the document loaders or maybe talk about the retrievers. So all these components were written in such a way that they were getting called with a different method. So if you have to use the model the model component of lang chain we used to call this using dot predict and I'm talking about the time when they did not they did not created a universal method to call each of this component. So in 2023 we were able to call the model uh the model component using the predict method. Similarly for prompt we were using format method. Similarly for loaders we were using dot load method. Similarly for retrievers we were using dot get relevant documents method. And similarly for parser we were we were using the parse parse method. So for parsers we were using the dot parse method. Now this was the problem. If someone wants to create a chain such that if they execute this the entire flow starts executing by itself. The problem was since every component of the lang chain was getting called in a different way or there was difficulty in connecting these components C1, C2, C3, C4 and so on. So that was another problem. So they thought let's go back and check our basics. They said we have to standardize it. We have to get rid of all this uh mess. I cannot create so many functions. So this is not acceptable. I cannot let people connect this component with this component using different different methods to call that particular component. So that is also not going to work out. They said let's go back to our basics and let's standardize them all together. And that is where the concept and the ideation of runnables came into picture. So what they did they created one abstract class. So this is the abstract class. And those of you who are not aware about the abstract class, abstract class is basically a class in Python that cannot be instantiated directly. And it is basically meant for a blueprint purpose. So abstract class is not solving any problem. It is just a blueprint that it provides to the another or other subclasses other subclasses. So if I have defined a particular method over here then all the subclasses which are inherent to the abstract class has to make sure that this method is implemented in that particular class also. So abstract class is basically a very simple logic. It consists of a blueprint. It consists of various methods and when a subclass inherit the abstract class this abstract class will will make sure that all these subclasses are basically implementing this method in their implementation so that there is a universal standardization that is happening. So all the subasses let's call it as SC1 SC2 and SC3. So all these subasses that are inheriting to this abstract class will have this method or these method implemented in their class also. So if I call this method as M. So I will find this method over here also over here also and over here also. So that is what they did. They created a abstract class which they called as runnable and all the components of the lang chain made inherit to this abstract class. So they created the runnable as a abstract class and all the components let's say the model component the parser component the prompt template component the retriever component the document loader So all these classes were inheriting to this particular abstract class which made sure that all of them are using the same methods and which all methods they used. So number one, this abstract class enforced all these subclasses to use the method dot invoke. This made sure that if I want to execute model, I can use invoke. If I want to uh execute parsers, I can use invoke. For prompt template, I can use invoke. For retrievers, I can use invoke. For document loaders also I can use invoke. There was few more methods that were introduced which were batch. So if you want to invoke multiple inputs at a time, you can use a batch for multiple inputs or they introduced a method which was a stream for streaming output str and they also introduced some async versions like a invoke like a batch and like a stream. So these were the methods that were the part of abstract class and since all these classes were inheriting to the abstract class these methods became compulsory for these particular classes or subasses to also implement and that is the reason I hope now you guys are able to understand why they all together created lang core because there was a lot of mess that happened because of this ideation of creating endless number of functions for each use case. So they bifurcated the entire library into multiple sub libraries and langu core now comes with all these important components which are all the runnables. So that was the concept of runnable. Now what is the advantage of runnable? The advantage is that now if you want to connect one component of lang chain with another component, you do not have to worry about uh connecting this with this because they are now the runnable in itself because you can call it using invoke. You can call this also with invoke. And since all these components are inheriting with the runnable class which is the abstract class. Each one of them is a runnable in itself. And since all of them can be called using the same method which is invoke. Now you can seamlessly connect them. And that was the advantage of runnables. So Runnable made it very simple for the developers to create chains and create complex chains without them worrying about these endless functions. So they do not need to or you guys do not need to learn about any of these functions. Now all you need to learn is the concept of these runnables. Now in lang chain we have two kinds of runnables. one are the runnable components. So these are those runnables which are basically the part of your lang chain framework. It is also known as the task specific runnables. Task specific runnables. So these are those components which are already the part of lang chain and lang chain creators converted them into a runnable. For example, chat open AI. Now we have used it a lot, right? And this is itself a runnable. For example, prompt template we have used this also and this itself is a runnable. for example um a string output parser and this is also a runnable for example the retriever and this also is a runnable. So all the components that you find in the lang chain uh library is a runnable in itself. So these are your task specific runnables. Now, Langchen community, the creators of Langchen also created some more additional runnables which were known as the runnable primitives runnable primitives. Now, these runnables are like the connector runnables. These runnables were helping in the process of orchestrating the execution process. So they created a runnable which was runnable sequence. Similarly they created a runnable which was runnable parallel. They created a runnable which was runnable lambda. They created a runnable which was runnable pass through. So we have talked about some of them. I think uh we have talked about runnable branch also in the last video but in this video I will tell you a more detailed explanation of it. Remember in the last video what we were doing we were taking the prompt we were using this pipe operator we were connecting it with the llm pipe operator we were connecting it with the parser. So this was your prompt, this was your LLM and this was your parser. And this particular pipe operator is basically the sequence runnable. It is also known as the LC lang chain expression language which is equivalent to runnable sequence. So we have already seen how we can create a sequence chain. So this is your lang chain component which is a task specific runnable. This is your this pipe operator is your runnable sequence which is a runnable primitive which is helping you to connect one runnable component with another runnable component in a very specific way. And using these runnable primitives together with these runnable components you can create any application that you want. you can create or you can connect any launching component in a specific way without worrying about how to call it because all of these components are getting called with one single method that is invoke and that was your runnable. So this is the theoretical concept of runnable and I hope I was able to explain you in a much detail about what exactly runnable is. Now let's try to understand and implement some of these runnables. So we will start with the runnable sequence which we have already done it but let's try to do it again using the pipe operator which is the LCEL. So runnable runnable sequence. Now what we are going to do uh we are going to get uh the prompt template. So from langchain_core dot prompt import prompt template. I have already created my model. So over here we have created GM which is the generative model of Google Gemini. So I will not be using that. Uh I think lang chain core is already 1 second I have done it. Let me run it to just cross check whether my no module lang l a n g c h a i n I have written it correctly. Let me run it again. Maybe I have to run this again. So let it set up. It will take about a minute. Okay. So we are able to run it. Let's also get the output parser. So from lang chain_core dot output parser we'll get the input uh string output parser and as of now I don't need this so let's create our prompt so let's create a prompt c is equal to prompt template I will define my template which is what's the capital of the country country and here I can put the country name uh let's define the input variable which is the country name and once we are done defining the prompt template Right? We can define the string output parser. So str is equal to string output parser. And then we can create our chain. So chain is equal to we will first create our prompt then pipe operator. It will go to my uh gemini model. And then finally the string output parser and let's use npal this time. Let's see what's the output. So here you can see the capital of Nepal is Kakmandu. Now this is what a runnable sequence look like. This is your runnable component which is a task specific runnable. This is your another runnable component which is a task specific runnable. And using this LCAL which is lang chain expression language we are connecting one task specific runnable with another task specific runnable. And this is your runnable sequence. So this is very simple right let's see how we implement runnable parallel runnable parallel and I think we have already done it in the previous video but this time I will take a different use case so that you guys are able to um able to revise it and understand the importance of runnable parallel this time as you guys already know that runnable parallel will let you execute ute two chains in parallel. So let me create prompt template T1 where we are going to ask about or maybe let me just copy paste the code just to save time because you guys have already done it. So this is the code that I have written and this particular runnable parallel is working over here. So what we have done we have created one prompt template and second prompt template. The first prompt template is getting me the capital of the country. The second prompt template is is specifying five points about why should I visit that country. Now remember one thing a runnable parallel will take a dictionary as a input where the output of this chain will get preserved in this dictionary itself with the key as text one. The output of this chain will get preserved in this particular uh dictionary against the keyword or the val key text_2 and the output of this particular so there is a problem what is the problem oh resource exhausted I think I have crossed my limit so now I have to change my GM to chat GPT. So let me try this out. One second. I think I have exhausted my limit the free usage limit of Gemini model. So that might be one issue. But let me see if this executes then definitely there is a problem in the code. Otherwise, I'll just change the GM to CGPT which is start GPT and it will work fine because I have sufficient credits in my chat open AI which is your GPT open AI. Yeah, that's the issue. So the issue is basically the the limit part. So what I'm going to do is instead of using this I will use CGP over here also CGPT and here I have used CGPT. So let me run it and this will work. Uh what I was trying to explain you is runnable parallel will take the input one input and both the inputs will be passed to your first chain and the second chain. So if I quickly try to help you visually. So visually what will happen? You will design a chain like this. So this is one chain. This is another chain and both the chains are getting the same input and the same input is getting passed to this as well as to this. So this is one thing that you need to remember. Runnable parallel takes one input. Second is it will generate the output as a dictionary and it will be exactly this dictionary. So the third prompt template that I have written needs to take two inputs text one and text two because I'm going to connect the output of this runnable parallel with another prompt template temp 3 which is taking two inputs text one and text two which is exactly the same the key of this particular runnable parallel. So visually this is what is happening. I have created another prompt over here. So in this the output of this is text one colon some value va text 2 colon some value v. And this is basically the output of this chain and this chain. Let's call this as C1 and let's call this as C2. So C1 is the output of your chain number one. C2 is the output of your chain number two. This is basically the output. Now this entire dictionary is getting passed to this prompt template which is generating my dynamic prompt over here. So the prompt is combine the text and summarize together text one and text two. where I'm getting this text underscore 1 from here. Where I'm getting this text underscore2 from here. So this is something which you need to remember. And then once I have used text three or temp 3 which is the prompt the dynamic prompt will go back to my LLM and the LLM will generate the output which will be parsed by this string output parser and this is basically the output that I'm getting. So this one input is going to this chain also to this chain also and this entire output is going to this particular prompt template which is going to this component which is your chat model and finally to the string output parser visually something like this is happening so this is your runnable parallel. Now let's talk about how we can create runnable pass through. I think we have already talked about it in a very brief manner in our last video but this video I will help you with a detailed overview about what is runnable pass through. Runnable pass through is not going to do anything for you. Runnable pass through is a very simple way of making sure that the input is as it is passed to the output. So runnable pass through will take the input and will return you the output the same output without doing anything. Let me show you this. Now this is particular runnable which has no specific task. Runnable pass through is basically going to take your input and return you exactly same output. So let's see what is the exact use case from langchain core.runnables I'm going to import the runnable pass through. Now let me show you uh by creating a example. So runnable pass through I'm creating a runnable parallel. Let me import runnable parallel also. Runnable parallel. So I'm going to create object of runnable parallel. I'm writing original is equal to runnable pass through. Then comma modified is equal to lambda x colon x of num + 1 comma double is equal to lambda x of num into 2 And finally let me invoke this runnable. So run.invoke this will take a dictionary with the input num value as five. Now what you are able to see the reason I have created a parallel runnable is because I'm executing three task or three runnables at a time. First is doing nothing. So whatsoever is the input, it will return you the same output. It is like a gate with no conditions. And here you can see that in my output, I'm getting a particular variable named original which is basically the name of the the input that we created. And this has the output which is exactly the same as the input. And here we have the input as num five. Second runnable modified is doing the addition operation plus one. So I'm taking the so input is a dictionary. So from the dictionary I'm taking the in value and adding. So this will return me a value corresponding to the key num which will be five. 5 + 1 is 6. So against modified I have six. And similarly over here I'm multiplying it with two. So I'm getting 10. So this is a simple use case of what runnable pass through can do. It will return you the same exact output as the input. Now let's see a detailed use case. So what we are going to do is now we are going to create two chains. In the first chain I will ask what is the capital of a country. Second chain I will ask uh to write the or let's take this example also the same example capital of the country right write five points why should I visit this country and then we are combining so let's say I want this output in my in my output basically here what you see you see the final output which is the combination of this and this by using this particular ROM template which is summarizing. So if I go back to my screen what we have done is this C1 is finding out the capital of a country. This C2 is writing five pointers why should I visit this country and this is the input which is the name of the country in the output. So after this we are passing this to a llm and then we are passing it to a parser and finally so this is my parser and finally I'm getting the output and this output is the combined output because this prompt template so let me reduce this okay so this prompt template over here is basically combining or it is asking the LLM to combine this text and this text and you can read it over here. So, temp one is asking the LLM to get the capital of the country. Temp 2 is asking the LLM to write five pointers about the country. Temp 3 is asking the LLM to combine the text one and text two which is generated by the previous parallel chain. And the final output that you get is the summary about the both the text that is generated by the chain one and chain two. So essentially you are not getting this output as an individual output. So what I'm going to do now is let me just copy it and show you what kind of chain we are going to create now. Now I'm going to do one modification. So once I get this output now I'm going to create one more parallel chain this and this. Now what this chain will do this particular chain is a pass through. So I'll write pt and this pass through will return me the entire output over here because this and this both parallel chain will get the same input. So this pass through will get me the output as your text one and the text two and then from here I can pull out the text one which is the uh which is the concern point right now the another parallel chain will do this part for me which is the summary part. So I am now going to create um the extension of this particular chain which we have already done such that now we are able to pull out this intermediate output as the output so that we can do something more on it and this chain will u independently perform the summarization. So let's try to uh first of all copy this entirely and let's do some modifications. So what we are going to do now uh nothing will change till this point. The only thing that will change is we will introduce another parallel chain which will be a runnable parallel. It will take the input. So I will say intermediate mediate intermediate the intermediate output is a runnable pass through a runnable pass through and then another chain will be my summary chain and this will execute this piece of this chain. Now I don't need this. So let me delete it. I hope I have written everything correctly. So comma is here. Yes. Now I will say that uh no I have to also put P chain over here. So P chain will take your 101 temp 2 and temp 1 is temp 2 is coming from. Now there is a problem. This is not required over here. I will now create my final chain which will be Pchain Pchain 2. So let's try to understand what the P chain will do. This chain this chain will execute this piece of prompt template and this piece of prompt template. This will return me the capital of the country which I will provide as the input. This will return me five points regarding why should I visit this. Then the output will be sent to this chain. The first piece of this parallel chain will do nothing and will return me the input which will be this dictionary. The second chain will take the input which is temp text one text two. We create a dynamic prompt. Go to chart GPT create my output. So now if I run this just see the output here you can see that intermediate we are getting this entire dictionary that we created over here right and then we should have the summary also uh over here I think summary summary summary where is summary so I should have print this for easy identification let me do this so if I just write result I should be able to see it. So here you can see we have a summary right and in the intermediate we have the dictionary which is entirely the text one and the text two. So this is basically um what the pass through has done. Now let's say I want to pull out the text one. So since it is a dictionary I can simply write result dot enter med8 dot text_1 and uh that is a problem spelling maybe let's me copy this put it over here uh dict object has no attribute or maybe it is not able to identify it. So let me use the square bracket method and over here also we can do the same thing. A square bracket here you can see the capital of Delhi is oh sorry India is New Delhi. So I hope you are able to understand this. Uh one more runnable that we did not discuss in the last video was the runnable lambda and this is one of the very powerful uh runnable. So runnable the the advantage of using runnable lambda is that runnable lambda can convert any of the Python function into a runnable and when something is a runnable you can seamlessly connect that particular component with other components of lang chain. So let's see the usage of it. So from langchain_core dot runnable import runnable. I want runnable lambda. Where is lambda? Over here. Let me create two functions. Now what these functions are going to do? they are going to first of all clean my email that is received by a user. So what exactly I'm going to do now? We will have a email from a user. Email are generally very badly written. So I will create a runnable lambda which is a python function. So runnable lambda and this runnable lambda will consist of a python function a python defined function which will basically clean the email. The another python function will help me with some metadata about this email. So this is again going to be our enable lambda and this is again going to be a python function python defined function uh to get the meta data metadata like how many words are there how many characters are there and so on. Then it will go to a prompt template uh pt prompt template to create a dynamic prompt. it will go to one lm and then we will call a string output parser. So st. So this is how it will look like. So this is where I will utilize the runnable lambda functionality. So let's create this function uh clean text and I will take the input name data which is going to be a dictionary and uh this is going to return a dictionary. So text is equal to data email because data is a dictionary. From that I'm going to fetch the value corresponding to to the key email. I'm going to strip it in order to remove all the white spaces and then make it all lower case. Then if that particular email so I'm going to preserve it in the same dictionary with the name clean and that is something which I'm going to limit to 200 words. So this is like trim trim the email to limit. Finally this will return the data. I'm going to create another defined function in Python. Add metadata. Uh again this will take data which is a dictionary and will return again another dictionary. I will add the word count in this dictionary and this dictionary already comes with the clean text. Right? So word count will be simply the length of data clean and I have to split it on spaces. Uh right now I'm doing a split on nothing. Nothing means it will take the space as a split on. Maybe we can explicitly mention a space. Oh no, we want word count, right? So let's let's put nothing as of now. Then data character count will be simply is equal to length of or let's not worry about this. Let let's keep it very simple. Return data. I think we can put a space over here. Now we are going to create a runnable using runnable lambda which basically means I'm going to convert this into a runnable using runnable lambda. I'm going to convert this into a runnable using runnable lambda. So clean is equal to runnable lambda and this will be clean text and again meta is equal to runnable lambda and this is meta. So now what we are going to do is we are going to now write a prompt template. PT is equal to prompt template. It will be template is equal to analyze the sentiment of this email. We can provide the email in either positive or negative or neutral only. And the reason I have written only is because I only need this. You can also use the pantic identic format to make sure that this only returns you positive, negative or neutral. As of now, I'm not doing it just to keep things simple. This is done. We have created this. Now, let's chain pt clean meta gpt and finally st. So, let's get the string output parser also. Now let's create a test email. Some sample email. Sample email is equal to email. I love your product. It's amazing and works perfectly. Now let's invoke this. So chain dot invoke I'm going to invoke with the test email and this will return me some result which I'm going to preserve in the variable result. Finally I'm going to print or just look at the result. Let's see what is the issue. string prompt value uh data data is clean is equal to text colon 2200. So it is showing me object is not scriptable which basically mean the problem is over here. The problem is over here. The chain is wrong. We should start with the clean chain then meta then it will go to the prompt. So let's write a pt over here then it will go to my gpt and then st. Now it is fine. So first I will pass this to this clean then meta. So let's run it now. And we have the answer. The sentiment of the email is positive. Now this is working fine but the problem is that as you can see I asked only for positive, negative or neutral. It is giving me the entire statement. If you want to control this you remember we talked about how we can use the pyante library to create or to force the schema and that is something which you have to use over here. So I hope this part is clear to everyone. So that's it for this particular video. So I hope you guys enjoyed and I hope you guys understood uh the concept of runnables and why runnables were introduced. I have not covered runnable branch because we have already done it in the last video. So please go through the last video also and those of you who are directly looking into this video you can check out the previous video where we have talked about the concept of chain where I have already introduced the concept of runnable branch also which is the conditional chain. So if you want to create a conditional chain you can use runnable branch. We have also talked about runnable parallel also. This particular video was specific to introduce all of you the concept of runnables and why langchain community introduced the concept of runnables and I hope I did justice in uh putting up this content together. So that is all about this particular video. Hope to see you guys in the next video with more such amazing content. Till then keep liking, keep learning and I see you in the next video. [music]

Original Description

Runnables are the building blocks that power structured AI workflows in LangChain. In this session, Sumit Shukla, Data Scientist and Educator, explains how LangChain Runnables and LangChain Expression Language (LCEL) are used to compose, chain, and control complex AI workflows in a clean and scalable way. Join FREE Masterclasses by Industry Experts: https://www.scaler.com/events/?utm_source=osocial&utm_medium=youtube&utm_campaign=agentic-ai-video6 This video focuses on building the right mental model for workflows, rather than jumping straight into code. What you will learn in this session: - What LangChain Expression Language (LCEL) is and why it exists - How Runnables help structure AI workflows - Runnable categories including Sequence, Parallel, and Passthrough - How the pipe operator is used to chain execution steps - Writing custom logic using RunnableLambda - Conditional routing using RunnableBranch - Combining Runnables to build complex workflows with LCEL Chapters 1:50 - Introduction to Runnables in LangChain 10:52 - Functions and Use Cases in LangChain 16:30 - Abstract Class or Runnables 22:45 - Runnable Components & Primitives 26:40 - Implementation of Runnable Sequence 35:40 - Creating a Runnable Pass Through 46:50 - Implementation & Uses of Runnable Lambda 55:25 - Conclusion This session is useful for developers and AI practitioners who want to understand how agentic and workflow-based systems are structured in LangChain. Scaler, langchain runnables, lcel langchain, langchain workflows, ai workflows langchain, runnable sequence, runnable parallel, runnable branch, pipe operator langchain, agentic ai workflows ______________________________________________________________________________ About SCALER: A transformative tech school, creating talent with impeccable skills. Upskill and Create Impact. Learn more about Scaler: http://bit.ly/3UPH9b6 📌 Follow us on Social and be a part of an amazing tech community📌 👉 Tweets you cannot afford
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

This video teaches how to use LangChain Runnables and LCEL to structure AI workflows, including creating runnables, chaining them together, and using prompt templates. It also covers how to use LangChain Core and other tools to create complex AI workflows.

Key Takeaways
  1. Create a runnable using the LangChain library
  2. Chain runnables together to create a complex AI workflow
  3. Use prompt templates to create dynamic prompts for LLMs
  4. Use LangChain Core to create a runnable parallel
  5. Create a chain of runnables to combine text from multiple sources
  6. Use a prompt template to combine text from different runnables
  7. Create a parallel chain to generate text one and text two
  8. Use a pass-through chain to return the entire output of both parallel chains
  9. Pass the output to a summary chain to generate a summary of both text one and text two
💡 LangChain Runnables and LCEL provide a powerful way to structure AI workflows and create complex AI systems

Related Reads

Chapters (8)

1:50 Introduction to Runnables in LangChain
10:52 Functions and Use Cases in LangChain
16:30 Abstract Class or Runnables
22:45 Runnable Components & Primitives
26:40 Implementation of Runnable Sequence
35:40 Creating a Runnable Pass Through
46:50 Implementation & Uses of Runnable Lambda
55:25 Conclusion
Up next
/dev/push: An Open Vercel Alternative to Ship Your Apps Quickly
Ian Wootten
Watch →