llama-index llms huggingface integration
Project description
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/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for llama_index_llms_huggingface-0.3.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ee4154db830839f602375f82f6c641290a36cc353cbdde37f0254788d729daa |
|
MD5 | 55954013c60f8dfff9a50f29ae928a1c |
|
BLAKE2b-256 | d24fc1d6ad667d603f27aae9d01ff3843352c0ac6e568826952e1126061c57cf |
Close
Hashes for llama_index_llms_huggingface-0.3.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 164b69319d372f26f6c11951b1c5f0a5ead9a82ffc6f2365ebe698cbe28fbcdf |
|
MD5 | 1cb31de22abc2fe21a5a0336c478eccd |
|
BLAKE2b-256 | b30eec2114b8e0a26bdfec30432a95f49a038cf07a237737e4724e3430562564 |