VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/how-to-use-hugging-face-with-langchain-/

⇱ How to use Hugging Face with LangChain ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to use Hugging Face with LangChain ?

Last Updated : 8 Dec, 2025

Hugging Face is an open-source platform that provides tools, datasets, and pre-trained models to build Generative AI applications. We can access a wide variety of open-source models using its API. With the Hugging Face API, we can build applications based on image-to-text, text generation, text-to-image, and even image segmentation. Quite fascinating, right? But the question is, how do we use it? Well, this is where LangChain comes into the picture.

👁 How-to-use-Hugging-Face-with-LangChain-copy
How to use Hugging Face with LangChain ?


LangChain is an open-source framework developed to simplify the development of applications based on LLMs. Using LangChain, we can integrate an LLM with databases, frameworks, and even other LLMs. With it, we can create pipelines for end-to-end Generative AI-based applications. By providing a simple and efficient way to interact with various APIs and databases in real-time, it reduces the complexity of building and deploying projects. Now then, having understood the use of both Hugging Face and LangChain, let's dive into the practical implementation with Python.

Implementation of Hugging Face using LangChain

Model Selection

An important aspect of creating Generative AI applications is choosing the correct model for the desired use case. There are various models with different architectures and sizes. In this example, we are using GPT-2, a causal language model (CLM) developed by OpenAI. GPT-2 is capable of generating coherent text by predicting the next word in a sequence. Although smaller than GPT-3 or GPT-neo models, it is suitable for practice projects and local experimentation.

Code Implementation

We start by importing the necessary libraries:

  • transformers.pipeline: from Hugging Face Transformers, used to create the text-generation pipeline with the GPT-2 model.
  • HuggingFacePipeline: from LangChain Community, used to wrap the Hugging Face pipeline so it can be used as a LangChain LLM object.

We create a local text-generation pipeline using GPT-2.

  • text-generation specifies the type of pipeline (causal language modeling).
  • model="gpt2" selects the GPT-2 model.
  • max_length=100 limits the generated output to 100 tokens.
  • do_sample=True enables sampling instead of greedy decoding, so outputs are more varied.
  • temperature=0.7 controls randomness (higher values make the output more creative).

To use Hugging Face models in LangChain applications, we wrap the pipeline with HuggingFacePipeline. This allows us to use LangChain’s standardized interface like .generate() for text generation..

We define a prompt and generate a response using the LangChain object.

Output:

👁 Screenshot-2024-08-28-002236
Generated output

Conclusion

You've now learned the basics of integrating Hugging Face models with LangChain. This allows you to explore a wide range of models and interact with databases. Once you're comfortable with these basics, you can advance to more complex projects involving text embeddings, transformers, and custom Generative AI pipelines.

Comment