Prompt
lazyllm.prompt_templates.prompt_template.PromptTemplate
Bases: BasePromptTemplate
Source code in lazyllm/prompt_templates/prompt_template.py
7 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 | |
validate_variables()
Validate the completeness of template variables.
Validation rules: 1. All keys in partial_vars must exist in template variables 2. required_vars + partial_vars keys must exactly equal all template variables
Raises:
-
ValueError–Raised when variable validation fails, including: - partial_vars contains variables not found in template - required_vars and partial_vars have overlap - The union of required_vars and partial_vars does not equal all template variables
Returns:
-
PromptTemplate–Validated instance itself
Source code in lazyllm/prompt_templates/prompt_template.py
format(**kwargs)
Format the prompt template.
Replace placeholders in template with provided variable values, supports function calls for partial variables.
Parameters:
-
**kwargs–Template variable names and corresponding values
Returns:
-
str(str) –Formatted prompt string
Raises:
-
KeyError–When required variables are missing or template variables not found
-
TypeError–When partial variable function call fails
-
ValueError–When template formatting fails
Source code in lazyllm/prompt_templates/prompt_template.py
partial(**partial_kwargs)
Create a partially filled copy of the template.
Set fixed values for specified variables, generating a new template instance where these variables no longer need to be provided.
Parameters:
-
**partial_kwargs–Variable names and values to set as partial variables
Returns:
-
PromptTemplate(PromptTemplate) –New partially filled template instance
Raises:
-
KeyError–When specified variables are not found in template
Source code in lazyllm/prompt_templates/prompt_template.py
from_template(template)
classmethod
Create PromptTemplate instance from template string.
Class method, automatically extracts all variables from template string and sets them as required variables.
Parameters:
-
template(str) –Template string containing {variable} placeholders
Returns:
-
PromptTemplate(PromptTemplate) –Newly created PromptTemplate instance
Source code in lazyllm/prompt_templates/prompt_template.py
lazyllm.prompt_templates.base.BasePromptTemplate
Bases: BaseModel, ABC
Source code in lazyllm/prompt_templates/base.py
get_template_variables(template)
staticmethod
Extracts all placeholder variable names from a given template string.
Uses Python's built-in string.Formatter to parse the template and identify placeholders . Returns a sorted list of unique variable names.
Parameters:
-
template(str) –A prompt template string containing placeholders
Returns:
-
list[str]–list[str]: A sorted list of placeholder variable names
Raises:
-
ValueError–If the template is malformed or parsing fails
Source code in lazyllm/prompt_templates/base.py
lazyllm.prompt_templates.few_shot_prompt_template.FewShotPromptTemplate
Bases: BasePromptTemplate
A few-shot prompt template class for constructing structured prompts with examples.
The template consists of three parts: a prefix, multiple formatted examples, and a suffix. Each example is rendered using the provided egs_prompt_template. It supports partial variable binding, allowing some variables to be pre-filled while others are supplied at final formatting time.
All template variables (from prefix and suffix) must be exactly covered by the union of required_vars (provided at runtime) and partial_vars (pre-bound).
Attributes:
-
prefix(str) –Introductory text before examples, may contain variable placeholders
-
suffix(str) –Instruction or question after examples, may contain variable placeholders
-
examples(List[Dict]) –List of example dictionaries, each must match egs_prompt_template's variables
-
egs_prompt_template(PromptTemplate) –Sub-template used to format each example
-
required_vars(List[str]) –List of variable names that must be provided in the final format call
-
partial_vars(Dict[str, Any]) –Pre-bound variables; values can be constants or zero-argument callables
-
separator_for_egs(str) –Separator between examples, defaults to newline '
'
Source code in lazyllm/prompt_templates/few_shot_prompt_template.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 | |
validate_variables()
A model validator automatically invoked after instance creation to ensure consistency between template variables and examples.
Performs the following checks: 1. All keys in partial_vars must appear as variables in the prefix or suffix; 2. required_vars and partial_vars must be disjoint (no overlap); 3. The union of required_vars and partial_vars must exactly match all variables in prefix and suffix; 4. Each example dictionary must contain all variables required by egs_prompt_template.
Raises ValueError if any check fails.
This method guarantees that the template is valid and self-consistent before use.
Source code in lazyllm/prompt_templates/few_shot_prompt_template.py
format(**kwargs)
Generates a complete few-shot prompt string by filling in the provided variables.
All variables listed in required_vars must be provided via kwargs. Variables in partial_vars are automatically applied (callable values are invoked) and will override any same-named kwargs. Each example is formatted using egs_prompt_template, joined by separator_for_egs, and combined with prefix and suffix to produce the final prompt.
Parameters:
-
**kwargs–Keyword arguments containing all required_vars
Returns:
-
str(str) –The fully rendered prompt text
Raises:
-
KeyError–If any required variable is missing or an unbound variable exists in the template
-
ValueError–If example formatting or final template rendering fails
Source code in lazyllm/prompt_templates/few_shot_prompt_template.py
partial(**kwargs)
Partially binds variables to the template and returns a new FewShotPromptTemplate instance.
The provided variables are moved from required_vars to partial_vars in the new instance, so they no longer need to be supplied in subsequent format calls. Useful for incrementally binding variables or creating reusable partially-filled templates.
Parameters:
-
**kwargs–Variable names and values to pre-bind (values can be constants or zero-argument callables)
Returns:
-
FewShotPromptTemplate(FewShotPromptTemplate) –A new template instance with the specified variables bound
Raises:
-
KeyError–If any variable in kwargs is not present in the template
Source code in lazyllm/prompt_templates/few_shot_prompt_template.py
lazyllm.prompt_templates.LazyLLMPromptLibraryBase
Base prompt library class that manages multilingual prompt collections and provides a unified access API.
Overview:
- Maintains a language-keyed dictionary of prompts (_prompts).
- Allows fetching a single prompt and listing all keys for a language.
- Instance may be initialized with a default language.
Main methods:
- get_prompt(key, lang=None): Retrieve the prompt (string or structured) by key and language. Falls back to instance language or class default when lang is omitted.
- get_all_keys(lang=None): Return a list of all prompt keys for the specified language; falls back to instance/default language when omitted.
Source code in lazyllm/prompt_templates/prompt_library.py
get_all_keys(lang=None)
List all available prompt keys for a specified language.
Parameters:
-
lang(str, default:None) –Optional language code ('zh' or 'en'). If omitted, instance language or class default is used.
Returns:
- list: A list of key names. Returns an empty list and logs a warning if the language is unsupported.
Source code in lazyllm/prompt_templates/prompt_library.py
get_prompt(key, lang=None)
Get the prompt for a given key and language.
Parameters:
-
key(str) –The prompt key name.
-
lang(str, default:None) –Optional language code ('zh' or 'en'). If omitted, instance language or class default is used.
Returns:
- str or dict: The prompt content (may be a string or structured dict). Raises ValueError if not found.
Source code in lazyllm/prompt_templates/prompt_library.py
lazyllm.ActorPrompt
Bases: LazyLLMPromptLibraryBase
Prompt library module. Contains a wide range of preset prompts, supporting Chinese and English categories, which can be retrieved by act (role) names.
Parameters:
-
lang(str, default:None) –Default language, optional 'zh' (Chinese) or 'en' (English). Defaults to 'zh' if not specified.
Examples:
>>> from lazyllm import ActorPrompt
>>> lib = ActorPrompt(lang='en')
>>> # Get all available acts
>>> acts = lib.get_all_acts()
>>> # Get prompt for a specific act
>>> prompt = lib.get_prompt('English Translator and Improver')
>>> # Also callable directly
>>> prompt = lib('English Translator and Improver')
>>> print(prompt[:50])
I want you to act as an English translator, spelli...
Source code in lazyllm/prompt_templates/prompt_library.py
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 | |
get_all_acts(lang=None)
Get the list of all supported acts for a specific language.
Parameters:
-
lang(str, default:None) –Language code ('zh' or 'en'). If not provided, uses the language set during initialization.
Returns:
- list: A list of all available act names.
Source code in lazyllm/prompt_templates/prompt_library.py
lazyllm.DataPrompt
Bases: LazyLLMPromptLibraryBase
Structured prompt library (for data processing modules) that stores prompts as dictionaries (including fields like system/user/tools/history/extra_keys) and builds ChatPrompter instances for use.
Features:
- Prompts can be added dynamically via the class method add_prompt.
- call returns a ChatPrompter by default or the raw prompt dict when return_raw=True.
Examples:
>>> from lazyllm import DataPrompt
>>> DataPrompt.add_prompt(
... act='simple_summarize',
... system_prompt='You are a concise summarizer.',
... user_prompt='Please summarize the following text: {text}',
... lang='en'
... )
>>> lib = DataPrompt(lang='en')
>>> prompter = lib('simple_summarize')
>>> print(type(prompter))
<class 'lazyllm.components.prompter.chatPrompter.ChatPrompter'>
>>> raw = lib('simple_summarize', return_raw=True)
>>> print(raw['system'][:20])
You are a concise su
Source code in lazyllm/prompt_templates/prompt_library.py
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 | |
__call__(key, lang=None, return_raw=False)
Build and return a ChatPrompter for the given key and language, or return the raw prompt dict when return_raw=True.
Parameters:
-
key(str) –Prompt key name.
-
lang(str, default:None) –Optional language code.
-
return_raw(bool, default:False) –If True, return the raw dict; otherwise return a ChatPrompter instance.
Returns:
- ChatPrompter or dict: Type depends on return_raw. Raises ValueError if key not found.
Source code in lazyllm/prompt_templates/prompt_library.py
add_prompt(act, system_prompt=None, user_prompt=None, tools=None, history=None, extra_keys=None, lang='zh')
classmethod
Add or overwrite a prompt entry in structured form.
Parameters:
-
act(str) –Prompt key name.
-
system_prompt(str, default:None) –Optional system message content.
-
user_prompt(str, default:None) –Optional user message content.
-
tools(any, default:None) –Optional tools description/config.
-
history(any, default:None) –Optional history context.
-
extra_keys(any, default:None) –Optional extra fields.
-
lang(str, default:'zh') –Target language code, default 'zh'.
Note: At least one of system_prompt or user_prompt must be provided.