Downloading prompts and tools
This section documents the classes and functions for downloading prompt templates and tools from the Hugging Face Hub.
prompt_templates.loaders
PromptTemplateLoader
Class for loading prompt templates from different sources.
This class provides methods to load prompt templates from either local files or the Hugging Face Hub. Templates are expected to be YAML files that follow a standardized format for either text or chat prompts.
Examples:
Load a template from the Hub:
>>> from prompt_templates import PromptTemplateLoader
>>> prompt_template = PromptTemplateLoader.from_hub(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> print(prompt_template)
ChatPromptTemplate(template=[{'role': 'system', 'content': 'You are a coding a..., template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator='jinja2')
>>> prompt_template.template
[{'role': 'system', 'content': 'You are a coding assistant...'}, ...]
>>> prompt_template.metadata["name"]
'Code Teacher'
Load a template from a local file:
>>> prompt_template = PromptTemplateLoader.from_local("./tests/test_data/translate.yaml")
>>> print(template)
TextPromptTemplate(template='Translate the following text to {{language}}:\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator='jinja2')
>>> prompt_template.template
'Translate the following text to {language}:\n{text}'
>>> prompt_template.template_variables
['language', 'text']
Source code in prompt_templates/loaders.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
from_local
classmethod
Load a prompt template from a local YAML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, Path]
|
Path to the YAML file containing the prompt template |
required |
populator |
[PopulatorType]
|
The populator type to use among Literal["double_brace_regex", "single_brace_regex", "jinja2"]. Defaults to "jinja2". |
'jinja2'
|
jinja2_security_level |
Literal['strict', 'standard', 'relaxed']
|
The security level for the Jinja2 populator. Defaults to "standard". |
'standard'
|
yaml_library |
str
|
The YAML library to use ("ruamel" or "pyyaml"). Defaults to "ruamel". |
'ruamel'
|
Returns:
Type | Description |
---|---|
Union[TextPromptTemplate, ChatPromptTemplate]
|
Union[TextPromptTemplate, ChatPromptTemplate]: The loaded template instance |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the file doesn't exist |
ValueError
|
If the file is not a .yaml/.yml file |
ValueError
|
If the YAML structure is invalid |
Examples:
Download a text prompt template:
>>> from prompt_templates import PromptTemplateLoader
>>> prompt_template = PromptTemplateLoader.from_local("./tests/test_data/translate.yaml")
>>> print(prompt_template)
TextPromptTemplate(template='Translate the following text to {{language}}:\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator='jinja2')
>>> prompt_template.template
'Translate the following text to {language}:\n{text}'
>>> prompt_template.template_variables
['language', 'text']
>>> prompt_template.metadata['name']
'Simple Translator'
Download a chat prompt template:
>>> prompt_template = PromptTemplateLoader.from_local("./tests/test_data/code_teacher.yaml")
>>> print(prompt_template)
ChatPromptTemplate(template=[{'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}.'}], template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator='jinja2')
>>> prompt_template.template
[{'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.template_variables
['concept', 'programming_language']
>>> prompt_template.metadata['version']
'0.0.1'
Source code in prompt_templates/loaders.py
from_hub
classmethod
from_hub(repo_id, filename, repo_type='dataset', revision=None, populator='jinja2', jinja2_security_level='standard', yaml_library='ruamel')
Load a prompt template from the Hugging Face Hub.
Downloads and loads a prompt template from a repository on the Hugging Face Hub. The template file should be a YAML file following the standardized format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The repository ID on Hugging Face Hub (e.g., 'username/repo') |
required |
filename |
str
|
Name of the YAML file containing the template |
required |
repo_type |
str
|
Type of repository. Must be one of ['dataset', 'model', 'space']. Defaults to "dataset" |
'dataset'
|
revision |
Optional[str]
|
Git revision to download from. Can be a branch name, tag, or commit hash. Defaults to None |
None
|
populator |
[PopulatorType]
|
The populator type to use among Literal["double_brace_regex", "single_brace_regex", "jinja2"]. Defaults to "jinja2". |
'jinja2'
|
jinja2_security_level |
Literal['strict', 'standard', 'relaxed']
|
The security level for the Jinja2 populator. Defaults to "standard". |
'standard'
|
yaml_library |
str
|
The YAML library to use ("ruamel" or "pyyaml"). Defaults to "ruamel". |
'ruamel'
|
Returns:
Type | Description |
---|---|
Union[TextPromptTemplate, ChatPromptTemplate]
|
Union[TextPromptTemplate, ChatPromptTemplate]: The loaded template instance |
Raises:
Type | Description |
---|---|
ValueError
|
If repo_id format is invalid |
ValueError
|
If repo_type is invalid |
FileNotFoundError
|
If file cannot be downloaded from Hub |
ValueError
|
If the YAML structure is invalid |
Examples:
Download a text prompt template:
>>> from prompt_templates import PromptTemplateLoader
>>> prompt_template = PromptTemplateLoader.from_hub(
... repo_id="MoritzLaurer/example_prompts",
... filename="translate.yaml"
... )
>>> print(prompt_template)
TextPromptTemplate(template='Translate the following text to {{language}}:\n{{..., template_variables=['language', 'text'], metadata={'name': 'Simple Translator', 'description': 'A si..., custom_data={}, populator='jinja2')
>>> prompt_template.template
'Translate the following text to {language}:\n{text}'
>>> prompt_template.template_variables
['language', 'text']
>>> prompt_template.metadata['name']
'Simple Translator'
Download a chat prompt template:
>>> prompt_template = PromptTemplateLoader.from_hub(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> print(prompt_template)
ChatPromptTemplate(template=[{'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}.'}], template_variables=['concept', 'programming_language'], metadata={'name': 'Code Teacher', 'description': 'A simple ..., custom_data={}, populator='jinja2')
>>> prompt_template.template
[{'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.template_variables
['concept', 'programming_language']
>>> prompt_template.metadata['version']
'0.0.1'
Source code in prompt_templates/loaders.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
ToolLoader
Class for loading tools from different sources.
This class provides methods to load tool functions from either local files or the Hugging Face Hub. Tools are expected to be single Python functions with Google-style docstrings that specify their functionality, inputs, outputs, and metadata.
Note
The ToolLoader class and related functionalities for working with tools is still highly experimental.
Examples:
Load a tool from a local file:
>>> tool = ToolLoader.from_local("./tests/test_data/get_stock_price.py")
>>> tool.name
'get_stock_price'
Load a tool from the Hub:
>>> tool = ToolLoader.from_hub(
... repo_id="MoritzLaurer/example_tools",
... filename="get_stock_price.py"
... )
Source code in prompt_templates/loaders.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
|
from_local
classmethod
Load a tool from a local Python file. The Python file should contain exactly one function with a Google-style docstring.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, Path]
|
Path to the Python file containing the tool function |
required |
Returns:
Name | Type | Description |
---|---|---|
Tool |
Tool
|
The loaded tool instance |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the file doesn't exist |
ValueError
|
If the file is not a .py file |
ImportError
|
If the module cannot be loaded |
ValueError
|
If file doesn't contain exactly one function or is missing docstring |
Examples:
>>> tool = ToolLoader.from_local("./tests/test_data/get_stock_price.py")
>>> print(tool.description)
Retrieve stock price data for a given ticker symbol.
Source code in prompt_templates/loaders.py
from_hub
classmethod
Load a tool from the Hugging Face Hub.
Downloads and loads a tool function from a repository on the Hugging Face Hub. The tool file should contain exactly one function with a Google-style docstring.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The repository ID on Hugging Face Hub (e.g., 'username/repo') |
required |
filename |
str
|
Name of the Python file containing the tool |
required |
repo_type |
str
|
Type of repository. Must be one of ['dataset', 'model', 'space']. Defaults to "dataset" |
'dataset'
|
revision |
Optional[str]
|
Git revision to download from. Can be a branch name, tag, or commit hash. Defaults to None |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tool |
Tool
|
The loaded tool instance |
Raises:
Type | Description |
---|---|
ValueError
|
If repo_id format is invalid |
ValueError
|
If repo_type is invalid |
FileNotFoundError
|
If file cannot be downloaded from Hub |
ImportError
|
If the module cannot be loaded |
ValueError
|
If file doesn't contain exactly one function or is missing docstring |
Examples:
>>> tool = ToolLoader.from_hub(
... repo_id="MoritzLaurer/example_tools",
... filename="get_stock_price.py"
... )
>>> print(tool.metadata)
{'version': '0.0.1', 'author': 'John Doe', 'requires_gpu': 'False', 'requires_api_key': 'False'}
Source code in prompt_templates/loaders.py
list_prompt_templates
List available prompt template YAML files in a Hugging Face Hub repository.
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 "dataset". |
'dataset'
|
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. |
Examples:
List all prompt templates in a repository:
>>> from prompt_templates import list_prompt_templates
>>> files = list_prompt_templates("MoritzLaurer/example_prompts")
>>> files
['code_teacher.yaml', 'translate.yaml', 'translate_jinja2.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.
Source code in prompt_templates/loaders.py
list_tools
List available tool Python files in a Hugging Face Hub repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str
|
The repository ID on Hugging Face Hub |
required |
repo_type |
str
|
The type of repository. Defaults to "dataset" |
'dataset'
|
token |
Optional[str]
|
An optional authentication token. Defaults to None |
None
|
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: A list of Python filenames in the repository sorted alphabetically |
Examples:
List all tools in a repository:
>>> from prompt_templates import list_tools
>>> files = list_tools("MoritzLaurer/example_tools")
>>> files
['get_stock_price.py']
Note
This function simply returns all .py file names in the repository. It does not validate if the files contain valid tools, which would require downloading them.