Hello guys.
So we are going to continue the discussion with respect to Hugging Face and Lang Chin.
Uh, as mentioned, hugging face and Lang Chin, they have combined together like they have working
together and they have actually created a new partner package which is called as hugging face.
Uh lang chin hugging face.
Right.
So in order to show you the entire practical implementation, first of all, what we will do is that
we'll go ahead and install pip install, uh, lang chin underscore hugging face.
Okay.
And before I go ahead, you'll be able to see that I have created this ninth folder right with this
experiment dot ipynb file.
We will be using the same virtual environment to execute it.
So I have selected Python 3.10.
Along with that you'll also be able to see that in the requirement dot txt I have installed Langen underscore
hugging face.
Okay, so this is the library that is basically required if you have not installed it.
So you can go ahead and mention it over here.
And what I will do.
First of all, I'll just go ahead and install this.
So see as soon as it is installed.
And already I've done the installation.
So it is showing requirement already.
Satisfied.
Okay.
Uh if you want to do the installation from requirement dot txt.
So just make sure to update it over here.
And I will just go and open my terminal.
Right.
I will write CD dot dot.
Okay I'll go back to my file where the requirement dot txt is present and I'll just go ahead and write
pip install minus our requirement dot txt.
Okay.
So once I do this all the installation will start taking place.
So please make sure that you do this particular work from your side okay.
But anyhow, I am also making sure to show you that how from the Jupyter notebook only you will be able
to do it.
Okay, one more thing, uh, that we are going to install is something called as Huggingface hub.
Right.
So I will also go ahead and do this and I'll write hugging face underscore hub okay.
And we'll also check whether it is available in the requirement dot txt or not.
So here you can see it is not available.
So I will go ahead and install this also.
So let me quickly go ahead and install this.
And this will also be important with respect to the requirement.
So here I will talk about why we will be using this.
But let's go ahead and do the installation okay.
So here also you can see the requirement is already satisfied.
Uh and I've done the installation now why hugging Face Hub will be there because this will be very much
handy when we have to do a API call.
Okay.
Like how do we do an API call?
Similarly we can use hugging face.
Uh, we can use this hugging face hub to do the API call for all the models that are available in the
hugging face.
Um, now, one thing that you really need to make sure that hugging face also has a paid, paid, uh,
plan, right?
So there you can also create your own, uh, endpoint address, and you can actually start with that.
I'll show you that.
But I'll show you just one example how you can actually do it.
But, uh, again, I don't want you all to put your card.
And unless and until you're working in a company for the same purpose.
Okay.
So here, uh, first of all, what we are going to do is that we are going to import OS.
So I'll go ahead and write import OS.
Along with that I will say from dot env I'll be importing my load underscore dot env okay.
And then I will go ahead and initialize load underscore dot env.
The reason is very simple because um, if you remember in my dot env file there will be something called
as off token.
So we will be using this specific token in order to do the API call Okay, so here I have made sure
that I will call all my environment variables okay.
Now the next thing is that the first way of probably calling your, uh, any models that is available
in the hugging face and I think, uh, uh, all the models should be accessible.
Uh, I've tried multiple models, and I could find that, okay, it was accessible, but there may be
scenarios that some of the model may not be accessible also through the endpoint.
Okay.
So let's consider one other example which is called by hugging face endpoint.
Now how to access hugging face models with the API.
If you want to access any hugging face model with the API, then you specifically require this hugging
face endpoint.
It is just like how you created grok API key and you are communicating with the grok model.
Similarly, if you just have an hugging face API, you will be able to communicate with all the models
over there.
So there are two ways to use this class.
You can specify the model with the repo ID parameter.
Then those endpoint.
You use the serverless API.
So they will they don't need to have any server.
Also, they'll use a complete serverless API, which is particularly beneficial to people using Pro
accounts or Enterprise Hub.
Still, regular users can already have an access to fair amount of requests by connecting with the Azure
token in the environment right where they are executing the code.
Now see, usually the serverless API is given to people who are having Huggingface Pro account and enterprise
hub, right?
And for the people who just have a free account for them, also some fair amount of request is basically
given.
You cannot go ahead and hit uh, 1000 request at a time, right?
For the people who are having pro and enterprise hub, they can actually go ahead and do that 1000 request
they want.
Okay.
So here what we will do is that I will try to show you how this hugging face endpoint works.
So for this I will go ahead and right from long chain underscore hugging face okay.
Hugging face I'm going to import hugging face.
See there are so many options.
Hugging face endpoint hugging face embedding hugging face pipeline.
Okay so I will talk about each and every thing okay.
Okay.
Um, and then I'll see you that which will be the best one if you are just doing it in a local machine.
So from Langston underscore hugging face import hugging face endpoint.
Um, so here what we will do is that we will go ahead and use one of the model.
Okay.
Let's see some of the models that we can actually go ahead and use it okay.
So I'll go back to my hugging face okay models let's say I want to go ahead and you can directly search
the model name also if you want.
Let's say I want to go ahead and do Mr..
All okay.
Uh Mr..
All seven b okay.
Let's go ahead and do this.
Okay.
Mr..
All seven B instruct.
So I am going to use this specific model.
Right.
So now here all I have to do is that in I'll just create a variable which is called as repo underscore
ID.
And I'll initialize this particular model name okay.
So this is the path of the model that we are specifically going to use.
Okay.
Now if I want to call this LM model.
Now if you want to know more about this model you can go ahead and see this this Mr..
Seven B instruct large language model is a instruct fine tuned version of Mr..
Seven B okay.
It has the following changes compared to this this this.
It recommends this this.
And what kind of task you will be able to do.
Text generation.
See.
Uh hey my name is Clara.
How are you?
Hey, Clara.
I'm just a program.
Uh, tell me about yourself.
Okay, so it is a text generation LM model.
So if I go ahead and click on send, I should be able to get the answer right.
See, as a computer programmer, I don't have any personal expense.
So if I really want to just use this specific model, uh, through this hugging face, uh, end point,
all I have to do is that get give this repo ID, and then we will go ahead and create our LM.
And here I will call this Huggingface endpoint.
I will initialize it.
First of all I need to give my repo id as my parameter.
So let me just go ahead and write repo ID is equal to repo id okay.
The next thing that I will be saying that hey there will we will set some max length parameter for the
tokens.
Let's say I'm going to set it to 150 okay.
Now once I set it to 150 next I will go ahead and set my temperature.
Like, how do I set for other.
Uh, other other LM models like this.
And finally I will give my token.
And now this token will be very important and will play a very important role.
Now in the ENB, I have actually created a token which is called as TF underscore token.
Right.
And how do I create this token?
I have to just go over here, go to the settings button okay.
In the settings button I will be having this particular access tokens right.
So I will go ahead and take this access token.
You can go ahead and create a new token and you can actually use it right.
So we will specifically be using this particular token.
So here I will have pasted this same token over here.
I will just go ahead and click on this okay.
And go to my page.
And here I have to call that particular token.
So for that I will be using OS dot get env right and I'll paste it over here.
So this basically becomes my LM model.
See now I have downloaded it right.
Not downloaded I'm accessing that particular API.
Right.
So here it says that token has not been saved to the grid credential path.
So and so now it has been saved to my and it is showing login successful.
So finally once the API is authenticated the API key is authenticated.
Then only will be able to do the access.
So finally here you can see hugging face endpoint repo ID Mr..
All this this this is there.
Okay.
Now the next thing what we are basically going to do, just use this LM and how we usually invoke with
the help of hugging face, we will go ahead and write lm dot invoke.
And let's say that I ask a question what is machine learning okay.
And I go ahead and execute it.
So if I just go ahead and ask LM dot invoke what is machine learning now that is going to interact with
the Mistral that is available in the hugging face, right.
Um, by using this hugging face endpoint.
Right.
Similarly, I can go ahead and ask LM dot invoke.
And here let me just go ahead and write what is generative AI.
Okay.
I'm just going to ask another question.
What is generative AI?
done?
Now?
If I go ahead and execute this again, I'll be able to see what is the response that I'm getting.
Generative AI refers to the type of artificial intelligence.
All these things are there.
Let's try with other model.
You may be thinking Krish, how many models will be accessible if you are lucky?
I have been lucky all the time and I was able to see multiple models.
Let's see some more models.
Okay, so I'll go and select models.
Let's go ahead with Google Gamma two okay.
I don't know whether this is a complete new model.
And it has recently come into uh it has been announced by Google.
Okay.
I'm recording today at seven seven 2024.
So um, let's see whether we will be able to access this model or not.
So here, instead of writing this repo ID, let me do let me copy this entirely.
Paste it over here.
Okay.
Now I will just go ahead and call this specific model.
And this model needs to be just put up over here itself.
Okay.
Now here let me just go ahead and execute it.
So login successful Google gamma model is also accessible.
That's amazing.
Now let me just go ahead and search for LM dot invoke and let me write what is machine learning okay.
And I will just go ahead and execute it.
So here I'm getting an error saying that the model okay.
It is too large to be loaded automatically.
So it is not we are not able to access it.
So for this we need to create a dedicated endpoint okay.
And uh there are scenarios where you cannot use some of the models.
Right.
These are some of the disadvantages over here.
And obviously the size is very huge.
So you are not able to upload it okay.
Uh the, the uh huggingface default spaces are only not able to upload it.
Now, let me do one thing.
Let me just use the same LM model over here.
So this will basically be my LM model.
And I will show you with the help of hugging face endpoint, can we create a rag application kind of
thing where we can integrate with, um, where we can also integrate with prompt template.
Right.
So now this is my LM model.
Now what I will do I will quickly go ahead and write from long chain import prompt template.
Along with that I'll also import lm chain.
Right.
We have discussed already about this.
Let's say uh I will just go ahead and create a template.
I'll say, hey, um, let's make a simple question over here, something like this.
Okay.
I'll say, hey, this is my question.
Okay.
Question.
And here I'm basically going to write my answer.
My answer is like let's think step by step.
I'm just adding this prompt okay.
Just to make it a little bit curious okay.
So this is basically becomes my prompt.
Now I'll go ahead and create my prompt templates.
So I will just go ahead and define this.
And I will go ahead and write my template.
And then finally I will also go ahead with input variables.
So input variables is nothing.
But it is your question okay.
And then I can go ahead and just display this prompt prompt okay.
So once I execute it.
So here you can see input variable is question template.
This this question is this answer.
Let's think let's think step by step okay.
Perfect.
So this basically becomes my prompt okay.
Now here uh I have to give question as an input.
Right.
So first of all what I will do I will go ahead and create my chain.
So you know how to create a chain right.
LM underscore chain.
Let's see.
And I will be initializing to my LM chain.
And here I will write LM is equal to LM.
And finally prompt is equal to.
It's nothing but prompt okay.
Whatever prompt template we have defined.
Now if I just go ahead and write lm dot invoke.
And inside this I'll give the I'll give my question okay.
Now instead of giving this question I will write something like this.
Uh question.
Um.
Mhm.
Question.
Colon.
What is machine learning?
Okay.
I'll execute it.
I'm getting an error.
Class dictionary must be a prompt value or list of.
Okay I should not be giving in the form of dictionary.
Instead I can just directly go ahead and write this particular question.
Okay, so the question will be, um, what is the uh.
Or I'll just write who won the World Cup.
Who won the Cricket World Cup?
Cricket World Cup 20 2011 okay, I'm just seeing the previous information.
So once I executed it, start thinking the question along with that what will be the text?
So here you can see the 2011 was the 10th World Cup which was held in Indian subcontinent for February
9th.
To this India on 2nd April, the final was played in Wankhede Stadium.
The tournament was won by India who defeated Sri Lanka by six wickets.
All this information is nicely, completely given over here, Right.
So this is good enough right now.
You can go ahead and try whichever models that you want, but always make sure that that model is accessible.
Right.
At least it should be accessible.
Uh, yeah.
Uh, this was uh, mostly about the hugging face, uh, hugging face endpoint.
Again, there are ways.
There is.
Let me just go through this particular way and show it to you.
Uh, but what I feel is that by using hugging face endpoint, you will be able to do most of your task.
Right?
So if I go back to the documentation over here, you'll also be able to see I've already shown you how
to properly load any, uh, embedding techniques.
Also, you can also use this specific embedding.
Right.
So in order to use this you can also use this hugging face embedding if you want right from Langston
underscore community dot embeddings and all.
You can probably use it.
right.
And similarly huggingface embeddings also.
So here you can see VGG models are hugging face are the best open source model BG model is created by
Beijing Academy of this.
It's a private non uh non-private organization engaged in AI research and development.
You can also go ahead and see some examples with respect to this and executed.
Let's see some examples over here.
So I will just go ahead and click this over here.
See.
So easy it is.
That's it.
How you basically call this hugging face embeddings right from Lang chain underscore community.
You just give the model name.
Let's execute this Lang chain documentation is really powerful.
You should definitely know how to probably read all these things.
So here I will go ahead and copy and paste it.
And let me just go ahead and execute this also.
So it will basically creates a 384 embedding.
Right.
So if I just go and search for this one this is getting executed.
Let's see.
Okay.
Uh, this has got executed okay.
Now if I go ahead and execute this.
So this is my embedding for this particular text.
Right.
So this is also an open source embedding which you can actually use it right in your projects.
We have used some other embedding over there.
Right.
If you probably go ahead and see right what all embedding techniques we had actually used.
So over here okay.
Long chain embeddings and hugging face embedding.
Right.
We use something called as all mini lm s l6 v2.
Right.
So hugging face sentence transformer is the python for state of art.
We have seen this.
And here also I have actually shown you right with respect to the embedding.
Now, uh, in hugging face, uh, there is also very important one library which I will also talk about
it, which is called as chatting hugging face.
Now here you will be able to see that see in hugging face you can create your own endpoint URL, right.
So in order to create an endpoint URL, what you have to do is that just go ahead and search for creating
an endpoint in hugging face, okay.
Once you do this, you just go ahead and click on this inference endpoint.
Now here uh with respect to this inference endpoint right.
Let's say it says that deploy a first model.
If you click over here okay you'll be able to see that you can create your endpoint address, but at
the end of the day you need to put your credit card and all.
So that is the reason why I'm not showing.
You see right now if I just go ahead and click on endpoint, is this our serverless endpoint which I
was able to access it I was able to access version 0.2, version 0.3, version 0.3.
Right.
So many number of requests I did.
But if you want a dedicated endpoint wherein you will be able to access everything.
So for that some charges will definitely be there.
Right?
So for that you need to probably go ahead and add your credit card.
Right.
You have to probably use this for paid services okay.
But if you are interested just go ahead and use this.
But I would again suggest unless and until you're working in a company, uh, don't put your money over
here because other than that, hugging face endpoint actually helps you to probably work with almost
most of the LM models that are available.
Other than that, you can also have grok API that you can use it.
So I hope, uh, you are able to understand this.
Uh, and now you can just understand how easy it really becomes to work with this right now.
In my next video, I will take up any one example, and I will try to implement an end to end project
with the hugging face endpoint itself.
Right?
So yes, uh, this was it from my side.
I hope you liked this particular video.
I will see you all in the next video.
Thank you.