Contrast
< Back to Blog
Original link:

https://youtu.be/aywZrzNaKjs

2023-07-24 06:50:32

LangChain Explained in 13 Minutes _ QuickStart Tutorial for Beginners

video content Image generated by Wilowrid

LANG chain .

What is it ?

Why should you use it ?

And how does it work ?

Let's have a look la chain is an open source framework that allows developers working with A I to combine large language models like GP T four with external sources of computation and data .

The framework is currently offered as a Python or a javascript package typescript .

To be specific in this video , we're going to start unpacking the Python framework and we're going to see why the popularity of the framework is exploding right now , especially after the introduction of GP T four in March 2023 to understand what need LA chain fills .

Let's have a look at a practical example .

So by now , we all know that chat or G BT four has an impressive general knowledge .

We can ask it about almost anything and we will get a pretty good answer .

video content Image generated by Wilowrid

Suppose you want to know something specifically from your own data , your own document , it could be a book , a PDF file , a database with proprietary information le chain allows you to connect a large language model like G BT four to your own sources of data .

And we're not talking about pasting a snippet of a text document into the chat tt prompt .

We're talking about referencing an entire database filled with your own data .

And not only that , once you get the information you need , you can have la chain help you take the action you want to take for instance , send an email with some specific information .

And the way you do that is by taking the document , you want your language model to reference and then you slice it up into smaller chunks and you store those chunks in a vector database .

The chunks are stored as embeddings , meaning they are vector representations of the text .

video content Image generated by Wilowrid

This allows you to build language model applications that follow a general pipeline .

A user asks an initial question .

This question is then sent to the language model and a vector representation of that question is used to do a similarity search in the vector database .

This allows us to fetch the relevant chunks of information from the vector database and feed that to the language model as well .

Now , the language model has both the initial question and the relevant information from the vector database and is therefore capable of providing an answer or taking action .

And lain helps build applications that follow a pipeline like this .

And these applications are both data aware .

We can reference our own data in a Victor store and they are agentic , they can take actions and not only provide answers to questions .

And these two capabilities open up for an infinite number of practical use cases .

video content Image generated by Wilowrid

Anything involving personal assistance will be huge .

You can have a large language model , book flights , transfer , money pay taxes .

Now imagine the implications for studying and learning new things .

You can have a large language model , reference an entire syllabus and help you learn the material as fast as possible , coding data analysis , data science is all gonna be affected by this .

One of the applications that I'm most excited about is the ability to connect large language models to existing company data such as customer data , marketing data and so on .

I think we're going to see an exponential progress in data analytics and data science .

Our ability to connect the large language models to advanced API S such as metas API or Google's API is really gonna gonna make things take off .

So the main value proposition of lang chain can be divided into three main concepts .

video content Image generated by Wilowrid

We have the LLM wrappers that allows us to connect to large language models like G BT four or the ones from hacking face prompt templates allows us to avoid having to hard code text which is the input to the LL MS .

Then we have indexes that allows us to extract relevant information for the LL MS .

The chains allows us to combine multiple components together to solve a specific task and build an entire LLM application .

And finally , we have the agents that allow the LLN to interact with external API S .

There's a lot to unpack in lang chain and new stuff is being added every day .

But on a high level , this is what the framework looks like .

We have models or wrappers around models , we have proms , we have chains , we have the embeddings and vector stores which are the indexes and then we have the agents .

So what I'm gonna do now is I'm gonna start unpacking each of these elements by writing code .

video content Image generated by Wilowrid

And in this video , I'm gonna keep it high level just to get an overview of the framework and a feel for the different elements .

First thing we're going to do is we're going to pip install three libraries .

We're going to need Python dot N to manage the environment file with the passports .

We're going to install link chain and we're going to install the pine cone .

Client Pine cone is going to be the vector store we're going to be using in this video in the environment file .

We need the open A I API key .

We need the pine cone environment and we need the pine cone API key .

Once you have signed up for a pine cone account , it's free the API keys and the environment name is easy to find .

Same thing is true for open .

May I just go to platform 0.0 may I dot com slash account slash API keys ?

Let's get started .

So when you have the keys in an environment file .

All you have to do is just load that in and find that in to get the keys .

And now we're ready to go .

video content Image generated by Wilowrid

So we're going to start off with the LL MS or the wrappers around the LL MS .

Then I'm going to import the open A I wrapper and I'm going to instantiate the text davinci double three completion model and ask it to explain what a large language model is .

And this is very similar to when you call the open A I API directly .

Next , we're gonna move over to the chat model .

