docs/examples/llm/huggingface.ipynb
<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/huggingface.ipynb" target="_parent"></a>
There are many ways to interface with LLMs from Hugging Face, either locally or via Hugging Face's Inference Providers.
Hugging Face itself provides several Python packages to enable access,
which LlamaIndex wraps into LLM entities:
transformers package:
use llama_index.llms.HuggingFaceLLMhuggingface_hub[inference]:
use llama_index.llms.HuggingFaceInferenceAPIThere are many possible permutations of these two, so this notebook only details a few. Let's use Hugging Face's Text Generation task as our example.
In the below line, we install the packages necessary for this demo:
transformers[torch] is needed for HuggingFaceLLMhuggingface_hub[inference] is needed for HuggingFaceInferenceAPIzsh)%pip install llama-index-llms-huggingface # for local inference
%pip install llama-index-llms-huggingface-api # for remote inference
!pip install "transformers[torch]" "huggingface_hub[inference]"
If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.
!pip install llama-index
Now that we're set up, let's play around:
First, you need to create a Hugging Face account and get a token. You can sign up here. Then you'll need to create a token here.
export HUGGING_FACE_TOKEN=hf_your_token_here
import os
from typing import List, Optional
from llama_index.llms.huggingface import HuggingFaceLLM
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
HF_TOKEN: Optional[str] = os.getenv("HUGGING_FACE_TOKEN")
# NOTE: None default will fall back on Hugging Face's token storage
# when this token gets used within HuggingFaceInferenceAPI
The easiest way to use an open source model is to use the Hugging Face Inference Providers. Let's use the DeepSeek R1 model, which is great for complex tasks.
With inference providers, you can use the model on serverless infrastructure from inference providers.
remotely_run = HuggingFaceInferenceAPI(
model_name="deepseek-ai/DeepSeek-R1-0528",
token=HF_TOKEN,
provider="auto", # this will use the best provider available
)
We can also specify our preferred inference provider. Let's use the together provider.
remotely_run = HuggingFaceInferenceAPI(
model_name="Qwen/Qwen3-235B-A22B",
token=HF_TOKEN,
provider="together", # this will use the best provider available
)
First, we'll use an open source model that's optimized for local inference. This model is downloaded (if first invocation) to the local Hugging Face model cache, and actually runs the model on your local machine's hardware.
We'll use the Gemma 3N E4B model, which is optimized for local inference.
locally_run = HuggingFaceLLM(model_name="google/gemma-3n-E4B-it")
We can also spin up a dedicated Inference Endpoint for a model and use that to run the model.
endpoint_server = HuggingFaceInferenceAPI(
model="https://(<your-endpoint>.eu-west-1.aws.endpoints.huggingface.cloud"
)
We can also use a local inference engine like vLLM or TGI to run the model.
# You can also connect to a model being served by a local or remote
# Text Generation Inference server
tgi_server = HuggingFaceInferenceAPI(model="http://localhost:8080")
Underlying a completion with HuggingFaceInferenceAPI is Hugging Face's
Text Generation task.
completion_response = remotely_run_recommended.complete("To infinity, and")
print(completion_response)
If you are modifying the LLM, you should also change the global tokenizer to match!
from llama_index.core import set_global_tokenizer
from transformers import AutoTokenizer
set_global_tokenizer(
AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha").encode
)
If you're curious, other Hugging Face Inference API tasks wrapped are:
llama_index.llms.HuggingFaceInferenceAPI.chat: Conversational taskllama_index.embeddings.HuggingFaceInferenceAPIEmbedding: Feature Extraction taskAnd yes, Hugging Face embedding models are supported with:
transformers[torch]: wrapped by HuggingFaceEmbeddinghuggingface_hub[inference]: wrapped by HuggingFaceInferenceAPIEmbeddingBoth of the above two subclass llama_index.embeddings.base.BaseEmbedding.