LlamaIndex Llms Integration: Huggingface
Installation
-
Install the required Python packages:
%pip install llama-index-llms-huggingface %pip install llama-index-llms-huggingface-api !pip install "transformers[torch]" "huggingface_hub[inference]" !pip install llama-index
-
Set the Hugging Face API token as an environment variable:
export HUGGING_FACE_TOKEN=your_token_here
Usage
Import Required Libraries
import os
from typing import List, Optional
from llama_index.llms.huggingface import HuggingFaceLLM
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
Run a Model Locally
To run the model locally on your machine:
locally_run = HuggingFaceLLM(model_name="HuggingFaceH4/zephyr-7b-alpha")
Run a Model Remotely
To run the model remotely using Hugging Face's Inference API:
HF_TOKEN: Optional[str] = os.getenv("HUGGING_FACE_TOKEN")
remotely_run = HuggingFaceInferenceAPI(
model_name="HuggingFaceH4/zephyr-7b-alpha", token=HF_TOKEN
)
Anonymous Remote Execution
You can also use the Inference API anonymously without providing a token:
remotely_run_anon = HuggingFaceInferenceAPI(
model_name="HuggingFaceH4/zephyr-7b-alpha"
)
Use Recommended Model
If you do not provide a model name, Hugging Face's recommended model is used:
remotely_run_recommended = HuggingFaceInferenceAPI(token=HF_TOKEN)
Generate Text Completion
To generate a text completion using the remote model:
completion_response = remotely_run_recommended.complete("To infinity, and")
print(completion_response)
Set Global Tokenizer
If you modify the LLM, ensure you 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
)
LLM Implementation example
https://docs.llamaindex.ai/en/stable/examples/llm/huggingface/
Metadata
Release files for llama-index-llms-huggingface 0.9.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llama_index_llms_huggingface-0.9.0.tar.gz | 7.9 kB | Without provenance | Details for llama_index_llms_huggingface-0.9.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llama_index_llms_huggingface-0.9.0-py3-none-any.whl | Python 3 | none | any | Details for llama_index_llms_huggingface-0.9.0-py3-none-any.whl |
Total release size: 15.6 kB
Release files / llama_index_llms_huggingface-0.9.0.tar.gz
| Download URL | llama_index_llms_huggingface-0.9.0.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c872f49dbaf4839d9e29ef94f20d5f8faf789e6c02c672e839f82a15f3d165a4
|
|
BLAKE2b-256 checksum How to use checksums |
b00129a1deba2791afefddca9be5dd8f1e2ed7548a0dc52cab768bf4e49ad054
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / llama_index_llms_huggingface-0.9.0-py3-none-any.whl
| Download URL | llama_index_llms_huggingface-0.9.0-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1f42809018614d5487df8c029f37f45f380ac1d41cca51004d65a9e3064994c5
|
|
BLAKE2b-256 checksum How to use checksums |
92340404da209e7c8679fbc67d862f83d6d84684f615284ec3f09d62614982c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for llama-index-llms-huggingface 0.9.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.