So G BT 3.5 and G BT four are chat models .

And in order to interact with the chat model through lach chain , we're going to import a schema consisting of three parts an A I message , a human message and a system message .

And then we're going to import chat or A I .

The system message is what you use to configure the system when you use a model and the human message is the user message .

To use the chat model , you combine the system message and the human message in a list and then you use that as an input to the chat model .

Here , I'm using GP T 3.5 turbo .

video content Image generated by Wilowrid

You could have used GP T four .

I'm not using that because the open A I service is a little bit limited at the moment .

So this works no problem .

Let's move to the next concept which is prom templates .

So prompts are what we are going to send to our language model .

But most of the time these prompts are not going to be static , they're gonna be dynamic , they're going to be used in an application .

And to do that lach chain has something called prompt templates .

And what that allows us to do is to take a piece of text and inject a user input into that text .

And we can then format the prompt with the user input and feed that to the language model .

So this is the most basic example , but it allows us to dynamically change the prompt with the user input .

video content Image generated by Wilowrid

The third concept we want to have a look at is the concept of a chain .

A chain takes a language model and a prompt template and combines them into an interface that takes an input from the user and outputs an answer from the language model sort of like a composite function where the inner function is the prompt template .

And the outer function is the language model .

We can also build sequential chains where we have one chain returning an output and then a second chain taking the output from the first chain as an input .

So here we have the first chain that takes a machine learning concept and gives us a brief explanation of that concept .

video content Image generated by Wilowrid

The second chain then takes the description of the first concept and explains it to me , like I'm five years old , then we simply combine the two chains , the first chain called chain and then the second chain called chain two into an overall chain and run that chain .

And we see that the overall chain returns both the first description of the concept and the explain it to me like I'm five explanation of the concept .

All right , let's move on to embeddings and vector stores .

But before we do that , let me just change the , explain it to me like I'm five prompt so that we get a few more words .

We're gonna go with 500 words .

All right .

video content Image generated by Wilowrid

So this is a slightly longer explanation for a five-year-old .

Now , what I'm gonna do is I'm going to take this text and I'm going to split it into chunks because we want to store it in a vector store in pine cone and Lain has a text bitter tool for that .

So I'm going to import recursive character text splitter .

And then I'm going to spit the text into chunks .

Like we talked about in the beginning of the video , we can extract the plain text of the individual elements of the list with page content .

And what we wanna do now is we want to turn this into an embedding , which is just a vector representation of this text .

And we can use a I's embedding model .

A with a I's model .

video content Image generated by Wilowrid

We can call embed query on the raw text that we just extracted from the chunks of the document .

And then we get the vector representation of that text or the embedding .

Now we're going to check the chunks of the explanation document and we're going to store the vector representations in pinecone .

So we'll import the pine cone Python client and we'll import Pine cone from lang chain vector stores and we initiate the Pine cone client with the key and the environment that we have in the environment file .

Then we take the variable texts which consists of all the chunks of data we want to store , we take the embeddings bottle and we take an index name and we load those chunks of the embeddings to pine cone .

And once we have the vector stored in pine cone , we can ask questions about the data stored , what is magical about an auto encoder .

video content Image generated by Wilowrid

And then we can do a similar research in pine cone to get the answer or to extract all the relevant chunks .

If we head over to pine cone , we can see that the index is here .

We can click on it and inspect it , check the index info .

We have a total of 13 vectors in the vector store .

All right .

So the last thing we're gonna do is we're gonna have a brief look at the concept of an agent .

Now , if you head over to open A I chat GT plugins page , you can see that they're showcasing a Python code interpreter .

Now we can actually do something similar in LA chain .

So here I'm importing the create Python agent as well as the Python Rale tool and the Python Rale from NACH chain .

video content Image generated by Wilowrid

Then we instantiate a Python agent executor using an open A I language model .

And this allows us to having the language model run Python code .

So here I want to find the roots of a quadratic function and we see that the agent executor is using numpy roots to find the roots of this quadratic function .

All right .

So this video was meant to give you a brief introduction to the core concepts of LANG chain .

If you want to follow along for a deep dive into the concepts hit , subscribe .

Thanks for watching .

Partnership

Attention YouTube vloggers and media companies!
Are you looking for a way to reach a wider audience and get more views on your videos?
Our innovative video to text transcribing service can help you do just that.
We provide accurate transcriptions of your videos along with visual content that will help you attract new viewers and keep them engaged. Plus, our data analytics and ad campaign tools can help you monetize your content and maximize your revenue.
Let's partner up and take your video content to the next level!
Contact us today to learn more.