Skip to content

How to use hugging face models in langchain ? – A detailed guide

🧠 How to Use Hugging Face Models in LangChain (2025 Guide)

Are you looking to use Hugging Face models in LangChain for your next AI application? Whether you’re building chatbots, document Q&A systems, or LLM-powered workflows, this complete guide will walk you through the most powerful ways to integrate Hugging Face models with LangChain.

πŸ” What is LangChain?

LangChain is a powerful framework designed to build applications powered by Large Language Models (LLMs). It allows developers to combine LLMs with memory, tools, APIs, and documents.

πŸ€– Why Use Hugging Face with LangChain?

  • Text generation
  • Embeddings
  • Summarization
  • Question answering
  • Translation

LangChain provides simple wrappers to use these modelsβ€”whether from the Hugging Face Hub or locally.

πŸš€ Getting Started: Installation

pip install langchain transformers huggingface_hub
pip install accelerate

πŸ’‘ 1. Using langchain-huggingface Integration

  • This code snippet explains how we can integrate huggingface into langchain. You need to import langchain.llms and huggingFacehub
  • Make sure you use repo_id=”gpt2″ – it is the model
  • set the termperature and max length
  • Print the output (this will demonstrate langchain-huggingace integration
from langchain.llms import HuggingFaceHub

llm = HuggingFaceHub(
    repo_id="gpt2",
    model_kwargs={"temperature": 0.7, "max_length": 100}
)

print(llm("What is LangChain?"))

πŸ’‘ 2. langchain-huggingface example – With Local Model

  • This section tells you how you can use langchain-huggingace with local models as well.
  • You need to import Autotokenizer, AutoModelForCausalLM, pipeline from transformers package
  • Below is the structure of code snipper to use langchain hugging face with local model
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from langchain.llms import HuggingFacePipeline

model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)

llm = HuggingFacePipeline(pipeline=pipe)
print(llm("Write a short story about Bangalore."))

πŸ’¬ 3. langchain-huggingface chat model

  • Below is the example of chat model implementation in langchain.
  • You can use this code snippet to implement a chat model
from langchain.chat_models import ChatHuggingFace
from langchain.schema.messages import HumanMessage

chat = ChatHuggingFace(
    repo_id="HuggingFaceH4/zephyr-7b-beta",
    model_kwargs={"temperature": 0.6}
)

response = chat([HumanMessage(content="Explain Hugging Face in simple terms.")])
print(response.content)

🌐 4. langchain huggingface endpoint (API-based)

from langchain.llms import HuggingFaceEndpoint

llm = HuggingFaceEndpoint(
    endpoint_url="https://api-inference.huggingface.co/models/your-model",
    huggingfacehub_api_token="your-hf-token"
)

print(llm("What is GPT-OSS?"))

πŸ“¦ 5. langchain-huggingface pip – Install Recap

pip install langchain transformers huggingface_hub accelerate

🧠 6. langchain-huggingface embeddings

from langchain.embeddings import HuggingFaceEmbeddings

embed = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vector = embed.embed_query("What is GenAI?")
print(vector[:5])

πŸ§ͺ 7. Full langchain-huggingface Example With Chain

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

prompt = PromptTemplate.from_template("Write a blog intro about {topic}")
chain = LLMChain(llm=llm, prompt=prompt)

print(chain.run("Generative AI for startups"))

πŸ“š 8. langchain-huggingface github

βœ… Final Thoughts

With LangChain and Hugging Face combined, the possibilities are endlessβ€”from building GenAI tools to document search apps and advanced chatbots.

πŸ”Ž Keywords for this article:

  • langchain-huggingface
  • langchain huggingface example
  • huggingface local model
  • huggingface embeddings
  • huggingface endpoint
  • langchain huggingface chat model
  • huggingface pip install
  • huggingface github langchain

 

Leave a Reply

Your email address will not be published. Required fields are marked *