class_docstr:     DynamicChatPromptBuilder is designed to construct dynamic prompts from a list of `ChatMessage` instances. It
      integrates with Jinja2 templating for dynamic prompt generation.
  
      DynamicChatPromptBuilder assumes that the last user message in the list contains a template and renders it with resolved
      pipeline variables and any additional template variables provided.
  
      You can provide additional template variables directly to the pipeline `run` method. They are then merged with the
      variables resolved from the pipeline runtime. This allows for greater flexibility and customization of the
      generated prompts based on runtime conditions and user inputs.
  
      The following example demonstrates how to use DynamicChatPromptBuilder to generate a chat prompt:
  
      ```python
      from haystack.components.builders import DynamicChatPromptBuilder
      from haystack.components.generators.chat import OpenAIChatGenerator
      from haystack.dataclasses import ChatMessage
      from haystack import Pipeline
  
      # no parameter init, we don't use any runtime template variables
      prompt_builder = DynamicChatPromptBuilder()
      llm = OpenAIChatGenerator(api_key="<your-api-key>", model="gpt-3.5-turbo")
  
      pipe = Pipeline()
      pipe.add_component("prompt_builder", prompt_builder)
      pipe.add_component("llm", llm)
      pipe.connect("prompt_builder.prompt", "llm.messages")
  
      location = "Berlin"
      system_message = ChatMessage.from_system("You are a helpful assistant giving out valuable information to tourists.")
      messages = [system_message, ChatMessage.from_user("Tell me about {{location}}")]
  
  
      res = pipe.run(data={"prompt_builder": {"template_variables": {"location": location}, "prompt_source": messages}})
      print(res)
  
      >> {'llm': {'replies': [ChatMessage(content="Berlin is the capital city of Germany and one of the most vibrant
      and diverse cities in Europe. Here are some key things to know...Enjoy your time exploring the vibrant and dynamic
      capital of Germany!", role=<ChatRole.ASSISTANT: 'assistant'>, name=None, meta={'model': 'gpt-3.5-turbo-0613',
      'index': 0, 'finish_reason': 'stop', 'usage': {'prompt_tokens': 27, 'completion_tokens': 681, 'total_tokens':
      708}})]}}
  
  
      messages = [system_message, ChatMessage.from_user("What's the weather forecast for {{location}} in the next
      {{day_count}} days?")]
  
      res = pipe.run(data={"prompt_builder": {"template_variables": {"location": location, "day_count": "5"},
                                          "prompt_source": messages}})
  
      print(res)
      >> {'llm': {'replies': [ChatMessage(content="Here is the weather forecast for Berlin in the next 5
      days:
  
  Day 1: Mostly cloudy with a high of 22°C (72°F) and...so it's always a good idea to check for updates
      closer to your visit.", role=<ChatRole.ASSISTANT: 'assistant'>, name=None, meta={'model': 'gpt-3.5-turbo-0613',
      'index': 0, 'finish_reason': 'stop', 'usage': {'prompt_tokens': 37, 'completion_tokens': 201,
      'total_tokens': 238}})]}}
      ```
  
      The primary advantage of using DynamicChatPromptBuilder is showcased in the examples provided above.
      DynamicChatPromptBuilder allows dynamic customization of prompt messages without the need to reload or recreate the
      pipeline for each invocation.
  
      In the example above, the first query asks for general information about Berlin, and the second query requests
      the weather forecast for Berlin in the next few days. DynamicChatPromptBuilder efficiently handles these distinct
      prompt structures by adjusting pipeline run parameters invocations, as opposed to a regular PromptBuilder, which
      would require recreating or reloading the pipeline for each distinct type of query, leading to inefficiency and
      potential service disruptions, especially in server environments where continuous service is vital.
  
      Note that the weather forecast in the example above is fictional, but it can be easily connected to a weather
      API to provide real weather forecasts.
      
