avatarVikash Singh

Summary

The web content introduces beginners to getting started with Large Language Models (LLMs) using the Hugging Face ecosystem, particularly its Transformers library, as a cost-effective alternative to OpenAI.

Abstract

The article serves as a beginner's guide to utilizing Hugging Face for accessing and working with LLMs. It highlights the open-source nature of Hugging Face, which offers a vast repository of models, datasets, and tools, including the renowned Transformers library for NLP. The guide outlines the steps to obtain a free Hugging Face API token, which is essential for accessing the models, and demonstrates how to integrate the Hugging Face models into LangChain for building LLM applications. It provides code snippets for installing the necessary module, setting up the API token, and using the Falcon-7B instruct model to generate text. The article acknowledges the potential for output improvement and suggests a future comparison with the OpenAI API. It encourages readers to engage with the content and offers links to related blogs on data science and machine learning for further exploration.

Opinions

  • The author suggests that Hugging Face is a viable and cost-effective alternative to OpenAI for those beginning their journey with LLMs.
  • The article posits that the integration of Hugging Face models with LangChain simplifies the process of building LLM applications.
  • While demonstrating the use of the Falcon-7B instruct model, the author notes that the initial output may be generic and suggests there is room for improvement.
  • The author expresses an interest in future articles that might compare the performance of Hugging Face with OpenAI's API.
  • The article encourages reader interaction by inviting them to share their thoughts and connect on LinkedIn.
  • The author indicates a preference for open-source tools and communities, emphasizing the collaborative nature of the Hugging Face ecosystem.

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:

  1. Sign up for a Hugging Face account at https://huggingface.co/join
  2. Navigate to https://huggingface.co/settings/tokens
  3. 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 HuggingFaceHub

The 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:

  1. Credit Risk Modelling in Python
  2. Exploring Credit Risk and IRFS9 Models
  3. Analyzing Loan Data with Binomial and Poisson Distributions in Python
  4. Mastering Credit Risk Analysis: A Step-by-Step Guide to Descriptive Statistics in Python
  5. Introduction to Hypothesis Testing
  6. Fraud Analytics — Strategies and Approaches
  7. Understanding Financial Risk Models: A Guide to Credit Risk, Stress Testing, and More
  8. The What, Why, and How of Generative AI
  9. Interview-Ready: Top Generative AI Questions You Need to Know
  10. 10 Movies to Binge-Watch for Data Science and AI Nerds!
  11. Sentiment Analysis in Python
  12. Frequently Asked Hypothesis Testing Questions for Data Scientist Interviews
  13. Frequently Asked Hypothesis Testing Interview Questions for Aspiring Data Scientists (Part 2)
  14. 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

Hugging Face
Large Language Models
OpenAI
Llm
Artificial Intelligence
Recommended from ReadMedium