Skip to content

Prompt

lazyllm.prompt_templates.prompt_template.PromptTemplate

Bases: BasePromptTemplate

Source code in lazyllm/prompt_templates/prompt_template.py
class PromptTemplate(BasePromptTemplate):
    model_config = ConfigDict(frozen=True)
    template: str = Field(..., description='The prompt template string with {variable} placeholders')
    required_vars: List[str] = Field(default_factory=list, description='List of required variable names')
    partial_vars: Dict[str, Any] = Field(
        default_factory=dict, description='Dictionary of partial variable functions or values'
    )

    @model_validator(mode='after')
    def validate_variables(self):
        """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
"""
        # 1. All keys in partial_vars must exist in template variables
        # 2. required_vars + partial_vars keys must exactly equal all template variables
        # Extract all variables from template
        all_vars = set(BasePromptTemplate.get_template_variables(self.template))

        # Check if partial_vars.keys() exist in template variables
        partial_vars_keys = set(self.partial_vars.keys())
        invalid_partial_vars = partial_vars_keys - all_vars
        if invalid_partial_vars:
            raise ValueError(f'partial_vars contains variables not found in template: {invalid_partial_vars}')

        required_vars_set = set(self.required_vars)
        # Check if required_vars and partial_vars have overlap
        overlap_vars = required_vars_set & partial_vars_keys
        if overlap_vars:
            raise ValueError(f'required_vars and partial_vars have overlap: {overlap_vars}')

        # Check if required_vars + partial_vars keys exactly equal all template variables
        combined_vars = required_vars_set | partial_vars_keys
        if combined_vars != all_vars:
            missing_vars = all_vars - combined_vars
            extra_vars = combined_vars - all_vars
            error_msg = []
            if missing_vars:
                error_msg.append(f'Missing variables in required_vars or partial_vars: {missing_vars}')
            if extra_vars:
                error_msg.append(f'Extra variables not found in template: {extra_vars}')
            raise ValueError('; '.join(error_msg))

        return self

    def format(self, **kwargs) -> str:
        """Format the prompt template.

Replace placeholders in template with provided variable values, supports function calls for partial variables.

Args:
    **kwargs: Template variable names and corresponding values

Returns:
    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
"""
        # Check if all required variables are provided
        missing_vars = set(self.required_vars) - set(kwargs.keys())
        if missing_vars:
            raise KeyError(f'Missing required variables: {missing_vars}')

        # 1. Apply partial variables. Note: Overrides the values in kwargs if var_name exists in partial_vars
        format_kwargs = {**kwargs}
        for var_name in self.partial_vars:
            try:
                # Apply partial variable function
                val = self.partial_vars[var_name]
                if callable(val):
                    format_kwargs[var_name] = val()
                else:
                    format_kwargs[var_name] = val
            except Exception as e:
                raise TypeError(f'Error applying partial function for variable "{var_name}": {e}')
        # 2. Format the template
        try:
            return self.template.format(**format_kwargs)
        except KeyError as e:
            raise KeyError(f'Template variable not found: {e}')
        except Exception as e:
            raise ValueError(f'Error formatting template: {e}')

    def partial(self, **partial_kwargs) -> 'PromptTemplate':
        """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.

Args:
    **partial_kwargs: Variable names and values to set as partial variables

Returns:
    PromptTemplate: New partially filled template instance

Raises:
    KeyError: When specified variables are not found in template
"""
        # Check if all partial variables exist in the template
        template_vars = set(BasePromptTemplate.get_template_variables(self.template))
        invalid_vars = set(partial_kwargs.keys()) - template_vars
        if invalid_vars:
            raise KeyError(f'Variables not found in template: {invalid_vars}')

        # Create new partial_vars dict with additional partial functions
        new_partial_vars = self.partial_vars.copy()
        new_required_vars = list(set(self.required_vars) - set(partial_kwargs.keys()))
        for var_name, var_value in partial_kwargs.items():
            # Create a function that returns the fixed value
            new_partial_vars[var_name] = var_value

        # Create new template with updated partial_vars
        return PromptTemplate(
            template=self.template,
            required_vars=new_required_vars,
            partial_vars=new_partial_vars
        )

    @classmethod
    def from_template(cls, template: str) -> 'PromptTemplate':
        """Create PromptTemplate instance from template string.

Class method, automatically extracts all variables from template string and sets them as required variables.

Args:
    template (str): Template string containing {variable} placeholders

Returns:
    PromptTemplate: Newly created PromptTemplate instance
"""
        # Extract all variables from template
        all_vars = BasePromptTemplate.get_template_variables(template)
        return cls(
            template=template,
            required_vars=all_vars,
            partial_vars={}
        )

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
    @model_validator(mode='after')
    def validate_variables(self):
        """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
"""
        # 1. All keys in partial_vars must exist in template variables
        # 2. required_vars + partial_vars keys must exactly equal all template variables
        # Extract all variables from template
        all_vars = set(BasePromptTemplate.get_template_variables(self.template))

        # Check if partial_vars.keys() exist in template variables
        partial_vars_keys = set(self.partial_vars.keys())
        invalid_partial_vars = partial_vars_keys - all_vars
        if invalid_partial_vars:
            raise ValueError(f'partial_vars contains variables not found in template: {invalid_partial_vars}')

        required_vars_set = set(self.required_vars)
        # Check if required_vars and partial_vars have overlap
        overlap_vars = required_vars_set & partial_vars_keys
        if overlap_vars:
            raise ValueError(f'required_vars and partial_vars have overlap: {overlap_vars}')

        # Check if required_vars + partial_vars keys exactly equal all template variables
        combined_vars = required_vars_set | partial_vars_keys
        if combined_vars != all_vars:
            missing_vars = all_vars - combined_vars
            extra_vars = combined_vars - all_vars
            error_msg = []
            if missing_vars:
                error_msg.append(f'Missing variables in required_vars or partial_vars: {missing_vars}')
            if extra_vars:
                error_msg.append(f'Extra variables not found in template: {extra_vars}')
            raise ValueError('; '.join(error_msg))

        return self

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
    def format(self, **kwargs) -> str:
        """Format the prompt template.

Replace placeholders in template with provided variable values, supports function calls for partial variables.

Args:
    **kwargs: Template variable names and corresponding values

Returns:
    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
"""
        # Check if all required variables are provided
        missing_vars = set(self.required_vars) - set(kwargs.keys())
        if missing_vars:
            raise KeyError(f'Missing required variables: {missing_vars}')

        # 1. Apply partial variables. Note: Overrides the values in kwargs if var_name exists in partial_vars
        format_kwargs = {**kwargs}
        for var_name in self.partial_vars:
            try:
                # Apply partial variable function
                val = self.partial_vars[var_name]
                if callable(val):
                    format_kwargs[var_name] = val()
                else:
                    format_kwargs[var_name] = val
            except Exception as e:
                raise TypeError(f'Error applying partial function for variable "{var_name}": {e}')
        # 2. Format the template
        try:
            return self.template.format(**format_kwargs)
        except KeyError as e:
            raise KeyError(f'Template variable not found: {e}')
        except Exception as e:
            raise ValueError(f'Error formatting template: {e}')

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
    def partial(self, **partial_kwargs) -> 'PromptTemplate':
        """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.

Args:
    **partial_kwargs: Variable names and values to set as partial variables

Returns:
    PromptTemplate: New partially filled template instance

Raises:
    KeyError: When specified variables are not found in template
"""
        # Check if all partial variables exist in the template
        template_vars = set(BasePromptTemplate.get_template_variables(self.template))
        invalid_vars = set(partial_kwargs.keys()) - template_vars
        if invalid_vars:
            raise KeyError(f'Variables not found in template: {invalid_vars}')

        # Create new partial_vars dict with additional partial functions
        new_partial_vars = self.partial_vars.copy()
        new_required_vars = list(set(self.required_vars) - set(partial_kwargs.keys()))
        for var_name, var_value in partial_kwargs.items():
            # Create a function that returns the fixed value
            new_partial_vars[var_name] = var_value

        # Create new template with updated partial_vars
        return PromptTemplate(
            template=self.template,
            required_vars=new_required_vars,
            partial_vars=new_partial_vars
        )

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
    @classmethod
    def from_template(cls, template: str) -> 'PromptTemplate':
        """Create PromptTemplate instance from template string.

Class method, automatically extracts all variables from template string and sets them as required variables.

Args:
    template (str): Template string containing {variable} placeholders

Returns:
    PromptTemplate: Newly created PromptTemplate instance
"""
        # Extract all variables from template
        all_vars = BasePromptTemplate.get_template_variables(template)
        return cls(
            template=template,
            required_vars=all_vars,
            partial_vars={}
        )

lazyllm.prompt_templates.base.BasePromptTemplate

Bases: BaseModel, ABC

Source code in lazyllm/prompt_templates/base.py
class BasePromptTemplate(BaseModel, ABC):

    @staticmethod
    def get_template_variables(template: str) -> list[str]:
        """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.

Args:
    template (str): A prompt template string containing placeholders


Returns:
    list[str]: A sorted list of placeholder variable names

Raises:
    ValueError: If the template is malformed or parsing fails
"""
        try:
            input_variables = {
                v for _, v, _, _ in Formatter().parse(template) if v is not None
            }
            return sorted(input_variables)
        except Exception as e:
            raise ValueError(f'Error getting template variables: {e}')

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
    @staticmethod
    def get_template_variables(template: str) -> list[str]:
        """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.

Args:
    template (str): A prompt template string containing placeholders


Returns:
    list[str]: A sorted list of placeholder variable names

Raises:
    ValueError: If the template is malformed or parsing fails
"""
        try:
            input_variables = {
                v for _, v, _, _ in Formatter().parse(template) if v is not None
            }
            return sorted(input_variables)
        except Exception as e:
            raise ValueError(f'Error getting template variables: {e}')

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
class FewShotPromptTemplate(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 '\n'
"""
    model_config = ConfigDict(frozen=True)
    prefix: str = Field(..., description='The prefix text that comes before examples, it may include variables')
    suffix: str = Field(..., description='The suffix text that comes after examples, it may include variables')
    examples: List[Dict[str, Any]] = Field(default_factory=list, description='List of example dictionaries')
    egs_prompt_template: PromptTemplate = Field(..., description='Template for formatting each example')
    required_vars: List[str] = Field(
        default_factory=list, description='List of required variable names for the final prompt'
    )
    partial_vars: Dict[str, Any] = Field(
        default_factory=dict, description='Dictionary of partial variable functions or values'
    )
    separator_for_egs: str = Field(default='\n', description='The separator between examples')

    @model_validator(mode='after')
    def validate_variables(self):
        """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.
"""
        # 1. All keys in partial_vars must exist in the combined prefix+suffix variables
        # 2. required_vars + partial_vars keys must exactly equal all template variables
        # 3. Examples must be compatible with egs_prompt_template

        # all variables: variables in prefix + suffix.
        # Note: egs_prompt_template variables are not included here.
        all_vars = self._get_all_variables()

        # Check if partial_vars.keys() exist in template variables
        partial_vars_keys = set(self.partial_vars.keys())
        invalid_partial_vars = partial_vars_keys - all_vars
        if invalid_partial_vars:
            raise ValueError(f'partial_vars contains variables not found in template: {invalid_partial_vars}')

        required_vars_set = set(self.required_vars)
        # Check if required_vars and partial_vars have overlap
        overlap_vars = required_vars_set & partial_vars_keys
        if overlap_vars:
            raise ValueError(f'required_vars and partial_vars have overlap: {overlap_vars}')

        # Check if required_vars + partial_vars keys exactly equal all template variables
        combined_vars = required_vars_set | partial_vars_keys
        if combined_vars != all_vars:
            missing_vars = all_vars - combined_vars
            extra_vars = combined_vars - all_vars
            error_msg = []
            if missing_vars:
                error_msg.append(f'Missing variables in required_vars or partial_vars: {missing_vars}')
            if extra_vars:
                error_msg.append(f'Extra variables not found in template: {extra_vars}')
            raise ValueError('; '.join(error_msg))

        # Check each example is compatible with egs_prompt_template
        if self.examples:
            egs_required_vars = set(self.egs_prompt_template.required_vars)
            for i, example in enumerate(self.examples):
                example_keys = set(example.keys())
                missing_egs_vars = egs_required_vars - example_keys
                if missing_egs_vars:
                    raise ValueError(f'Example {i} missing required variables : {missing_egs_vars}')
        return self

    def format(self, **kwargs) -> str:
        """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.

Args:
    **kwargs: Keyword arguments containing all required_vars

Returns:
    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
"""
        # Check if all required variables are provided
        missing_vars = set(self.required_vars) - set(kwargs.keys())
        if missing_vars:
            raise KeyError(f'Missing required variables: {missing_vars}')

        # 1. Apply partial variables. Note: Overrides the values in kwargs if var_name exists in partial_vars
        format_kwargs = {**kwargs}
        for var_name in self.partial_vars:
            try:
                # Apply partial variable function
                val = self.partial_vars[var_name]
                if callable(val):
                    format_kwargs[var_name] = val()
                else:
                    format_kwargs[var_name] = val
            except Exception as e:
                raise TypeError(f'Error applying partial function for variable "{var_name}": {e}')

        # 2. Format examples
        formatted_examples = []
        sep = self.separator_for_egs
        for example in self.examples:
            try:
                formatted_example = self.egs_prompt_template.format(**example)
                formatted_examples.append(formatted_example)
            except Exception as e:
                raise ValueError(f'Error formatting example {example}: {e}')

        # 3. Combine prefix, examples(already formatted), and suffix
        examples_text = sep.join(formatted_examples)

        # 4. Format the final prompt
        try:
            final_template = f'{self.prefix}\n{examples_text}\n{self.suffix}'
            return final_template.format(**format_kwargs)
        except KeyError as e:
            raise KeyError(f'Template variable not found: {e}')
        except Exception as e:
            raise ValueError(f'Error formatting template: {e}')

    def partial(self, **kwargs) -> 'FewShotPromptTemplate':
        """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.

Args:
    **kwargs: Variable names and values to pre-bind (values can be constants or zero-argument callables)

Returns:
    FewShotPromptTemplate: A new template instance with the specified variables bound

Raises:
    KeyError: If any variable in kwargs is not present in the template
"""
        # Check if all partial variables exist in the template
        all_vars = self._get_all_variables()

        invalid_vars = set(kwargs.keys()) - all_vars
        if invalid_vars:
            raise KeyError(f'Variables not found in template: {invalid_vars}')

        # Create new partial_vars dict with additional partial functions
        new_partial_vars = self.partial_vars.copy()
        new_required_vars = list(set(self.required_vars) - set(kwargs.keys()))
        for var_name, var_value in kwargs.items():
            new_partial_vars[var_name] = var_value

        # Create new template with updated partial_vars
        return FewShotPromptTemplate(
            prefix=self.prefix,
            suffix=self.suffix,
            examples=self.examples.copy(),
            egs_prompt_template=self.egs_prompt_template,
            required_vars=new_required_vars,
            partial_vars=new_partial_vars
        )

    def _get_all_variables(self) -> Set[str]:
        prefix_vars = set(BasePromptTemplate.get_template_variables(self.prefix))
        suffix_vars = set(BasePromptTemplate.get_template_variables(self.suffix))
        return prefix_vars | suffix_vars

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
    @model_validator(mode='after')
    def validate_variables(self):
        """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.
"""
        # 1. All keys in partial_vars must exist in the combined prefix+suffix variables
        # 2. required_vars + partial_vars keys must exactly equal all template variables
        # 3. Examples must be compatible with egs_prompt_template

        # all variables: variables in prefix + suffix.
        # Note: egs_prompt_template variables are not included here.
        all_vars = self._get_all_variables()

        # Check if partial_vars.keys() exist in template variables
        partial_vars_keys = set(self.partial_vars.keys())
        invalid_partial_vars = partial_vars_keys - all_vars
        if invalid_partial_vars:
            raise ValueError(f'partial_vars contains variables not found in template: {invalid_partial_vars}')

        required_vars_set = set(self.required_vars)
        # Check if required_vars and partial_vars have overlap
        overlap_vars = required_vars_set & partial_vars_keys
        if overlap_vars:
            raise ValueError(f'required_vars and partial_vars have overlap: {overlap_vars}')

        # Check if required_vars + partial_vars keys exactly equal all template variables
        combined_vars = required_vars_set | partial_vars_keys
        if combined_vars != all_vars:
            missing_vars = all_vars - combined_vars
            extra_vars = combined_vars - all_vars
            error_msg = []
            if missing_vars:
                error_msg.append(f'Missing variables in required_vars or partial_vars: {missing_vars}')
            if extra_vars:
                error_msg.append(f'Extra variables not found in template: {extra_vars}')
            raise ValueError('; '.join(error_msg))

        # Check each example is compatible with egs_prompt_template
        if self.examples:
            egs_required_vars = set(self.egs_prompt_template.required_vars)
            for i, example in enumerate(self.examples):
                example_keys = set(example.keys())
                missing_egs_vars = egs_required_vars - example_keys
                if missing_egs_vars:
                    raise ValueError(f'Example {i} missing required variables : {missing_egs_vars}')
        return self

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
    def format(self, **kwargs) -> str:
        """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.

Args:
    **kwargs: Keyword arguments containing all required_vars

Returns:
    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
"""
        # Check if all required variables are provided
        missing_vars = set(self.required_vars) - set(kwargs.keys())
        if missing_vars:
            raise KeyError(f'Missing required variables: {missing_vars}')

        # 1. Apply partial variables. Note: Overrides the values in kwargs if var_name exists in partial_vars
        format_kwargs = {**kwargs}
        for var_name in self.partial_vars:
            try:
                # Apply partial variable function
                val = self.partial_vars[var_name]
                if callable(val):
                    format_kwargs[var_name] = val()
                else:
                    format_kwargs[var_name] = val
            except Exception as e:
                raise TypeError(f'Error applying partial function for variable "{var_name}": {e}')

        # 2. Format examples
        formatted_examples = []
        sep = self.separator_for_egs
        for example in self.examples:
            try:
                formatted_example = self.egs_prompt_template.format(**example)
                formatted_examples.append(formatted_example)
            except Exception as e:
                raise ValueError(f'Error formatting example {example}: {e}')

        # 3. Combine prefix, examples(already formatted), and suffix
        examples_text = sep.join(formatted_examples)

        # 4. Format the final prompt
        try:
            final_template = f'{self.prefix}\n{examples_text}\n{self.suffix}'
            return final_template.format(**format_kwargs)
        except KeyError as e:
            raise KeyError(f'Template variable not found: {e}')
        except Exception as e:
            raise ValueError(f'Error formatting template: {e}')

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
    def partial(self, **kwargs) -> 'FewShotPromptTemplate':
        """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.

Args:
    **kwargs: Variable names and values to pre-bind (values can be constants or zero-argument callables)

Returns:
    FewShotPromptTemplate: A new template instance with the specified variables bound

Raises:
    KeyError: If any variable in kwargs is not present in the template
"""
        # Check if all partial variables exist in the template
        all_vars = self._get_all_variables()

        invalid_vars = set(kwargs.keys()) - all_vars
        if invalid_vars:
            raise KeyError(f'Variables not found in template: {invalid_vars}')

        # Create new partial_vars dict with additional partial functions
        new_partial_vars = self.partial_vars.copy()
        new_required_vars = list(set(self.required_vars) - set(kwargs.keys()))
        for var_name, var_value in kwargs.items():
            new_partial_vars[var_name] = var_value

        # Create new template with updated partial_vars
        return FewShotPromptTemplate(
            prefix=self.prefix,
            suffix=self.suffix,
            examples=self.examples.copy(),
            egs_prompt_template=self.egs_prompt_template,
            required_vars=new_required_vars,
            partial_vars=new_partial_vars
        )

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
class LazyLLMPromptLibraryBase(metaclass=LazyLLMRegisterMetaClass):
    """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.
"""
    _prompts = {}
    _default_lang = 'zh'

    def __init__(self, lang=None):
        self.lang = lang
        if self.lang and self.lang not in self.supported_langs:
            LOG.warning(f'Language "{self.lang}" passed to init is not supported. Supported: {self.supported_langs}')

    @property
    def supported_langs(self):
        return list(self._prompts.keys())

    def get_prompt(self, key: str, lang=None):
        """Get the prompt for a given key and language.

Args:
    key (str): The prompt key name.
    lang (str): 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.
"""
        lang = lang or self.lang or self._default_lang
        if lang not in self._prompts:
            raise ValueError(f'Language "{lang}" not supported. Supported: {self.supported_langs}')
        prompt = self._prompts[lang].get(key)
        if prompt is None:
            raise ValueError(f'Prompt for key "{key}" not found in library (lang: {lang}).')
        return prompt

    def get_all_keys(self, lang=None) -> list:
        """List all available prompt keys for a specified language.

Args:
    lang (str): 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.
"""
        if lang is None:
            if self.lang:
                lang = self.lang
                LOG.info(f'get_all_keys: no lang passed, using instance language "{self.lang}"')
            else:
                lang = self._default_lang
                LOG.info(f'get_all_keys: no lang passed, using default language "{self._default_lang}"')

        if lang not in self._prompts:
            LOG.warning(f'Language "{lang}" not supported. Supported: {self.supported_langs}')
            return []
        return list(self._prompts[lang].keys())

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
    def get_all_keys(self, lang=None) -> list:
        """List all available prompt keys for a specified language.

Args:
    lang (str): 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.
"""
        if lang is None:
            if self.lang:
                lang = self.lang
                LOG.info(f'get_all_keys: no lang passed, using instance language "{self.lang}"')
            else:
                lang = self._default_lang
                LOG.info(f'get_all_keys: no lang passed, using default language "{self._default_lang}"')

        if lang not in self._prompts:
            LOG.warning(f'Language "{lang}" not supported. Supported: {self.supported_langs}')
            return []
        return list(self._prompts[lang].keys())

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
    def get_prompt(self, key: str, lang=None):
        """Get the prompt for a given key and language.

Args:
    key (str): The prompt key name.
    lang (str): 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.
"""
        lang = lang or self.lang or self._default_lang
        if lang not in self._prompts:
            raise ValueError(f'Language "{lang}" not supported. Supported: {self.supported_langs}')
        prompt = self._prompts[lang].get(key)
        if prompt is None:
            raise ValueError(f'Prompt for key "{key}" not found in library (lang: {lang}).')
        return prompt

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
class ActorPrompt(LazyLLMPromptLibraryBase):
    """Prompt library module. Contains a wide range of preset prompts, supporting Chinese and English categories, which can be retrieved by act (role) names.

Args:
    lang (str): 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...
    """
    _prompts: dict = {}
    _prompts_paths = [
        ('awesome-chatgpt-prompts-zh.json', 'zh'),
        ('prompts.chat.json', 'en'),
    ]
    _loaded = False
    _load_lock = threading.Lock()
    _default_lang = 'zh'

    def __init__(self, lang=None):
        if not ActorPrompt._loaded:
            with ActorPrompt._load_lock:
                if not ActorPrompt._loaded:  # Double-checked locking
                    self._build_library()
                    ActorPrompt._loaded = True

        super().__init__(lang)

    def _load_prompts(self, path, lang):
        if lang not in self._prompts:
            self._prompts[lang] = {}

        if not os.path.exists(path):
            LOG.warning(f'ActorPrompt file not found: {path}')
            return

        try:
            with open(path, 'r', encoding='utf-8') as f:
                prompts = json.load(f)

            count = 0
            for item in prompts:
                act = item.get('act')
                prompt = item.get('prompt')
                if act and prompt:
                    if act in self._prompts[lang]:
                        LOG.warning(
                            f'Duplicate act "{act}" found in {path} for '
                            f'lang {lang}. Overwriting existing prompt.')
                    self._prompts[lang][act] = prompt
                    count += 1
            LOG.debug(f'Loaded {count} prompts from {path} for lang {lang}')
        except json.JSONDecodeError:
            LOG.error(f'Failed to decode JSON from {path}')
        except Exception as e:
            LOG.error(f'Error loading prompts from {path}: {e}')

    def _build_library(self):
        base_path = os.path.dirname(__file__)
        for rel_path, lang in self._prompts_paths:
            abs_path = os.path.join(base_path, 'prompts_actor', rel_path)
            self._load_prompts(abs_path, lang)
            # Show summary of loaded prompts
            if lang in self._prompts:
                LOG.info(f'After loading {rel_path}, total prompts for lang "{lang}": {len(self._prompts[lang])}')

    def __call__(self, act: str, lang=None, return_raw=False) -> 'Union[ChatPrompter, str]':
        prompt = self.get_prompt(act, lang)  # Get the raw prompt string
        if return_raw:
            return prompt
        return ChatPrompter(prompt)

    def get_all_acts(self, lang=None) -> list:
        """Get the list of all supported acts for a specific language.

Args:
    lang (str): Language code ('zh' or 'en'). If not provided, uses the language set during initialization.

**Returns:**

- list: A list of all available act names.
"""
        return self.get_all_keys(lang)

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
    def get_all_acts(self, lang=None) -> list:
        """Get the list of all supported acts for a specific language.

Args:
    lang (str): Language code ('zh' or 'en'). If not provided, uses the language set during initialization.

**Returns:**

- list: A list of all available act names.
"""
        return self.get_all_keys(lang)

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
class DataPrompt(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
    """
    _prompts: dict = {}
    _default_lang = 'zh'
    _load_lock = threading.Lock()

    def __init__(self, lang=None):
        super().__init__(lang)

    def __call__(self, key: str, lang=None, return_raw=False) -> 'Union[ChatPrompter, str]':
        """Build and return a ChatPrompter for the given key and language, or return the raw prompt dict when return_raw=True.

Args:
    key (str): Prompt key name.
    lang (str): Optional language code.
    return_raw (bool): 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.
"""
        prompt = self.get_prompt(key, lang)  # Get the raw prompt dict
        if return_raw:
            return prompt  # Dict

        prompt = copy.deepcopy(prompt)
        tools = prompt.get('tools')
        history = prompt.get('history')
        extra_keys = prompt.get('extra_keys')
        system = prompt.get('system', '')
        user = prompt.get('user', '')

        return ChatPrompter({'system': system, 'user': user}, tools=tools, history=history, extra_keys=extra_keys)

    @classmethod
    def add_prompt(cls, act, system_prompt=None, user_prompt=None, tools=None, history=None, extra_keys=None, lang='zh'):
        """Add or overwrite a prompt entry in structured form.

Args:
    act (str): Prompt key name.
    system_prompt (str): Optional system message content.
    user_prompt (str): Optional user message content.
    tools (any): Optional tools description/config.
    history (any): Optional history context.
    extra_keys (any): Optional extra fields.
    lang (str): Target language code, default 'zh'.

Note: At least one of system_prompt or user_prompt must be provided.
"""
        assert system_prompt or user_prompt, 'At least one of system_prompt or user_prompt must be provided'
        with cls._load_lock:
            if lang not in cls._prompts:
                cls._prompts[lang] = {}

            if act in cls._prompts[lang]:
                LOG.warning(f'Duplicate act "{act}" found in DataPrompt for lang {lang}. Overwriting.')

            cls._prompts[lang][act] = {
                'system': system_prompt if system_prompt is not None else '',
                'user': user_prompt if user_prompt is not None else '',
                'tools': tools,
                'history': history,
                'extra_keys': extra_keys
            }

__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
    def __call__(self, key: str, lang=None, return_raw=False) -> 'Union[ChatPrompter, str]':
        """Build and return a ChatPrompter for the given key and language, or return the raw prompt dict when return_raw=True.

Args:
    key (str): Prompt key name.
    lang (str): Optional language code.
    return_raw (bool): 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.
"""
        prompt = self.get_prompt(key, lang)  # Get the raw prompt dict
        if return_raw:
            return prompt  # Dict

        prompt = copy.deepcopy(prompt)
        tools = prompt.get('tools')
        history = prompt.get('history')
        extra_keys = prompt.get('extra_keys')
        system = prompt.get('system', '')
        user = prompt.get('user', '')

        return ChatPrompter({'system': system, 'user': user}, tools=tools, history=history, extra_keys=extra_keys)

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.

Source code in lazyllm/prompt_templates/prompt_library.py
    @classmethod
    def add_prompt(cls, act, system_prompt=None, user_prompt=None, tools=None, history=None, extra_keys=None, lang='zh'):
        """Add or overwrite a prompt entry in structured form.

Args:
    act (str): Prompt key name.
    system_prompt (str): Optional system message content.
    user_prompt (str): Optional user message content.
    tools (any): Optional tools description/config.
    history (any): Optional history context.
    extra_keys (any): Optional extra fields.
    lang (str): Target language code, default 'zh'.

Note: At least one of system_prompt or user_prompt must be provided.
"""
        assert system_prompt or user_prompt, 'At least one of system_prompt or user_prompt must be provided'
        with cls._load_lock:
            if lang not in cls._prompts:
                cls._prompts[lang] = {}

            if act in cls._prompts[lang]:
                LOG.warning(f'Duplicate act "{act}" found in DataPrompt for lang {lang}. Overwriting.')

            cls._prompts[lang][act] = {
                'system': system_prompt if system_prompt is not None else '',
                'user': user_prompt if user_prompt is not None else '',
                'tools': tools,
                'history': history,
                'extra_keys': extra_keys
            }