- 1. Using Hosted Hugging Face Models (init_chat_model)
- 2. Direct Initialization
- 3. Practice Exercises
1. Using Hosted Hugging Face Models (init_chat_model)
The recommended, modern way to use hosted Hugging Face models in LangChain is by using the uniforminit_chat_model function with model_provider="huggingface". This handles model initialization, connection to the serverless Inference API, and chat message formatting automatically.
[!IMPORTANT] SpecifyQuestion: Initialize a Hugging Face chat model directly using LangChain’s uniformbackend="endpoint": By default,init_chat_model(model_provider="huggingface")attempts to run models locally (usingHuggingFacePipelineunder the hood). This will trigger a gated model error and attempt to download the entire model to your system. To query the serverless hosted Inference API instead, always setbackend="endpoint".
init_chat_model function to query the hosted serverless API and query it.
Plan & Steps:
- Install
langchain-huggingfacepackage:pip install langchain-huggingface. - Ensure you have your
HUGGINGFACEHUB_API_TOKENset in your.envfile. - Import
init_chat_modelfromlangchain.chat_models. - Initialize the chat model using
init_chat_model, specifying an open-access model (likeQwen/Qwen2.5-7B-Instruct), settingmodel_provider="huggingface", and settingbackend="endpoint". - Pass a list of structured messages to the model and invoke it.
Code Explanation:
init_chat_model(model_provider="huggingface"): Dynamically initializes and returns aChatHuggingFacewrapper around an inferred serverlessHuggingFaceEndpointinstance, offering a seamless provider-agnostic experience.backend="endpoint": Tells LangChain to execute queries via Hugging Face’s serverless hosted Inference API instead of installing and running the model locally.
Running Locally with init_chat_model:
If you want to run the model locally using init_chat_model, you can simply change backend="endpoint" to backend="local" (or omit it entirely, since local is the default backend). Just like the other local approaches, this will download the model to your machine and requires the local packages (transformers, torch, accelerate) installed:
2. Direct Initialization
If you want a single-class interface resembling standard classes likeChatOpenAI, you can use the from_model_id() factory constructor. This allows you to easily run open-source models either hosted in the cloud (Serverless API) or completely locally on your hardware.
Hosted Option
Query the hosted model on Hugging Face’s serverless API using the direct factory method. Question: InitializeChatHuggingFace directly without manually creating an endpoint object using the from_model_id() factory method to run hosted serverless queries.
Plan & Steps:
- Import
ChatHuggingFacefromlangchain_huggingfaceandHumanMessagefromlangchain_core.messages. - Initialize
ChatHuggingFace.from_model_idpassing the model ID (Qwen/Qwen2.5-7B-Instruct) and settingbackend="endpoint". - Pass a human message to the model and invoke it.
Local Option
To run the model completely locally on your own CPU or GPU (offline and private), you can setbackend="local".
Prerequisites:
Because this downloads the model and runs it on your machine, you must install the machine learning libraries first:ChatHuggingFace.from_model_id() and query it offline.
Plan & Steps:
- Import
ChatHuggingFacefromlangchain_huggingfaceandHumanMessagefromlangchain_core.messages. - Initialize the local chat model by setting
model_id="Qwen/Qwen2.5-0.5B-Instruct"(a tiny ~1 GB model that runs easily on standard hardware) and settingbackend="local". - Pass a human message to the model and invoke it.
Code Explanation:
ChatHuggingFace.from_model_id: A factory constructor that automatically creates the underlying wrapper based on the model ID.backend="endpoint"vs.backend="local": Toggles between serverless cloud execution (no heavy libraries needed) and local execution using local GPU/CPU.
3. Practice Exercises
Practice 1: Swapping to GPU
How do you configure a local pipeline run to execute on a CUDA-enabled GPU (NVIDIA) if one is available on your machine?Solution
Solution
When using
ChatHuggingFace.from_model_id() locally, you can pass pipeline parameters directly inside pipeline_kwargs. Setting device_map="auto" automatically handles GPU allocation: