Downloading prompts
This section documents the functions for downloading and listing prompt templates from the Hugging Face Hub.
hf_hub_prompts.hub_api
download_prompt_template
Download a prompt template from the Hugging Face Hub.
Examples:
Download and use a text prompt template:
>>> from hf_hub_prompts import download_prompt_template
>>> # Download translation prompt
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="translate.yaml"
... )
>>> # Inspect the template
>>> prompt_template.template
'Translate the following text to {language}:\n{text}'
>>> prompt_template.input_variables
['language', 'text']
>>> prompt_template.metadata['name']
'Simple Translator'
Download and use a chat prompt template:
>>> # Downloadas code teaching prompt
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> # Inspect the template
>>> prompt_template.messages
[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what {concept} is in {programming_language}.'}]
>>> prompt_template.input_variables
['concept', 'programming_language']
>>> prompt_template.metadata['version']
'0.0.1'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The repository ID on Hugging Face Hub (e.g., 'username/repo_name'). |
required |
filename |
str
|
The filename of the prompt YAML file. |
required |
repo_type |
Optional[str]
|
The type of repository to download from. Defaults to "model". |
'model'
|
Returns:
Name | Type | Description |
---|---|---|
BasePromptTemplate |
Union[TextPromptTemplate, ChatPromptTemplate]
|
The appropriate template type based on YAML content: - TextPromptTemplate: If YAML contains a 'template' key - ChatPromptTemplate: If YAML contains a 'messages' key |
Raises:
Type | Description |
---|---|
ValueError
|
If the YAML file cannot be parsed or does not meet the expected structure. |
Source code in hf_hub_prompts/hub_api.py
list_prompt_templates
List available prompt template YAML files in a Hugging Face Hub repository.
Examples:
List all prompt templates in a repository:
>>> from hf_hub_prompts import list_prompt_templates
>>> files = list_prompt_templates("MoritzLaurer/example_prompts")
>>> files
['code_teacher.yaml', 'translate.yaml']
Note
This function simply returns all YAML file names in the repository. It does not validate if the files contain valid prompt templates, which would require downloading them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The repository ID on Hugging Face Hub. |
required |
repo_type |
Optional[str]
|
The type of repository. Defaults to "model". |
'model'
|
token |
Optional[str]
|
An optional authentication token. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: A list of YAML filenames in the repository sorted alphabetically. |