ADaSci Banner 2024

How to Integrate LangChain and Hugging Face for Open-Source LLM Deployment?

Discover how the langchain-huggingface package enhances NLP by integrating Hugging Face models with LangChain's framework.

In the realm of Natural Language Processing (NLP), Hugging Face and LangChain have made significant individual contributions. Hugging Face has earned renown for its extensive library of pre-trained models, establishing itself as a cornerstone within the NLP community. On the other hand, LangChain has garnered recognition for its practical framework tailored for building and deploying language model-driven applications. Despite their independent impacts on NLP advancement, these two entities have joined forces to unveil the langchain-huggingface package. This package simplifies the utilization of Hugging Face’s models within LangChain’s versatile ecosystem. In this article, we will delve into the langchain-huggingface package, explore its functionalities and applications, and implement it to achieve different tasks. 

Table of Contents

  1. Understanding LangChain-HuggingFace
  2. Key Features of LangChain-HuggingFace
  3. Use Cases and Applications
  4. Integration, Set Up and Uses

Let us now understand the use cases of LangChain-HuggingFace and implement this package:

Understanding LangChain-HuggingFace

The langchain-huggingface package integrates smoothly with LangChain, making it easy and efficient to use Hugging Face models within LangChain’s ecosystem. This collaboration is more than just combining technologies; it reflects a shared dedication to maintaining and continually enhancing this integration.

Key Features of LangChain-HuggingFace

By integrating these two forces, langchain_huggingface offers several advantages:

Easy Integration

It simplifies our setup by easily leveraging Hugging Face’s vast collection of pre-trained models directly within LangChain workflows.

Enhanced Functionality

It enhances LangChain’s advanced linguistic processing capabilities with Hugging Face’s models, improving tokenization, lemmatization, and a wide range of other NLP tasks.

Streamlined Pipelines

It builds optimized NLP pipelines within LangChain to handle tasks like text classification and machine translation seamlessly.

Deployment Flexibility

It makes it easy to deploy our NLP models within the LangChain framework, opening up diverse application possibilities.

Use Cases and Applications

The combination of the two strong tools of NLP has enhanced its use cases and can be applied in different fields. 


This integration helps develop a customer service chatbot that can understand and respond to users instantly. Hugging Face identifies the user’s query purpose, LangChain retrieves useful information, and Hugging Face’s text generation model creates the chatbot’s responses.

Content Creation and Summarization

This partner package can be used for content development and writing synopsis. Using the Hugging Face model we can generate news articles or summarize an article based on given prompts. LangChain workflow is used to review and edit the generated content.

Extended Functionality

LangChain combined with Hugging Face can be used for advanced language processing methods. This improves understanding of language and makes it easier to complete NLP tasks.

Sentiment Analysis

Sentiment analysis helps understand how people feel about something. This integration lets you create more accurate sentiment analysis tools by using Hugging Face’s specialized models along with LangChain’s language processing skills.

Integration, Set Up and Uses

Using the HuggingFace and LangChain integrated package, we can enhance the working of both. HuggingFace provides us with many models and LangChain helps in understanding the language better. Integrating Hugging Face with Langchain involves leveraging the strengths of both platforms through streamlined communication between their respective APIs.

Now, let us see how we can utilize this package. 

To begin with, install all the important packages and import all the libraries.

%pip install langchain openai tiktoken transformers accelerate cohere --quiet langchain-community 
import os
from getpass import getpass
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper
from langchain_core.prompts import PromptTemplate
from langchain import HuggingFaceHub

Next, we have to call the HuggingFace token to our environment. To get the hugging face token, we can just go to the hugging face website, go to settings, and from there take the token. If you are facing an issue with importing the token or invalid token issues, then we have to restart the kernel.

os.environ["HUGGINGFACEHUB_API_TOKEN"] = '*****'

Now, we have everything that we need to use this package. To begin with, we will first see the summarization task.

Here, I have generated a small story using GPT. We will use the model and summarizing task of Hugging Face and LangChain to understand the context and produce a proper summary.

summarizer = HuggingFaceHub(
   model_kwargs={"temperature":0, "max_length":180}
def summarize(llm, text) -> str:
   return llm(f"Summarize this: {text}!")

summarize(summarizer, story)

Next, we will use LangChain-HuggingFace to answer a query. Here we will use the zephyr-7b-beta model from HuggingFace using its repo and generate responses using queries to this model. 

llm = HuggingFaceHub(
   model_kwargs={"temperature": 0.5, "max_length": 64,"max_new_tokens":512}

query = "Who is the actor that played the role of Harry in Harry Potter?"

prompt = f"""
You are an AI assistant that follows instruction extremely well.
Please be truthful and give direct answers

response = llm.predict(prompt)

Next, we will also see how we can use this package to generate an image. 

We are using LLM’s LLMChain to create a chain. This will have the HuggingFace Model and the prompt. 

llm = HuggingFaceHub(
   model_kwargs={"temperature": 0.5, "max_length": 64,"max_new_tokens":512}

prompt = PromptTemplate(
   template="Generate a detailed prompt to generate an image based on the following description: {image_desc}",
chain = LLMChain(llm=llm, prompt=prompt)

Next, we will use DALL-E to generate an image. This image is generated by using both Hugging Face and LangChain.

image_url = DallEAPIWrapper().run("gentleman in a peaky blinders setting having cigar in mouth looking up, ultra-realistic, photo-realism, black and white shade"))

Thus, by combining HuggingFace and LangChain, we have easily generated a summary, answered a query, and generated an image. 

HuggingFace and LangChain together through the langchain-huggingface package have several advantages that make it superior to using these platforms individually. The advantages include double capability, easy integration, and vast applications.


In conclusion, the langchain-huggingface package is a significant step in the world of NLP. With the combined strength, properties, and efficiency of these powerful tools, developers and researchers gain a very friendly and easy-to-use toolkit. With this integration, the future of NLP looks brighter, offering exciting possibilities for communication and information processing across various domains.


  1. Link to Code
  2. LangChain-HuggingFace

Learn more about LangChain and its Hands-On. Join the following course.

Picture of Shreepradha Hegde

Shreepradha Hegde

Shreepradha is an accomplished Associate Lead Consultant at AIM, showcasing expertise in AI and data science, specifically Generative AI. With a wealth of experience, she has consistently demonstrated exceptional skills in leveraging advanced technologies to drive innovation and insightful solutions. Shreepradha's dedication and strategic mindset have made her a valuable asset in the ever-evolving landscape of artificial intelligence and data science.

The Chartered Data Scientist Designation

Achieve the highest distinction in the data science profession.

Elevate Your Team's AI Skills with our Proven Training Programs

Strengthen Critical AI Skills with Trusted Generative AI Training by Association of Data Scientists.

Our Accreditations

Get global recognition for AI skills

Chartered Data Scientist (CDS™)

The highest distinction in the data science profession. Not just earn a charter, but use it as a designation.

Certified Data Scientist - Associate Level

Global recognition of data science skills at the beginner level.

Certified Generative AI Engineer

An upskilling-linked certification initiative designed to recognize talent in generative AI and large language models

Join thousands of members and receive all benefits.

Become Our Member

We offer both Individual & Institutional Membership.