prompt¶
The Prompt module provides core functionality for managing and rendering prompt templates.
This module contains multiple prompt template implementations for more convenient construction of dynamic LLM prompt content.
BasePromptTemplate ¶
Bases: BaseModel
Abstract base class for prompt templates.
This class provides a common interface for messages from template strings with variable substitutions.
Attributes:
| Name | Type | Description |
|---|---|---|
template_str | str | The template string containing placeholders for variable substitution. The specific placeholder syntax depends on the concrete implementation (e.g., f-string, Jinja2, etc.). |
Methods:
| Name | Description |
|---|---|
format_message | Format a single message from the template. |
format_messages | Format multiple messages from the template. |
Notes
This is an abstract base class that must be subclassed to provide concrete implementations. Subclasses should implement the format_message and format_messages methods according to their specific template formatting requirements.
Examples:
Source code in bridgic/core/prompt/_base_template.py
format_message ¶
Format a single message from the template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role | Union[Role, str] | The role of the message (e.g., 'user', 'assistant', 'system'). | Role.USER |
**kwargs | Additional keyword arguments to be substituted into the template. | {} |
Returns:
| Type | Description |
|---|---|
Message | A formatted message object. |
Raises:
| Type | Description |
|---|---|
NotImplementedError | This method must be implemented by subclasses. |
Source code in bridgic/core/prompt/_base_template.py
format_messages ¶
format_messages(**kwargs) -> List[Message]
Format multiple messages from the template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs | Additional keyword arguments to be substituted into the template. | {} |
Returns:
| Type | Description |
|---|---|
List[Message] | A list of formatted message objects. |
Raises:
| Type | Description |
|---|---|
NotImplementedError | This method must be implemented by subclasses. |
Source code in bridgic/core/prompt/_base_template.py
FstringPromptTemplate ¶
Bases: BasePromptTemplate
This template implementation uses Python's f-string syntax (braces {}).
Methods:
| Name | Description |
|---|---|
format_message | Format a single message from the template. |
Notes
This template supports single message rendering via format_message(). The template uses Python's built-in str.format() method for variable substitution, which provides basic formatting capabilities.
Examples:
Basic usage:
With context:
Multiple variables:
Source code in bridgic/core/prompt/_fstring_template.py
format_message ¶
Format a single message from the template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role | Union[Role, str] | The role of the message (e.g., 'user', 'assistant', 'system'). Required parameter for this template implementation. | required |
**kwargs | Keyword arguments containing values for all variables referenced in the template string. All variables must be provided. | {} |
Returns:
| Type | Description |
|---|---|
Message | A formatted message object with the specified role and rendered content. |
Raises:
| Type | Description |
|---|---|
PromptRenderError | If any variables referenced in the template are missing from the provided keyword arguments. |
Source code in bridgic/core/prompt/_fstring_template.py
EjinjaPromptTemplate ¶
Bases: BasePromptTemplate
Extended Jinja2-based prompt template with custom message blocks.
This template implementation extends the standard Jinja2 syntax with custom {% msg %} blocks to create structured Message objects. It supports both single message and multiple message rendering with variable substitution and content block parsing.
Attributes:
| Name | Type | Description |
|---|---|---|
_env_template | Template | The compiled Jinja2 template object. |
_render_cache | MemoryCache | Cache for rendered template results to improve performance. |
Methods:
| Name | Description |
|---|---|
format_message | Format a single message from the template. |
format_messages | Format multiple messages from the template. |
Notes
This template supports two rendering modes:
- Single Message Mode: Use
format_message()to render one message. - Multiple Messages Mode: Use
format_messages()to render multiple messages.
Examples:
Single message with role in template:
Single message with role as parameter:
Multiple messages:
Source code in bridgic/core/prompt/_ejinja_template.py
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 | |
format_message ¶
Format a single message from the template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role | Union[Role, str] | The role of the message. If the template contains a | None |
**kwargs | Additional keyword arguments to be substituted into the template. | {} |
Returns:
| Type | Description |
|---|---|
Message | A formatted message object with the specified role and content. |
Raises:
| Type | Description |
|---|---|
PromptSyntaxError | If the template contains more than one |
PromptRenderError | If role parameter conflicts with template-defined role, or if no role is specified when template has no |
Source code in bridgic/core/prompt/_ejinja_template.py
format_messages ¶
format_messages(**kwargs) -> List[Message]
Format multiple messages from the template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs | Additional keyword arguments to be substituted into the template. | {} |
Returns:
| Type | Description |
|---|---|
List[Message] | A list of formatted message objects. Each line of the rendered template should be a valid JSON representation of a Message object. If no valid messages are found but content exists, a default user message is created. |
Raises:
| Type | Description |
|---|---|
PromptRenderError | If any line in the rendered template is not a valid JSON representation of a Message object. |
Notes
This method uses caching to improve performance for repeated calls with the same parameters. The rendered template is cached based on the provided keyword arguments.