Prompt templates
This section documents the prompt template classes.
hf_hub_prompts.prompt_templates
BasePromptTemplate
Bases: ABC
An abstract base class for prompt templates.
This class defines the common interface and shared functionality for all prompt templates. Users should not instantiate this class directly, but instead use TextPromptTemplate or ChatPromptTemplate, which are subclasses of BasePromptTemplate.
Source code in hf_hub_prompts/prompt_templates.py
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 |
|
populate_template
abstractmethod
Abstract method to populate the prompt template with the given variables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**input_variables |
Any
|
The values to fill placeholders in the template. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
PopulatedPrompt |
PopulatedPrompt
|
A PopulatedPrompt object containing the populated content. |
Source code in hf_hub_prompts/prompt_templates.py
display
Display the prompt configuration in the specified format.
Examples:
>>> from hf_hub_prompts import download_prompt_template
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="translate.yaml"
... )
>>> prompt_template.display(format="yaml")
template: 'Translate the following text to {language}:
{text}'
input_variables:
- language
- text
metadata:
name: Simple Translator
description: A simple translation prompt for illustrating the standard prompt YAML
format
tags:
- translation
- multilinguality
version: 0.0.1
author: Some Person
Source code in hf_hub_prompts/prompt_templates.py
TextPromptTemplate
Bases: BasePromptTemplate
A class representing a standard text prompt template.
Examples:
Download and use a text prompt template:
>>> from hf_hub_prompts import download_prompt_template
>>> # Download example translation prompt
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="translate.yaml"
... )
>>> # Inspect template attributes
>>> prompt_template.template
'Translate the following text to {language}:\n{text}'
>>> prompt_template.input_variables
['language', 'text']
>>> prompt_template.metadata['name']
'Simple Translator'
>>> # Use the template
>>> prompt = prompt_template.populate_template(
... language="French",
... text="Hello world!"
... )
>>> prompt.content
'Translate the following text to French:\nHello world!'
Source code in hf_hub_prompts/prompt_templates.py
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 |
|
populate_template
Populate the prompt by replacing placeholders with provided values.
Examples:
>>> from hf_hub_prompts import download_prompt_template
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="translate.yaml"
... )
>>> prompt_template.template
'Translate the following text to {language}:\n{text}'
>>> prompt = prompt_template.populate_template(
... language="French",
... text="Hello world!"
... )
>>> prompt.content
'Translate the following text to French:\nHello world!'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**input_variables |
Any
|
The values to fill placeholders in the prompt template. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
PopulatedPrompt |
PopulatedPrompt
|
A PopulatedPrompt object containing the populated prompt string. |
Source code in hf_hub_prompts/prompt_templates.py
to_langchain_template
Convert the TextPromptTemplate to a LangChain PromptTemplate.
Examples:
>>> from hf_hub_prompts import download_prompt_template
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="translate.yaml"
... )
>>> lc_template = prompt_template.to_langchain_template()
>>> # test equivalence
>>> from langchain_core.prompts import PromptTemplate as LC_PromptTemplate
>>> isinstance(lc_template, LC_PromptTemplate)
True
Returns:
Name | Type | Description |
---|---|---|
PromptTemplate |
PromptTemplate
|
A LangChain PromptTemplate object. |
Raises:
Type | Description |
---|---|
ImportError
|
If LangChain is not installed. |
Source code in hf_hub_prompts/prompt_templates.py
ChatPromptTemplate
Bases: BasePromptTemplate
A class representing a chat prompt template that can be formatted for and used with various LLM clients.
Examples:
Download and use a chat prompt template:
>>> from hf_hub_prompts import download_prompt_template
>>> # Download example code teaching prompt
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> # Inspect template attributes
>>> 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']
>>> # Populate the template
>>> prompt = prompt_template.populate_template(
... concept="list comprehension",
... programming_language="Python"
... )
>>> prompt.content
[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what list comprehension is in Python.'}]
>>> # By default, the populated prompt is in the OpenAI messages format, as it is adopted by many open-source libraries
>>> # You can convert to formats used by other LLM clients like Anthropic like this:
>>> messages_anthropic = prompt.format_for_client("anthropic")
>>> messages_anthropic
{'system': 'You are a coding assistant who explains concepts clearly and provides short examples.', 'messages': [{'role': 'user', 'content': 'Explain what list comprehension is in Python.'}]}
>>> # Convenience method to populate and format in one step
>>> messages = prompt_template.create_messages(
... client="anthropic",
... concept="list comprehension",
... programming_language="Python"
... )
>>> messages
{'system': 'You are a coding assistant who explains concepts clearly and provides short examples.', 'messages': [{'role': 'user', 'content': 'Explain what list comprehension is in Python.'}]}
Source code in hf_hub_prompts/prompt_templates.py
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 311 312 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 |
|
populate_template
Populate the prompt messages by replacing placeholders with provided values.
Examples:
>>> from hf_hub_prompts import download_prompt_template
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> prompt = prompt_template.populate_template(
... concept="list comprehension",
... programming_language="Python"
... )
>>> prompt.content
[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what list comprehension is in Python.'}]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**input_variables |
Any
|
The values to fill placeholders in the messages. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
PopulatedPrompt |
PopulatedPrompt
|
A PopulatedPrompt object containing the populated messages. |
Source code in hf_hub_prompts/prompt_templates.py
create_messages
Convenience method to populate a prompt template and format for client in one step.
Examples:
>>> from hf_hub_prompts import download_prompt_template
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> # Format for OpenAI (default)
>>> messages = prompt_template.create_messages(
... concept="list comprehension",
... programming_language="Python"
... )
>>> messages
[{'role': 'system', 'content': 'You are a coding assistant who explains concepts clearly and provides short examples.'}, {'role': 'user', 'content': 'Explain what list comprehension is in Python.'}]
>>> # Format for Anthropic
>>> messages = prompt_template.create_messages(
... client="anthropic",
... concept="list comprehension",
... programming_language="Python"
... )
>>> messages
{'system': 'You are a coding assistant who explains concepts clearly and provides short examples.', 'messages': [{'role': 'user', 'content': 'Explain what list comprehension is in Python.'}]}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client |
str
|
The client format to use ('openai', 'anthropic'). Defaults to 'openai'. |
'openai'
|
**input_variables |
Any
|
The variables to fill into the prompt template. For example, if your template expects variables like 'name' and 'age', pass them as keyword arguments: |
{}
|
Returns:
Type | Description |
---|---|
Union[List[Dict[str, Any]], Dict[str, Any]]
|
Union[List[Dict[str, Any]], Dict[str, Any]]: Populated and formatted messages. |
Source code in hf_hub_prompts/prompt_templates.py
to_langchain_template
Convert the ChatPromptTemplate to a LangChain ChatPromptTemplate.
Examples:
>>> from hf_hub_prompts import download_prompt_template
>>> prompt_template = download_prompt_template(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> lc_template = prompt_template.to_langchain_template()
>>> # test equivalence
>>> from langchain_core.prompts import ChatPromptTemplate as LC_ChatPromptTemplate
>>> isinstance(lc_template, LC_ChatPromptTemplate)
True
Returns:
Name | Type | Description |
---|---|---|
ChatPromptTemplate |
ChatPromptTemplate
|
A LangChain ChatPromptTemplate object. |
Raises:
Type | Description |
---|---|
ImportError
|
If LangChain is not installed. |