Unlocking the Power of LLMs: A Hands-On Guide with HuggingFace and LangChain

Ashish Agarwal
3 min readOct 10, 2023

Nov 30, 2022 marked the launch of a pioneering tool in the industry of technology. OpenAI released ChatGPT [Chat-Generative Pre-trained Transformer] for general public. It was a landmark milestone arguably only seen after the launch of Internet in early 90s.

Following chart shows the popularity of ChatGPT on Google Trends over last 12 months

What powers the ChatGPT behind the scenes is a powerful algorithm (i.e. GPT 3.5/4.0) based on Large Language Models (LLMs).

So, what are LLMs

These are models that are trained on massive amounts of text data to learn patterns and entity relationships in the language. LLMs use Deep Learning algorithms to process & understand natural language, perform tasks such as translating, analysing sentiments and generating new text.

Following figure shows the uses of LLMs

In this article we will show sample usage of a text generation LLM.

We would need the following for the hands-on assignment -

  1. Anaconda (for writing Python code in Jupyter notebooks)

2. Python Packages — HuggingFace (open source community that develops and publishes LLM models) , LangChain (open source wrapper framework for writing LLMs with multiple providers like Meta, OpenAI and HuggingFace)

Lets dive-in and get started :-)

Steps

  1. Install Anaconda program based on your OS type

2. Click Launch under Jupyter notebook

3. Create a HuggingFace account and generate the Access Tokens from -

Profile > Settings > Access Tokens

4. Create a new Jupyter notebook — MyFirstLLMApp

Following is the source code of MyFirstLLMApp notebook

!pip install langchain
!pip install huggingface_hub

import os
os.environ["HUGGINGFACEHUB_API_TOKEN"]="<YOUR ACCESS TOKEN>"

from langchain.llms import HuggingFaceHub

llm=HuggingFaceHub(repo_id="google/flan-t5-xxl")

# The LLM takes a prompt as an input and outputs a completion
query1 = "What is the currency of India?"
completion = llm(query1)

print(completion)

query2 = "when is diwali this year?"
completion = llm(query2)

print(completion)

Output is -

The actual name of the LLM model used in this code is — google/flan-t5-xxl.

You may read more about it here.

Note: For some reason the model does not output correct answer to the 2nd query. One probable reason is that t5 model is trained on 11Bn parameters. You may try with other sophisticated models like Meta’s LLaMA 2 which is trained on 70Bn parameters. Another good option is Falcon

That’s All for a 101 article on hands-on LLM.

If you liked my article, consider giving a few claps and follow me on LinkedIn.

--

--