
In this video I show you how you can quickly set up an application where you query a domain for a question by setting up a small langchain application with unstructured.io and HNSWLib (for vector storage) Repo: https://github.com/developersdigest/Getting-Started-with-Langchain.JS-and-Unstructured.IO
--- type: transcript date: 2024-01-05 youtube_id: 2B161Dpvyxw --- # Transcript: Getting Started with Langchain.JS and Unstructured.IO in 6 Minutes in this video I'm going to be showing you how you can leverage a lang chain Jaz unstructured IO as well as the open AI apis to create a rag system that will query a domain with a particular question that you have and then generate a response for you I'm going to go through all the ins and outs how to set this up and it should only take us a few minutes to get everything going the first thing that we're going to do is just create a brand new directory if you just go ahead and mpm in it- y you can also use bun init Dy if you're using bun and once that's all set up we're going to go ahead and I'm just going to open up my package Json we're going to install these dependencies here so you can either bun ey all of these dependencies or similarly mpm all these different dependencies once we have that we're going to touch a couple files so if we just go touch index.js and then you can also while you're in here do a EnV and just create our Javascript file as well as our environment variables file we're going to be using two API keys we're going to be using the open AI API so in this example we're just going to be leveraging the GPT 3.5 example you can always Swap this out to gp4 if you'd like but it should really only only cost a couple of pennies to set up and run this example a handful of times in terms of unstructured IO there is a hosted version that you can pay for but I'm going to be showing you how to get started with their free tier so just head to the links within the EnV here I'm going to put the links to this within the description of the video within a repository that you can go ahead and pull down as well if you'd like so the first thing that we're going to do is we're going to import all the dependencies that we're going to be using once we have our environment variables all loaded up next we're going to set up our configuration object this is going to be where we pass in the domain that we want to query as well is the actual question for that domain the next thing that we're going to set up are some Global variables as well as a function that will log out each step that we're on as it iterates through the application so it's going to tell you step by step everything that's going on once you're comfortable with this approach you can go ahead and tear out this function and all the this is really just to get you comfortable with what the application is actually doing and then the performance it's really just going to show you if you run into bottlenecks or things are taking a particularly long time you can go ahead and start to isolate which step is really causing the lag within your application so the next thing that we're going to do is we're going to wrap our application within a main function and the first thing that we're going to do is we're just going to log out that we're starting our main function from there we're going to initialize our open aai embeddings here this is where we're going to be passing in within our openai API key and then we're going to just log out everything so once we have that all logged out we're going to go ahead and perform a simple get request to The Domain so there we're just going to go ahead and get that raw HTML from the page and then we're just going to create a simple caching directory and then we're going to store that file within this directory here then next we're just going to log out that it's been checked or created if it wasn't already created then from there all that we're going to do is we're going to create the path for where we save out the HTML once we have that path we can go ahead and write out our cached HTML file so from there what we're going to do we're going to log out once again next we're going to set up the unstructured loader and what this loader will do is it will take our HTML file we'll send it to the unstructured io API and then it will return a response in Json with a ton of helpful information it will go ahead and parse that document for us and give us a ton of different metadata and tags that we can feed into our Vector storage once we have that set up we're going to go ahead and convert that data we're just going to do a little bit of parsing we're going to include the page contents and the metadata now these are going to be the values that we ultimately feed within our Vector storage next we're going to go ahead and generate our embeddings and create a vector store locally so hnsw lib is way that we can create these Vector stores locally and just save them to our machine then the embeddings process that's going to be querying the open a embeddings API and it's going to return numerical representations of the similarity between different items so if you're not familiar with embeddings I'd encourage you to take a look at other videos on how it actually works but essentially that's how it works in a nutshell so once that's all set up we're going to go ahead and log out again then we're going to set up what is known as a QA chain so we're going to go ahead and declare the model that we want to use you don't necessarily need to use open AI models if you don't want to you could use anthropic you could use coher you could use local models open source models whatever you want so you can go ahead and decide on which model you'd like to use for something like this now once that's declared you can create your retrieval QA chain now that's going to accept the model the vector store as a retriever then this last key within the object return Source documents this is going to be the way that you can return the metadata from the response if you need it so say if you need something like annotations this is the way that you could potentially do that type of thing so then from there we're just going to log out once again then we're finally actually going to run the chain we're going to pass in the query that's within our config so just to remind us our query is what is the Second Story on Hacker News and then we're going to go ahead log out that the execution timing ended and then we're finally going to run our main function so if I just go ahead and run this and I say fun index you'll see that what it does it's going to log out every every single step a lot of them are very quick and happen right after one another what the Ke Chain is doing is if I scroll back to the top here is is embeds this line here so it's going to return a numerical representation of this question and then based on the top results from that comparison it's going to go ahead and return those results once those top results have been returned it's going to go ahead and feed that into GPT 3.5 at that point and then that's how we get our response back so we see that the Second Story on news is titled hard drive LEDs and noisy machines so this is just one way that you can use Lang chain obviously you could wrap this within an Express server something like that if you'd like but otherwise if you find this video useful please like comment share and subscribe and otherwise until the next one
Technical content at the intersection of AI and development. Building with AI agents, Claude Code, and modern dev tools - then showing you exactly how it works.
Weekly deep dives on AI agents, coding tools, and building with LLMs - delivered to your inbox.
Free forever. No spam.
Subscribe FreeNew tutorials, open-source projects, and deep dives on coding agents - delivered weekly.