LangChain Tutorial (JS) #3: Output Parsers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's have a look at a very important feature of Lang chain called output parsers with output parsers we are able to control the structure and format of the response being returned from the AI when we invoke our chain we can see the response being returned in this content field and we can see that the content is returned as a string with some funny characters like this new line character over here and even then this is a concatenation between multiple strings what output parts allow us to do is to instruct the chain to take this response from the AI and then reformat it into a very specific way this is important because in production applications you might want to take the output from the AI and then pass it along to another system which will expect the response to be in a specific way enough talk let's have a look at this in our project let's create another file and let's call it output parsers DJs to save a bit of time let's copy the the code from this prompt template file and then move it to output parsers and as a reminder you can find the source code in this get up repository simply change the branch to lesson two and then copy the code from this prompt template file in this video we will cover a few of the most popular output parsers but you can view a full list of parsers in the Lang chain documentation and you should find the parer for your specific use case so let's just go through this code again first we install iate our model and then we create our promt template and then we create a chain using the piping syntax and then lastly we are invoking this chain and simply passing in the input as dog and to test that this is working let's run it in the terminal by calling node and output parsers and in the response we get this AI message object back and in the content field we get a value that contains these new line characters so let's use a parser to convert this to a string the first thing we need to do is to import the output parser so let's type import something from at Lang chain slash core slash output parsers and the first parser we will have a look at is the string output parser now let's instantiate this parser just below the template by typing parser is equal to new string output parser and that's all we have to do for the parser now all we have to do is attach the sparser to the chain and we can do that by adding dot pipe and then passing in the parser this piping syntax might look a bit odd but this is part of the Lang chain expression language and this is just a way to easily attach objects to a chain the way we read this is the output from this object will be passed as input into this object and then the output of this object will be passed into this object as input now let's run the script again now instead of receiving that AI message object back we are now simply getting the string right let's have a look at a few more exciting examples let's say we wanted the model to generate a list of values and then return a JavaScript array as output we can do that using the list parser because we will be having a look at a few parsers in this video I just want to make this code a little bit more readable so let's wrap the code for each parser in a function so I'll just create a new function and let's call this call string output parser and let's simply wrap this code in this function like so then because we need to call a wait let's simply add async to this function module and that will resolve the error and let's also remove this const response and simply replace it with return so what's going to happen is when we call this function it will simply return the result of this chain and then we can console log it down here so let's define response and set that equal to a wait and call string output parser let's just run this again to make sure that it's still working great right let's have a look at that list output parser so let's create a new function called a sync function call list output parser and let's write the code for this so what we want to do here is we want to create a prompt and let's set our prompt equal to chat prompt template from template and for the template let's pass in provide five synonyms separated by commas for the following word and let's add our word placeholder then let's create our output parser and let's set this equal to something that we need to import so just after string output parser let's now import the comma separated list output parser and then we'll set output parser equal to new comma separated output parser like so let's create our Chain by setting the chain equal to prompt do pipe in pipe let's pass in the model and then also add dot pipe and for this pipe we'll attach our output parser all we have to do now is return a we chain doino and for the input we'll pass in a word of happy right so let's quickly have a look at this we will pass in a word and the model will then generate five synonyms separated by commas however the response from the model will be a type string which is not what we want but instead we want the response to be converted into a JavaScript array which should be an object so therefore we are attaching this comma separate ated list output parser which will take the string and convert it to an array let's test this so down here I'm just going to comment out this line let's create response which is equal to A8 call list output parser and let's run this in the terminal and now what we can see in the terminal is that we are getting an array back with the different values and let me show you what would have happened if we did not include the output parser in this instance so in the terminal let's run this again and in the content we can see that we are only receiving a string and not a JavaScript array so let's add this back and hopefully you can already start to see the benefits of using output parsers to control the output now let's move on to another output parser that's extremely important for production use cases and that is the structured output parser with the structured output parser we can convert the response into a JavaScript script object and for situations where you need to pass a Json object to a service or another application this is extremely important and this is super easy to implement so let's have a look at this let's create our function let's call it call structured parser and let's set things up first let's import the structured parser and this parser actually comes from a different package which we can import from Lang chain slash output parsers and this is called the structured output parser so in our function let's first create our prompt and for the prompt let's set it to from template and let's give this a value like extract information from the following phrase and let's add the phrase placeholder so what I want to do in this example is to pass in a phrase like something that contains a name and the age of a person and I want the model to extract the name and the age and then create a Json like object with a name and age property now in order for the model to understand what information to extract and to which properties the information should be assign to we also need to provide some formatting instructions so we can already go ahead and provide this placeholder variable for the format instructions like so and let's also add some text like formatting instructions like so let's also go ahead and create our output parser by creating our variable output power s it set that equal to structured output power Dot and now we have a few options for creating this structure and we will have a look at two of them we will have a look at from names and descriptions and from Zod schema if you have a very simple structure of individual Fields you can simply use from names and descriptions but from Zod schema will allow us to create a structure that contains arrays let's first have a look at this from names and descriptions method and this method takes in an object as input and we can now Define the structure of the object that we want the chain to return in this example we wanted to return the name and here we can provide a description of this field like the name of the person and we also want the age so let's give a description like the age of the person right let's go ahead and create our Chain by assigning chain to prompt. pipe let's pass in the model and let's also pipe in the output parser like so lastly let's return A8 chain dot invoke and for invoke we need to pass two values the phrase let's set it to something like Max is 30 years old and we also need to provide those formatting instructions which we can get from the output parser do getet format instructions great just to recap when we call this chain we will pass in this phrase Max is 30 years old which will be passed into this prompt template then the output parer will take the structure that be defined over here and convert that into formatting instructions which the model will understand and we will then pass those instructions into the prompt template as well and hopefully when we run this the name Max will be assigned to this name property and age will be set to 30 let's try this let's comment out this list output parser let's define response as a wait call structured parser and let's run this in the terminal let's run node output parsers and indeed the model was able to extract that information and the chain was able to convert that response into a JavaScript object and if we have a look at the type of response it should be a JavaScript object which it is this is extremely powerful especially when you want the response of the AI to be passed into different processes in your application if you want a slightly more advanced structure with nested structures and arrays we can use the from Zod schema method this is also really easy to implement and let's have a look at it in order to use Zod we do need to install it so in the terminal let's run npm install Zod now at the top of our code we can import Zod so just below this line let's import Z from Zod like so let's also create the new function for this so just below this this call structured parser function I'll write a new function which is an async function called call Zod output parser and first let's create our prompt which is equal to chat prompt template Dot from template and for this prompt I'm simply going to copy the prompt that we have up here because it will be the same we simply want the model to extract information based on the phrase and some formatting instructions let's also go ahead and assign our output parser to be equal to the structured output parser and on this we will now call a method called from Zod schema and now in from Zod schema we can call Z doob and now within object we can Define the object that we want returned and in this example I'm going to pass in a phrase that contains the name of a me meal as well as some of its ingredients and I want this object to contain firstly the name of the recipe as well as an array of the ingredients let's first set up recipe this is simply a string value so let's call Z do string and in order for the model to understand the meaning of this field we also need to add a description and let's call this name of recipe and this is how we can add individual properties to our object now for ingredients let's call Z but this time it's of type array and the contents of this array will be of type z. string like so and all we have to do now is add the description of this array which is simply ingredients we're almost done all we have to do now is create our Chain by calling prompt. pipe let's add our model and let's also add our output parser to this chain and lastly let's return await chain do invoke and for invoke we need to pass in the phrase and in this case it is something like the ingredients for a spaghetti bolock nise recipe are tomatoes minced beef garlic wine and herbs and lastly let's also pause in the formatting instructions and we can get these from our output parser doget format instructions and this should be all we need to do let's go ahead and test this so let's comment out this let's create our new response which is equal to await call Zod output parser and then let's run this in the terminal by running node output parsers and in the response we are getting a JavaScript object with a property called recipe which contains spaghetti bcn and we have a property called ingredients which is a JavaScript array with all of the ingredients that be listed in our phrase how awesome is that
Info
Channel: Leon van Zyl
Views: 3,726
Rating: undefined out of 5
Keywords: langchain tutorial, langchain, langchain ai, langchain openai, natural language processing, large language models, langchain demo, gpt 4, langchain explained, llm, openai, langchain agent, what is langchain, prompt engineering, chatgpt, langchain chatgpt, train openai with own data, train openai model, langchain prompt, langchain javascript, llms, langchain ai tutorial, langchain own data, Output Parsers, langchain output parser, langchain structured output parser
Id: vvZ4cyxl99Q
Channel Id: undefined
Length: 15min 7sec (907 seconds)
Published: Sun Jan 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.