Populated prompt
This section documents the populated prompt class.
prompt_templates.populated_prompt
PopulatedPrompt
dataclass
A class representing a populated prompt that can be formatted for different LLM clients.
This class serves two main purposes: 1. Store populated prompts (either in simple text or chat format) 2. Convert chat prompts between different LLM client formats (e.g., OpenAI, Anthropic)
The class handles two types of content: * Text prompts: Simple strings that can be used directly with any LLM * Chat prompts: Lists or Dicts of messages that are compatible with the format expected by different LLM clients
Attributes:
Name | Type | Description |
---|---|---|
_content |
Union[str, List[Dict[str, Any]], Dict[str, Any]]
|
The populated prompt content, either as a string or a list of message dictionaries. |
Access
You can access individual elements of a PopulatedPrompt instance using standard indexing or key access,
i.e. you can use prompt[index]
or prompt[key]
.
Examples:
>>> from prompt_templates import PromptTemplateLoader
>>> prompt_template = PromptTemplateLoader.from_hub(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> prompt = prompt_template.populate_template(
... concept="list comprehension",
... programming_language="Python"
... )
>>> print(prompt)
[{'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.'}]
You can also access individual elements of the prompt like with standard lists and dicts:
>>> print(prompt[0]["content"])
'You are a coding assistant who explains concepts clearly and provides short examples.'
Source code in prompt_templates/populated_prompt.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 |
|
to_dict
Make the class JSON serializable for LLM clients by returning its raw content.
__getattr__
Allow the class to be used directly with LLM clients by forwarding attribute access to _content.
Source code in prompt_templates/populated_prompt.py
__getitem__
Allow direct access to the content elements.
Source code in prompt_templates/populated_prompt.py
__iter__
Make the prompt iterable if content is a list.
__len__
format_for_client
Format the chat messages prompt for a specific LLM client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client |
str
|
The client format to use ('openai', 'anthropic', 'google'). Defaults to 'openai'. |
'openai'
|
Returns:
Name | Type | Description |
---|---|---|
PopulatedPrompt |
PopulatedPrompt
|
A new PopulatedPrompt instance with content formatted for the specified client. |
Raises:
Type | Description |
---|---|
ValueError
|
If an unsupported client format is specified or if trying to format a non-messages template. |
Examples:
Format chat messages for different LLM clients:
>>> from prompt_templates import PromptTemplateLoader
>>> prompt_template = PromptTemplateLoader.from_hub(
... repo_id="MoritzLaurer/example_prompts",
... filename="code_teacher.yaml"
... )
>>> prompt = prompt_template.populate_template(
... concept="list comprehension",
... programming_language="Python"
... )
>>> print(prompt) # By default in OpenAI format
[{'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.'}]
>>> # Convert to Anthropic format
>>> anthropic_prompt = prompt.format_for_client("anthropic")
>>> print(anthropic_prompt)
{'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.'}]}
>>> # Convert to Google (Gemini) format
>>> google_prompt = prompt.format_for_client("google")
>>> print(google_prompt)
{'system_instruction': 'You are a coding assistant who explains concepts clearly and provides short examples.', 'contents': 'Explain what list comprehension is in Python.'}