Skip to main content

ChatPromptBuilder

Use ChatPromptBuilder to send a prompt to a ChatGenerator.

Basic Information​

  • Type: components.builders.chat_prompt_builder.ChatPromptBuilder
  • Components it can connect with:
    • ChatGenerators: ChatPromptBuilder sends instructions to a ChatGenerator.
    • Ranker: ChatPromptBuilder can receive ranked documents from a Ranker and add them to the prompt.

Inputs​

ParameterTypeDefaultDescription
templateOptional[List[ChatMessage]]NoneAn optional list of ChatMessage objects to send to the LLM.
template_variablesOptional[Dict[str, Any]]NoneAn optional dictionary of variables to include in the template.
kwargsAnyPipeline variables used for rendering the prompt.

Outputs​

ParameterTypeDefaultDescription
promptList[ChatMessage]The updated list of ChatMessage objects after rendering the templates.

Overview​

ChatPromptBuilder renders a chat prompt from a template. It constructs prompts using static or dynamic templates, which you can update for each pipeline run.The template is a list of ChatMessage objects provided in the ChatMessage format or as Jinja2 strings.

Template variables in the template are optional unless specified otherwise. If an optional variable isn't provided, it defaults to an empty string. Use variable and required_variables to define input types and required variables.

ChatMessage​

ChatMessage is a data class that represents a message in a chat. It contains the following fields:

  • role: The role of the message. Available roles are system, user, tool,and assistant.
  • content: The content of the message.
  • metadata: The metadata of the message.

In the template parameter, you can pass a list of ChatMessage objects in the following format:

- _content:
- content_type: | #replace this with the content type, supported types are text, image, tool_call, and tool_call_result
Type the prompt here, it may contain Jinja2 variables.
_role: role # supported roles are system, user, tool, and assistant
_metadata:
- key: value # optional metadata

For example:

- _content:
- text: |
You are a helpful assistant answering the user's questions.
If the answer is not in the documents, rely on the web_search tool to find information.
Do not use your own knowledge.
_role: system
- _content:
- text: |
Question: {{ query }}
_role: user

You can also pass the same content as a Jinja2 string using the {% message %} tag:

{% message role="system" %}
You are a helpful assistant answering the user's questions.
If the answer is not in the documents, rely on the web_search tool to find information.
Do not use your own knowledge.
{% endmessage %}

{% message role="user" %}
Question: {{ query }}
{% endmessage %}

Usage Example​

Initializing the Component​

components:
ChatPromptBuilder:
type: components.builders.chat_prompt_builder.ChatPromptBuilder
init_parameters:
template:
- _content:
- text: |
You are a helpful assistant answering the user's questions.
If the answer is not in the documents, rely on the web_search tool to find information.
Do not use your own knowledge.
_role: system

Using the Component in a Pipeline​

Using the ChatMessage Format​

ChatPromptBuilder sends the instructions to a ChatGenerator in the form of a list of ChatMessage objects. You pass the instructions in the template parameter, which must follow the ChatMessage format:

- content: 
- content_type: # replace this with the content type, supported content types are: text, tool_call, tool_call_result
# content may contain variables
role: role # supported roles are: user, system, assistant, tool`

For example:

- _content:
- text: |
You are a helpful assistant answering the user's questions.
If the answer is not in the documents, rely on the web_search tool to find information.
Do not use your own knowledge.
_role: system
- _content:
- text: |
Question: {{ query }}
_role: user

You can also pass documents in the prompt, like this:

- _content:
- text: |
Here are the results that your last search yielded.
{% for doc in documents %}
{{doc.content}}
{% endfor %}

Question: {{ query }}
_role: user

ChatPromptBuilder also supports passing images in prompts using the templatize_part filter:

- _content:
- text: |
What's the difference between the following images?
{% for image in images %}
{{ image | templatize_part }}
{% endfor %}
{% endmessage %}
_role: user

Using the Jinja2 Template Syntax​

You can use Jinja2 strings in ChatPromptBuilder's template parameter through the {% message %} tag. This makes it possible to create structured ChatMessages with mixed content types, such as images and text. For example, this ChatPromptBuilder contains instructions for follow up question classification and rewriting queries at the start of the pipeline. There are two chat messages, one with role "system" and another one with role "assistant". It also includes chat history.

components:
ChatPromptBuilder:
type: haystack.components.builders.chat_prompt_builder.ChatPromptBuilder
init_parameters:
template: |
{% message role="system" %}
You are an excellent labeling tool.
You receive a chat history.
If the last user question is asking for information from a database, output "QUESTION".
This is also the case when something needs to be explained.

If the last user question contains instructions for working with a given passage, output "PASSAGE".
If the chat history is empty, the label "PASSAGE" cannot be used.

Example questions referring to the passage are:
“write a summary of this”, “in bullet points”, or “rephrase as an email”.

For the "QUESTION" label, you additionally output a "query".
The "query" must be in natural language, since it will be processed by a hybrid of keyword and semantic search. Therefore, be as explicit as possible with the keywords.
If both X and Y are asked about, only include Y in the "query" and do not repeat X.
Abbreviations such as “ArbVG”, “BVwG”, or “Abs”, as well as all paragraphs, must remain unchanged and must not be reformulated in the "query".
If the question does not need context from the chat history, output it unchanged in the "query".

For the "PASSAGE" label, you output NOTHING additional.
Simply: "PASSAGE".

{% endmessage %}

{% for message in chat_history %}
{% message role=message.role %}
{{ message.text }}
{% endmessage %}
{% endfor %}

{% message role="assistant" %}
{ "label": "
{% endmessage %}

Parameters​

Init Parameters​

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
templateOptional[List[ChatMessage]]NoneA list of ChatMessage objects. The component looks for Jinja2 template syntax and renders the prompt with the provided variables.
required_variablesOptional[Union[List[str], Literal['*']]]NoneA list of variables that ChatPromptBuilder must receive as input. If a variable listed as required is not provided, an exception is raised. If set to "*", all variables in the prompt are required.
variablesOptional[List[str]]NoneA list of input variables to use in prompt templates instead of the ones inferred from the template parameter. For example, to use more variables during prompt engineering than the ones present in the default template, you can provide them here.

Run Method Parameters​

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
templateOptional[List[ChatMessage]]NoneAn optional list of ChatMessage objects to overwrite ChatPromptBuilder's default template. If None, the default template provided at initialization is used.
template_variablesOptional[Dict[str, Any]]NoneAn optional dictionary of template variables to overwrite the pipeline variables.
kwargsAnyPipeline variables used for rendering the prompt.