Skip to main content

llama-index llms huggingface integration

Project description

LlamaIndex Llms Integration: Huggingface

Installation

  1. 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
    
  2. 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/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_index_llms_huggingface-0.7.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llama_index_llms_huggingface-0.7.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_llms_huggingface-0.7.0.tar.gz.

File metadata

  • Download URL: llama_index_llms_huggingface-0.7.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for llama_index_llms_huggingface-0.7.0.tar.gz
Algorithm Hash digest
SHA256 a5057c0aa44122953d67e8157ad84f277417f8a935ccfe3f5dcdf1ac12e53f51
MD5 5d4f0812c897be0c99d5f893c9b0bb9c
BLAKE2b-256 d06f1460e058297dbb499c5644db39b71b469ccb92250281ab10fd405c272673

See more details on using hashes here.

File details

Details for the file llama_index_llms_huggingface-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: llama_index_llms_huggingface-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for llama_index_llms_huggingface-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5d27fa829a0c928ea1e4345a991755e70c7beda1225a3530a59db173c0e44042
MD5 aedd4b72030c8b30206e231ac651fa1b
BLAKE2b-256 85401e9c68cfd4cb9dd4999773cb24ca2c7e9ed90fefdfb07a0349a2a819fc58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page