Skip to content

Huggingface prompt builder

Defines a prompt builder that can be used for Hugging Face models that support chat templating.

Authors

Henry Wicaksono (henry.wicaksono@gdplabs.id)

References

[1] https://huggingface.co/docs/transformers/main/chat_templating [2] https://huggingface.co/models

HuggingFacePromptBuilder(system_template='', user_template='', model_name_or_path='', ignore_extra_keys=False)

Bases: BasePromptBuilder

A prompt builder for Hugging Face models that support chat templating.

The HuggingFacePromptBuilder class is designed to create prompts compatible with models hosted in Hugging Face, given that the model support chat templating. It loads the appropriate tokenizer and formats the prompt by injecting placeholder values.

Attributes:

Name Type Description
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}.

user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}.

prompt_key_set set[str]

A set of expected keys that must be present in the prompt templates.

ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt.

formatter HuggingFacePromptFormatter

The formatter to be used to format the prompt into a string in the format_as_string method.

Note

If you're trying to access the prompt builder template of a gated model, you'd need to: 1. Request access to the gated repo using your HuggingFace account. 2. Login to HuggingFace in your system. This can be done as follows: 2.1. Install huggingface-hub: pip install huggingface-hub 2.2. Login to HuggingFace: huggingface-cli login 2.3. Enter your HuggingFace token.

Initializes a new instance of the HuggingFacePromptBuilder class.

Parameters:

Name Type Description Default
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
model_name_or_path str

The model name or path of the Hugging Face model tokenizer to be loaded. Defaults to an empty string.

''
ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt. Defaults to False.

False