Skip to content

PromptBuilder

PromptBuilder is an abstract class designed to facilitate the construction of prompts for interaction with LLMs.

Lifecycle

The class is initialized with an optional argument model_name, representing the name of the model for which a tokenizer should be loaded.

Tip

To use OpenAIs' models, you do not need to provide the model_name argument because tokenization is performed inside the OpenAI API.

The main method used to build prompts is self.build. It takes a prompt_template (a ChatFormat object) and a formatting dictionary fmt as input. It formats the prompt using the format_prompt method. If a tokenizer is available (i.e., if model_name was provided during initialization), it applies the tokenizer to the formatted prompt. The method returns the formatted prompt, either as a string (if it was formatted for a Hugging Face model) or as a ChatFormat.

dbally.data_models.prompts.common_validation_utils.ChatFormat module-attribute

ChatFormat = Tuple[Dict[str, str], ...]

dbally.prompts.prompt_builder.PromptBuilder

PromptBuilder(model_name: Optional[str] = None)

Class used to build prompts

PARAMETER DESCRIPTION
model_name

Name of the model to load a tokenizer for. Tokenizer is used to append special tokens to the prompt. If empty, no tokens will be added.

TYPE: Optional[str] DEFAULT: None

RAISES DESCRIPTION
OSError

If model_name is not found in huggingface.co/models

Source code in src/dbally/prompts/prompt_builder.py
def __init__(self, model_name: Optional[str] = None) -> None:
    """
    Args:
        model_name: Name of the model to load a tokenizer for.
                    Tokenizer is used to append special tokens to the prompt. If empty, no tokens will be added.

    Raises:
        OSError: If model_name is not found in huggingface.co/models
    """
    self._tokenizer: Optional["PreTrainedTokenizer"] = None

    if model_name is not None and not model_name.startswith("gpt"):
        try:
            from transformers import AutoTokenizer  # pylint: disable=import-outside-toplevel
        except ImportError as exc:
            raise ImportError("You need to install transformers package to use huggingface models.") from exc

        self._tokenizer = AutoTokenizer.from_pretrained(model_name)

format_prompt

format_prompt(prompt_template: PromptTemplate, fmt: Dict[str, str]) -> ChatFormat

Format prompt using provided arguments

PARAMETER DESCRIPTION
prompt_template

this template will be modified in place

TYPE: PromptTemplate

fmt

formatting dict

TYPE: Dict[str, str]

RETURNS DESCRIPTION
ChatFormat

ChatFormat formatted prompt

Source code in src/dbally/prompts/prompt_builder.py
def format_prompt(self, prompt_template: PromptTemplate, fmt: Dict[str, str]) -> ChatFormat:
    """
    Format prompt using provided arguments

    Args:
        prompt_template: this template will be modified in place
        fmt: formatting dict

    Returns:
        ChatFormat formatted prompt
    """
    return tuple({**msg, "content": msg["content"].format(**fmt)} for msg in prompt_template.chat)

build

build(prompt_template: PromptTemplate, fmt: Dict[str, str]) -> Union[str, ChatFormat]

Build the prompt

PARAMETER DESCRIPTION
prompt_template

Prompt template in system/user/assistant openAI format.

TYPE: PromptTemplate

fmt

Dictionary with formatting.

TYPE: Dict[str, str]

RETURNS DESCRIPTION
Union[str, ChatFormat]

Either prompt as a string (if it was formatted for a hf model, model_name provided), or prompt as an

Union[str, ChatFormat]

openAI client style list.

RAISES DESCRIPTION
KeyError

If fmt does not fill all template arguments.

Source code in src/dbally/prompts/prompt_builder.py
def build(self, prompt_template: PromptTemplate, fmt: Dict[str, str]) -> Union[str, ChatFormat]:
    """Build the prompt

    Args:
        prompt_template: Prompt template in system/user/assistant openAI format.
        fmt: Dictionary with formatting.

    Returns:
        Either prompt as a string (if it was formatted for a hf model, model_name provided), or prompt as an
        openAI client style list.

    Raises:
        KeyError: If fmt does not fill all template arguments.
    """

    prompt = self.format_prompt(prompt_template, fmt)
    if self._tokenizer is not None:
        prompt = self._tokenizer.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)
    return prompt