Skip to content

PromptTemplate

PromptTemplate

This class embeds task inputs from GenerationInstance or MultipleChoiceInstance into a text that can be used as a prompt for LanguageModel.

Source code in flexeval/core/prompt_template/base.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
class PromptTemplate(ABC):
    """
    This class embeds task inputs from `GenerationInstance` or `MultipleChoiceInstance` into a text that can be used
    as a prompt for `LanguageModel`.
    """

    @abstractmethod
    def embed_inputs(self, input_dict: dict[str, Any]) -> str:
        """
        Embeds the input into a prompt template.
        """
        raise NotImplementedError

embed_inputs abstractmethod

embed_inputs(input_dict: dict[str, Any]) -> str

Embeds the input into a prompt template.

Source code in flexeval/core/prompt_template/base.py
13
14
15
16
17
18
@abstractmethod
def embed_inputs(self, input_dict: dict[str, Any]) -> str:
    """
    Embeds the input into a prompt template.
    """
    raise NotImplementedError

Jinja2PromptTemplate

Embed task inputs using Jinja2 template engine.

Parameters:

  • template (str | None, default: None ) –

    The Jinja2 template to use.

  • template_path (str | None, default: None ) –

    The path to a file with the Jinja2 template to use.

Source code in flexeval/core/prompt_template/jinja2.py
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
class Jinja2PromptTemplate(PromptTemplate):
    """
    Embed task inputs using Jinja2 template engine.

    Args:
        template: The Jinja2 template to use.
        template_path: The path to a file with the Jinja2 template to use.
    """

    def __init__(self, template: str | None = None, template_path: str | None = None) -> None:
        if template is None and template_path is None:
            msg = "Either template or template_path must be provided"
            raise ValueError(msg)
        if template is not None and template_path is not None:
            msg = "Only one of template or template_path can be provided"
            raise ValueError(msg)

        if template_path is not None:
            with open(template_path) as f:
                self.template = f.read()
        else:
            self.template = template

        self.compiled_template = JINJA2_ENV.from_string(self.template)

    def embed_inputs(self, input_dict: dict[str, Any]) -> str:
        return self.compiled_template.render(input_dict)

    def __repr__(self) -> str:
        return f"Jinja2PromptTemplate(template={self.template!r})"

template instance-attribute

template = read()

compiled_template instance-attribute

compiled_template = from_string(template)

__init__

__init__(
    template: str | None = None,
    template_path: str | None = None,
) -> None
Source code in flexeval/core/prompt_template/jinja2.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def __init__(self, template: str | None = None, template_path: str | None = None) -> None:
    if template is None and template_path is None:
        msg = "Either template or template_path must be provided"
        raise ValueError(msg)
    if template is not None and template_path is not None:
        msg = "Only one of template or template_path can be provided"
        raise ValueError(msg)

    if template_path is not None:
        with open(template_path) as f:
            self.template = f.read()
    else:
        self.template = template

    self.compiled_template = JINJA2_ENV.from_string(self.template)

embed_inputs

embed_inputs(input_dict: dict[str, Any]) -> str
Source code in flexeval/core/prompt_template/jinja2.py
43
44
def embed_inputs(self, input_dict: dict[str, Any]) -> str:
    return self.compiled_template.render(input_dict)

__repr__

__repr__() -> str
Source code in flexeval/core/prompt_template/jinja2.py
46
47
def __repr__(self) -> str:
    return f"Jinja2PromptTemplate(template={self.template!r})"

instantiate_prompt_template_from_string

instantiate_prompt_template_from_string(
    template_or_path: str,
) -> Jinja2PromptTemplate
Source code in flexeval/core/prompt_template/jinja2.py
11
12
13
14
15
def instantiate_prompt_template_from_string(template_or_path: str) -> Jinja2PromptTemplate:
    # Use `os.path.isfile` instead of `Path.is_file()` to avoid "OSError: [Errno 36] File name too long"
    if os.path.isfile(template_or_path):  # noqa: PTH113
        return Jinja2PromptTemplate(template_path=template_or_path)
    return Jinja2PromptTemplate(template=template_or_path)