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
dbally.prompts.prompt_builder.PromptBuilder
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:
|
RAISES | DESCRIPTION |
---|---|
OSError
|
If model_name is not found in huggingface.co/models |
Source code in src/dbally/prompts/prompt_builder.py
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:
|
fmt |
formatting dict
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ChatFormat
|
ChatFormat formatted prompt |
Source code in src/dbally/prompts/prompt_builder.py
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:
|
fmt |
Dictionary with formatting.
TYPE:
|
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. |