Getting started with LLMs in Hugging Face
Your first step to Large Language Models with Hugging Face.
OpenAI is the popular option for building LLM applications, but if you’re beginning your journey, and are looking for a cost effective alternative, than the Hugging Face ecosystem is something worth exploring.
So, what is Hugging Face?
It’s an open source repository of models, datasets, and tools. It is particularly renowned for its Transformers library, which is designed for natural language processing (NLP) applications.
Apart from the Transformers library, there are also thousands of language models freely available to use on Hugging Face. The API integrates really nicely into LangChain, which is an open-source framework for connecting LLMs, data sources, and other functionalities under a unified syntax.
In this beginner’s guide, you’ll get started with LLMs using Hugging Face.
Steps to access the Hugging Face API token
To follow-along, you’ll first need to create a Hugging Face API token. Creating this token is completely free, and there are no charges for loading models. The simple steps are as follows:
- Sign up for a Hugging Face account at
https://huggingface.co/join - Navigate to
https://huggingface.co/settings/tokens - Select “New token” and copy the key
Once you have the above logistics set-up, you’re ready to get started with LLMs.
Hugging Face models in LangChain!
To begin, install the langchain_community module, which contains components you’ll need to implement the base interfaces defined in LangChain Core. You can read more here.
!pip install langchain_community
from langchain_community.llms import HuggingFaceHubThe next step is to create an object that will store the hugging face token we created in the beginning.
# Set your Hugging Face API token
huggingfacehub_api_token = 'Your Hugging Face API token'Next, we’ll define an LLM using the Falcon-7B instruct model from Hugging Face, which has the ID: 'tiiuae/falcon-7b-instruct'.
llm = HuggingFaceHub(repo_id='tiiuae/falcon-7b-instruct', huggingfacehub_api_token=huggingfacehub_api_token)We have instantiated the llm object, and we’ll pass a text as an input, and then use the llm.invoke() function to predict the words based on the input provided. This is shown in the code below.
input = 'Health is wealth, but is it the order of priority for most of us?'
output = llm.invoke(input)
print(output)The above code will generate the following output:
Health is wealth, but is it the order of priority for most of us? We are all guilty of putting our health on the back burner. We all know that we should be eating healthier, exercising more, and getting enough sleep. But we all have our own priorities and we all have our own ways of prioritizing our lives. I have been working on my health for the past 5 years. I have been working on my health for the past 5 years. I have been working on my
You can see the response from the llm. It’s a generic one and there is repetition as well. So, there is room for improvement, and it’s also a good idea to compare the output using the OpenAI API.
However, that’s something for the future articles. The objective for this blog was to get you started with LLMs using a cost-effective alternative, Hugging Face.
Please feel free to share your thoughts.
If you’re interested in statistics, data science and machine learning, you’ll like these blogs:
- Credit Risk Modelling in Python
- Exploring Credit Risk and IRFS9 Models
- Analyzing Loan Data with Binomial and Poisson Distributions in Python
- Mastering Credit Risk Analysis: A Step-by-Step Guide to Descriptive Statistics in Python
- Introduction to Hypothesis Testing
- Fraud Analytics — Strategies and Approaches
- Understanding Financial Risk Models: A Guide to Credit Risk, Stress Testing, and More
- The What, Why, and How of Generative AI
- Interview-Ready: Top Generative AI Questions You Need to Know
- 10 Movies to Binge-Watch for Data Science and AI Nerds!
- Sentiment Analysis in Python
- Frequently Asked Hypothesis Testing Questions for Data Scientist Interviews
- Frequently Asked Hypothesis Testing Interview Questions for Aspiring Data Scientists (Part 2)
- How to Transition into Data Science from a Non-Technical Background
You can also connect with me on LinkedIn.
Good luck!
#ConversationalAI #Chatbots #OpenAI #Python #LearningJourney #LLM #FinetuningLLMs #PromptEngineering #HuggingFace #LangChain






