HuggingFaceReader¶
Overview¶
The HuggingFaceReader
class is designed to interface with Hugging Face's models to generate text based on a given prompt. It extends the BaseLLMReader
class and uses the transformers
library to communicate with the models.
Installation¶
- Install the
transformers
library:pip install transformers
Example Usage¶
Here's an example demonstrating how to use the HuggingFaceReader
class:
from euler.llm_readers import HuggingFaceReader
# Initialize the HuggingFaceReader
huggingface_reader = HuggingFaceReader(
api_key='your_hugging_face_api_key_here',
model_id='gpt2'
)
# Generate text based on the input prompt
text = "Once upon a time in a faraway land,"
response = huggingface_reader.read(text)
print(response.text if response else "No response received.")
Using LLMReaderFactory¶
The LLMReaderFactory
class can be used to easily initialize different LLM readers, including HuggingFaceReader
.
from euler.llm_readers import LLMReaderFactory
# Initialize the HuggingFaceReader using LLMReaderFactory
huggingface_reader = LLMReaderFactory.get_llm_reader(
reader_type='huggingface',
api_key='your_hugging_face_api_key_here',
model_id='gpt2'
)
# Generate text based on the input prompt
text = "Once upon a time in a faraway land,"
response = huggingface_reader.read(text)
print(response.text if response else "No response received.")
Classes and Methods¶
HuggingFaceConfig
¶
The HuggingFaceConfig
class is a configuration model that holds the API key, model ID, and an optional default prompt.
Attributes:
- api_key
(str): The API key for accessing the Hugging Face API.
- model_id
(str): The model ID to use for generating responses.
- default_prompt
(Optional[str]): A default prompt to use if no custom prompt is provided.
HuggingFaceResponse
¶
The HuggingFaceResponse
class represents the response from the Hugging Face API.
Attributes:
- text
(str): The response text generated by the Hugging Face model.
HuggingFaceReader
¶
The HuggingFaceReader
class interfaces with Hugging Face's models to generate text based on a given prompt.
Methods:
- __init__(self, api_key: str, model_id: str = 'gpt2', default_prompt: Optional[str] = None)
: Initializes the reader with the API key, model ID, and optional default prompt.
- read(self, text: str, custom_prompt: Optional[str] = None) -> Optional[HuggingFaceResponse]
: Reads the text and returns the generated response.
This documentation provides an overview, example usage, and detailed description of the HuggingFaceReader
class and its associated classes and methods.