docstr: Executes the dynamic prompt building process by processing a list of `ChatMessage` instances.
  The last user message is treated as a template and rendered with the resolved pipeline variables and any
  additional template variables provided.
  
  You can provide additional template variables directly to this method, which are then merged with the variables
  resolved from the pipeline runtime.
  
  :param prompt_source: A list of `ChatMessage` instances. We make an assumption that the last user message has
  the template for the chat prompt
  :type prompt_source: List[ChatMessage]
  
  :param template_variables: An optional dictionary of template variables. Template variables provided at
  initialization are required to resolve pipeline variables, and these are additional variables users can
  provide directly to this method.
  :type template_variables: Optional[Dict[str, Any]]
  
  :param kwargs: Additional keyword arguments, typically resolved from a pipeline, which are merged with the
  provided template variables.
  
  :return: A dictionary containing the key "prompt", which holds either the updated list of `ChatMessage`
  instances or the rendered string template, forming the complete dynamic prompt.
  :rtype: Dict[str, List[ChatMessage]
docstr_tok: ["Executes","the","dynamic","prompt","building","process","by","processing","a","list","of","`","ChatMessage","`","instances",".","The","last","user","message","is","treated","as","a","template","and","rendered","with","the","resolved","pipeline","variables","and","any","additional","template","variables","provided",".","You","can","provide","additional","template","variables","directly","to","this","method",",","which","are","then","merged","with","the","variables","resolved","from","the","pipeline","runtime",".",":","param","prompt_source",":","A","list","of","`","ChatMessage","`","instances",".","We","make","an","assumption","that","the","last","user","message","has","the","template","for","the","chat","prompt",":","type","prompt_source",":","List","[","ChatMessage","]",":","param","template_variables",":","An","optional","dictionary","of","template","variables",".","Template","variables","provided","at","initialization","are","required","to","resolve","pipeline","variables",",","and","these","are","additional","variables","users","can","provide","directly","to","this","method",".",":","type","template_variables",":","Optional","[","Dict","[","str",",","Any","]","]",":","param","kwargs",":","Additional","keyword","arguments",",","typically","resolved","from","a","pipeline",",","which","are","merged","with","the","provided","template","variables",".",":","return",":","A","dictionary","containing","the","key","``","prompt","''",",","which","holds","either","the","updated","list","of","`","ChatMessage","`","instances","or","the","rendered","string","template",",","forming","the","complete","dynamic","prompt",".",":","rtype",":","Dict","[","str",",","List","[","ChatMessage","]"]
code: def run(
      self,
      prompt_source: List[ChatMessage],
      template_variables: Optional[Dict[str, Any]] = None,
      **kwargs,
  ):
      """
      Executes the dynamic prompt building process by processing a list of `ChatMessage` instances.
      The last user message is treated as a template and rendered with the resolved pipeline variables and any
      additional template variables provided.
  
      You can provide additional template variables directly to this method, which are then merged with the variables
      resolved from the pipeline runtime.
  
      :param prompt_source: A list of `ChatMessage` instances. We make an assumption that the last user message has
      the template for the chat prompt
      :type prompt_source: List[ChatMessage]
  
      :param template_variables: An optional dictionary of template variables. Template variables provided at
      initialization are required to resolve pipeline variables, and these are additional variables users can
      provide directly to this method.
      :type template_variables: Optional[Dict[str, Any]]
  
      :param kwargs: Additional keyword arguments, typically resolved from a pipeline, which are merged with the
      provided template variables.
  
      :return: A dictionary containing the key "prompt", which holds either the updated list of `ChatMessage`
      instances or the rendered string template, forming the complete dynamic prompt.
      :rtype: Dict[str, List[ChatMessage]
      """
      kwargs = kwargs or {}
      template_variables = template_variables or {}
      template_variables_combined = {**kwargs, **template_variables}
      if not template_variables_combined:
          logger.warning(
              "The DynamicChatPromptBuilder run method requires template variables, but none were provided. "
              "Please provide an appropriate template variable to enable correct prompt generation."
          )
      result: List[ChatMessage] = self._process_chat_messages(
          prompt_source, template_variables_combined
      )
      return {"prompt": result}
  
file_path: haystack/haystack/components/builders/dynamic_chat_prompt_builder.py
imports: import logging
  from typing import Dict, Any, Optional, List, Set
  from jinja2 import Template, meta
  from haystack import component
  from haystack.dataclasses.chat_message import ChatMessage, ChatRole
label_desc: Use image node_id 2 for calling the DynamicChatPromptBuilder obj's underlying member method code with example usage: obj.run(prompt_source, template_variables) and returns: dict