提示词
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