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.4.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for llama_index_llms_huggingface-0.4.0.tar.gz
Algorithm Hash digest
SHA256 eb608b05a4546bb3b26ce3c97dbcd219d535505693bc7c63702e6e8ec8ded719
MD5 2d895bb614b580dbcfd8f732f965f268
BLAKE2b-256 c691023eb83657a11031829acd70c83653bfd551d23128940afc1e2955fbd2eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for llama_index_llms_huggingface-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 629d135f1a7476658dc432d57e5742efc5ab1f45d369b03c36663980e125ca71
MD5 ca5c7418ece78e21db7daa038601bdc2
BLAKE2b-256 c117c47b4759d0743a91e8c471db3b7ef7a3698784bb8e352d2b88b26ae50249

See more details on using hashes here.

Supported by

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