Skip to content

Prompt Builder

Modules concerning the prompt builders used in Gen AI applications.

AgnosticPromptBuilder(system_template='', user_template='', separator='\n', ignore_extra_keys=False)

Bases: BasePromptBuilder

A prompt builder that is agnostic to specific model types and formats prompts with a customizable separator.

The AgnosticPromptBuilder class constructs a prompt by joining the content of the prompt templates using a specified separator. It is designed to work independently of specific model types.

Attributes:

Name Type Description
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}.

user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}.

prompt_key_set set[str]

A set of expected keys that must be present in the prompt templates.

ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt.

formatter AgnosticPromptFormatter

The formatter to be used to format the prompt into a string in the format_as_string method.

Initializes a new instance of the AgnosticPromptBuilder class.

Parameters:

Name Type Description Default
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
separator str

A string used to separate each prompt template's content. Defaults to "\n".

'\n'
ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt. Defaults to False.

False

HuggingFacePromptBuilder(system_template='', user_template='', model_name_or_path='', ignore_extra_keys=False)

Bases: BasePromptBuilder

A prompt builder for Hugging Face models that support chat templating.

The HuggingFacePromptBuilder class is designed to create prompts compatible with models hosted in Hugging Face, given that the model support chat templating. It loads the appropriate tokenizer and formats the prompt by injecting placeholder values.

Attributes:

Name Type Description
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}.

user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}.

prompt_key_set set[str]

A set of expected keys that must be present in the prompt templates.

ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt.

formatter HuggingFacePromptFormatter

The formatter to be used to format the prompt into a string in the format_as_string method.

Note

If you're trying to access the prompt builder template of a gated model, you'd need to: 1. Request access to the gated repo using your HuggingFace account. 2. Login to HuggingFace in your system. This can be done as follows: 2.1. Install huggingface-hub: pip install huggingface-hub 2.2. Login to HuggingFace: huggingface-cli login 2.3. Enter your HuggingFace token.

Initializes a new instance of the HuggingFacePromptBuilder class.

Parameters:

Name Type Description Default
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
model_name_or_path str

The model name or path of the Hugging Face model tokenizer to be loaded. Defaults to an empty string.

''
ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt. Defaults to False.

False

LlamaPromptBuilder(system_template, user_template, model_name='Meta-Llama-3.1-8B-Instruct', ignore_extra_keys=False)

Bases: HuggingFacePromptBuilder

A prompt builder for Llama models, ensuring compatibility and formatting of prompts.

The LlamaPromptBuilder class is designed to create prompts compatible with specific Llama models. It loads the appropriate tokenizer and formats the prompt by injecting placeholder values.

Attributes:

Name Type Description
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}.

user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}.

prompt_key_set set[str]

A set of expected keys that must be present in the prompt templates.

ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt.

formatter HuggingFacePromptFormatter

The formatter to be used to format the prompt into a string in the format_as_string method.

Note

If you're trying to access the prompt builder template of a gated model, you'd need to: 1. Request access to the gated repo using your HuggingFace account. 2. Login to HuggingFace in your system. This can be done as follows: 2.1. Install huggingface-hub: pip install huggingface-hub 2.2. Login to HuggingFace: huggingface-cli login 2.3. Enter your HuggingFace token.

Initializes a new instance of the LlamaPromptBuilder class.

Parameters:

Name Type Description Default
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

required
user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

required
model_name str

The name of the Llama model tokenizer to be loaded. Defaults to Meta-Llama-3.1-8B-Instruct.

'Meta-Llama-3.1-8B-Instruct'
ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt. Defaults to False.

False

MistralPromptBuilder(system_template='', user_template='', model_name='Mistral-7B-Instruct-v0.3', ignore_extra_keys=False)

Bases: HuggingFacePromptBuilder

A prompt builder for Mistral models, ensuring compatibility and formatting of prompts.

The MistralPromptBuilder class is designed to create prompts compatible with specific Mistral models. It loads the appropriate tokenizer and formats the prompt by injecting placeholder values.

Attributes:

Name Type Description
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}.

user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}.

prompt_key_set set[str]

A set of expected keys that must be present in the prompt templates.

ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt.

formatter HuggingFacePromptFormatter

The formatter to be used to format the prompt into a string in the format_as_string method.

Note

If you're trying to access the prompt builder template of a gated model, you'd need to: 1. Request access to the gated repo using your HuggingFace account. 2. Login to HuggingFace in your system. This can be done as follows: 2.1. Install huggingface-hub: pip install huggingface-hub 2.2. Login to HuggingFace: huggingface-cli login 2.3. Enter your HuggingFace token.

Initializes a new instance of the MistralPromptBuilder class.

Parameters:

Name Type Description Default
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
model_name str

The name of the Mistral model tokenizer to be loaded. Defaults to Mistral-7B-Instruct-v0.3.

'Mistral-7B-Instruct-v0.3'
ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt. Defaults to False.

False

OpenAIPromptBuilder(system_template='', user_template='', ignore_extra_keys=False)

Bases: BasePromptBuilder

A prompt builder for OpenAI models, ensuring compatibility and formatting of prompts.

The OpenAIPromptBuilder class is designed to create prompts compatible with specific OpenAI models. It formats the prompt templates by prefixing each template with its role (converted to title case) and concatenating them into a single prompt string.

Attributes:

Name Type Description
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}.

user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}.

prompt_key_set set[str]

A set of expected keys that must be present in the prompt templates.

ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt.

formatter OpenAIPromptFormatter

The formatter to be used to format the prompt into a string in the format_as_string method.

Initializes a new instance of the OpenAIPromptBuilder class.

Parameters:

Name Type Description Default
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt. Defaults to False.

False

PromptBuilder(system_template='', user_template='', ignore_extra_keys=False)

A prompt builder class used in Gen AI applications.

Attributes:

Name Type Description
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}.

user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}.

prompt_key_set set[str]

A set of expected keys that must be present in the prompt templates.

ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt.

Initializes a new instance of the PromptBuilder class.

Parameters:

Name Type Description Default
system_template str

The system prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
user_template str

The user prompt template. May contain placeholders enclosed in curly braces {}. Defaults to an empty string.

''
ignore_extra_keys bool

Whether to ignore extra keys when formatting the prompt. Defaults to False.

False

Raises:

Type Description
ValueError

If both system_template and user_template are empty.

format(history=None, extra_contents=None, attachments=None, **kwargs)

Formats the prompt templates into a MultimodalPrompt.

This method processes each prompt template, replacing the placeholders in the template content with the corresponding values from kwargs. If any required key is missing from kwargs, it raises a ValueError. It also handles the provided history and extra contents. It formats the prompt as a MultimodalPrompt.

Parameters:

Name Type Description Default
history MultimodalPrompt | None

The optional history to be included in the prompt. Defaults to None.

None
extra_contents list[MultimodalContent] | None

The optional extra contents to be included in the user message. Defaults to None.

None
attachments list[Attachment] | None

Deprecated parameter to handle attachments. Will be removed in v0.5.0. Defaults to None.

None
**kwargs Any

A dictionary of placeholder values to be injected into the prompt templates. Values must be either a string or an object that can be serialized to a string.

{}

Returns:

Name Type Description
MultimodalPrompt MultimodalPrompt

A multimodal prompt.

Raises:

Type Description
ValueError

If a required key for the prompt template is missing from kwargs.