class_docstr: DynamicPromptBuilder is designed to construct dynamic prompts for the pipeline. Users can change the prompt
  template at runtime by providing a new template for each pipeline run invocation if needed.
  
  The following example demonstrates how to use the DynamicPromptBuilder:
  
  ```python
  from typing import List
  from haystack.components.builders import DynamicPromptBuilder
  from haystack.components.generators import OpenAIGenerator
  from haystack import Pipeline, component, Document
  
  prompt_builder = DynamicPromptBuilder(runtime_variables=["documents"])
  llm = OpenAIGenerator(api_key="<your-api-key>", model="gpt-3.5-turbo")
  
  
  @component
  class DocumentProducer:
  
      @component.output_types(documents=List[Document])
      def run(self, doc_input: str):
          return {"documents": [Document(content=doc_input)]}
  
  
  pipe = Pipeline()
  pipe.add_component("doc_producer", DocumentProducer())
  pipe.add_component("prompt_builder", prompt_builder)
  pipe.add_component("llm", llm)
  pipe.connect("doc_producer.documents", "prompt_builder.documents")
  pipe.connect("prompt_builder.prompt", "llm.prompt")
  
  template = "Here is the document: {{documents[0].content}} \n Answer: {{query}}"
  result = pipe.run(
      data={
          "doc_producer": {"doc_input": "Hello world, I live in Berlin"},
          "prompt_builder": {
              "prompt_source": template,
              "template_variables": {"query": "Where does the speaker live?"},
          },
      }
  )
  print(result)
  
  >> {'llm': {'replies': ['The speaker lives in Berlin.'],
  >> 'meta': [{'model': 'gpt-3.5-turbo-0613',
  >> 'index': 0,
  >> 'finish_reason': 'stop',
  >> 'usage': {'prompt_tokens': 28,
  >> 'completion_tokens': 6,
  >> 'total_tokens': 34}}]}}
  
  Note how in the example above, we can dynamically change the prompt template by providing a new template to the
  run method of the pipeline. This dynamic prompt generation is in stark contrast to the static prompt generation
  using `PromptBuilder`, where the prompt template is fixed for the pipeline's lifetime and cannot be changed
  for each pipeline run invocation.
docstr: Executes the dynamic prompt building process. Depending on the provided type of `prompt_source`, this method
  either processes a list of `ChatMessage` instances or a string template. In the case 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. For a string template, it directly applies the template variables to
  render the final prompt. You can provide additional template variables directly to this method, that are then
  merged with the variables resolved from the pipeline runtime.
  
  :param prompt_source: A string template.
  :type prompt_source: str
  
  :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, Union[List[ChatMessage], str]]
docstr_tok: ["Executes","the","dynamic","prompt","building","process",".","Depending","on","the","provided","type","of","`","prompt_source","`",",","this","method","either","processes","a","list","of","`","ChatMessage","`","instances","or","a","string","template",".","In","the","case","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",".","For","a","string","template",",","it","directly","applies","the","template","variables","to","render","the","final","prompt",".","You","can","provide","additional","template","variables","directly","to","this","method",",","that","are","then","merged","with","the","variables","resolved","from","the","pipeline","runtime",".",":","param","prompt_source",":","A","string","template",".",":","type","prompt_source",":","str",":","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",",","Union","[","List","[","ChatMessage","]",",","str","]","]"]
code: def run(
      self,
      prompt_source: str,
      template_variables: Optional[Dict[str, Any]] = None,
      **kwargs,
  ):
      """
      Executes the dynamic prompt building process. Depending on the provided type of `prompt_source`, this method
      either processes a list of `ChatMessage` instances or a string template. In the case 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. For a string template, it directly applies the template variables to
      render the final prompt. You can provide additional template variables directly to this method, that are then
      merged with the variables resolved from the pipeline runtime.
  
      :param prompt_source: A string template.
      :type prompt_source: str
  
      :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, Union[List[ChatMessage], str]]
      """
      kwargs = kwargs or {}
      template_variables = template_variables or {}
      template_variables_combined = {**kwargs, **template_variables}
      if not template_variables_combined:
          raise ValueError(
              "The DynamicPromptBuilder run method requires template variables, but none were provided. "
              "Please provide an appropriate template variable to enable prompt generation."
          )
  
      template = self._validate_template(
          prompt_source, set(template_variables_combined.keys())
      )
      result = template.render(template_variables_combined)
      return {"prompt": result}
  
imports: import logging
  from typing import Dict, Any, Optional, List, Set
  from jinja2 import Template, meta
  from haystack import component
label_desc: Use image node_id 2 for calling the DynamicPromptBuilder obj's underlying member method code with example usage: obj.run(prompt_source, template_variables) and returns: dict