Get Started with LangChain in Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right in this video I'm going to be showing you how to build out an application where you can have a natural language conversation with a large body of text such as a book the technology that we're going to use so we're going to use node.js Lang chain we're going to use the openai API and without further Ado let's get into it so the first thing that I'm going to do is I'm going to be using this book the creative act by Rick Rubin and part of the reason why I wanted to use this book is because it's recently released so just in the past several months here and just to demonstrate this so if I go over to chat gbt and reference their newest model or any model for that sake and if I ask a question about this book you'll see that chatgpt doesn't have any information on this because it was trained on data prior to 2021. so what I did the first thing that I did is I had an Epub version of the book and I simply converted that EPUB to text so I just use one of these online conversion tools I'm sure there'd be a way you could do it within a node um itself but I found that's just easy enough just drag drop download and then once I had that I put the text file of the text version of the book within my directory here and then the next thing that I'm going to have you do is go over to the openai website we're going to grab an API key so if you don't have an account super simple you don't need a credit card to initially sign up you also get some credits off the bat without needing to tie in a credit card or anything so if you're just trying this out you can go that route and get some free credits to try this out and then once we have that we're going to copy our API key from here we're going to go into our DOT EnV I'm not going to pull it up here but just put it in open AI underscore API underscore key equals and then paste in your key and then once we have that we're going to initialize a couple things so I'll just copy this for a second so you're going to want to run npm and nit Dash y just like that and run that so I've already done that I'm not going to actually run that now once you've initialized it you're going to install a few packages here scroll down here so we're going to install npmi this is the vector Library we're going to install a langchain and Dot EnV so this is a dependency for Lang chain when we're using this so once you have those installed um we'll head over to our index.js so just initialize an index.js if you don't already have one and then we're going to be running through these instructions here so one thing to note I'll also delete this but this is what we're going to be doing in this tutorial so we're going to be saving a vector version locally we're not going to be using pine cone or any database in this I just want to keep it simple and straight to the point but I also want to add that added feature where you're not continually querying and embedding potentially large documents so we'll just sort of quality of life thing cache a local version of it with what we're about to do here okay so if I open this up now the first thing that we're going to do is we're going to install the necessary libraries so we're going to be referencing Lang chain for most of this but then we're also going to be using dot EnV and Fs for what we're going to be using FS for saving things locally reading things locally and then dot NV to reference that environment variable so we're going to load in our environment variable and then right here we're going to set up a few variables just to make this a little bit more reusable so the first variable we're going to have the name of the file if I open it up over here we have the name of our book in this case here we're going to have the question of the book so the question I derived just from looking at the first few pages here in sort of the chapters list there's a chapter called the opposite is true question mark um and I'm going to be sort of making a question you know without actually going to the book seeing what it says based on that So based on the uh the the text file name we're going to be using that to reference the actual text file itself but we're also going to be using this as the variable to create the vector store index and so what that will look like once it runs will be this you have a few Json files and then this dot index file here so I'll just delete this because we're going to run through this just an a second so the first thing that we're going to do is we're going to set up an asynchronous function and I just called it run with embeddings we could probably name it something a bit more appropriate but as I say you know namings one of the harder things in programming so once we have that we're just going to set a new instance and initialize the openai model I'm not going to be passing in any arguments here so different people have different access like three or four depending on what you have requested or what's just available to you from the API then from there we're going to set a variable for our Vector store and before doing anything we're going to check if the vector store path exists so that folder that I just deleted we're going to check if it exists um if it already exists so it doesn't have to run the embeddings each time so to save you on hitting that endpoint and being charged repeatedly especially for big documents so if it exists we're just going to say okay it exists and then we're going to load that into our Vector store variable here so from there we're going to go into our separate conditions so this will run the first time if it doesn't exist we're going to read our text file once we've read it we're going to establish this function where we break up our document into chunks of a thousand and the reason why we want to do that is because the embeddings endpoint on open AI only can handle a certain number of characters or tokens with each request so once we have that we're actually going to split our text into little little chunks and then from there we're actually going to create the new Vector store and reach out to the open AI embeddings endpoint then once that's awaited and returned we're going to save that to our path like we had seen above and then from there we're going to use this retrieval QA chain and then we're going to be passing the model that we referenced here so in this case we're using open Ai and then we're going to also be passing our Vector store so from there we're going to pass in our question that we established up here and actually call the chain and wait for the response so we'll simply in this example just log out the response but we could also you know send this response back through an API or however you want to set this up then from there we're just going to invoke the function so I'll just um also open up my terminal is a little broken there so I'll open up a new terminal and just clear this and I'm just going to run the file so the first time that you run it depending on the size it will take a little bit more time than if it's referencing the locally cached version but as you can see even though this was a 200 Page book it embedded it stored it as a vector and also gave us a response in less than a couple seconds really so you can see here the wisdom of the opposite of true etc etc if I go back to the book I could say um you know let's just take that and if I say tell me about oh and it also copies all that we'll just get rid of that save that and then now when I run it it's going to use that local Vector store instead of actually embedding it again so once you have it cached you're not going to be incurring that initial larger cost of embedding that whole document so hopefully you found this useful if you did please like comment share subscribe if you have any questions just let me know in the comments below and until the next one
Info
Channel: Developers Digest
Views: 11,691
Rating: undefined out of 5
Keywords: LangChain, Node.js, OpenAI, 10-minute crash course, natural language processing, AI chat, beginner's guide, large documents, book conversations, NLP tutorial, AI integration, AI in Node.js, chat experiences, language AI, quick setup, AI mastery, Node.js tutorial, LangChain guide, OpenAI API, book chatbot, AI-powered conversations, language processing, AI breakthrough, AI for books, text analysis, AI chat tutorial, LangChain library, The Creative Act, Rick Rubin
Id: Wxx1KUWJFv4
Channel Id: undefined
Length: 9min 29sec (569 seconds)
Published: Wed Apr 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.