class_docstr: A component to parse the output of a Generator to `Answer` objects using regular expressions.
docstr: Parse the output of a Generator to `Answer` objects using regular expressions.
  
  :param query: The query used in the prompts for the Generator as a string.
  :param replies: The output of the Generator. A list of strings.
  :param meta: The metadata returned by the Generator. An optional list of dictionaries. If not specified,
                      the generated answer will contain no metadata.
  :param documents: The documents used as input to the Generator. A list of `Document` objects. If
                    `documents` are specified, they are added to the `Answer` objects.
                    If both `documents` and `reference_pattern` are specified, the documents referenced in the
                    Generator output are extracted from the input documents and added to the `Answer` objects.
                    Default: `None`.
  :param pattern: The regular expression pattern to use to extract the answer text from the generator output.
                  If not specified, the whole string is used as the answer. The regular expression can have at
                  most one capture group. If a capture group is present, the text matched by the capture group
                  is used as the answer. If no capture group is present, the whole match is used as the answer.
                  Examples:
                      `[^\n]+$` finds "this is an answer" in a string "this is an argument.\nthis is an answer".
                      `Answer: (.*)` finds "this is an answer" in a string "this is an argument. Answer: this is an answer".
                  Default: `None`.
  :param reference_pattern: The regular expression pattern to use for parsing the document references.
                            We assume that references are specified as indices of the input documents and that
                            indices start at 1.
                            Example: `\[(\d+)\]` finds "1" in a string "this is an answer[1]".
                            If not specified, no parsing is done, and all documents are referenced.
                            Default: `None`.
docstr_tok: ["Parse","the","output","of","a","Generator","to","`","Answer","`","objects","using","regular","expressions",".",":","param","query",":","The","query","used","in","the","prompts","for","the","Generator","as","a","string",".",":","param","replies",":","The","output","of","the","Generator",".","A","list","of","strings",".",":","param","meta",":","The","metadata","returned","by","the","Generator",".","An","optional","list","of","dictionaries",".","If","not","specified",",","the","generated","answer","will","contain","no","metadata",".",":","param","documents",":","The","documents","used","as","input","to","the","Generator",".","A","list","of","`","Document","`","objects",".","If","`","documents","`","are","specified",",","they","are","added","to","the","`","Answer","`","objects",".","If","both","`","documents","`","and","`","reference_pattern","`","are","specified",",","the","documents","referenced","in","the","Generator","output","are","extracted","from","the","input","documents","and","added","to","the","`","Answer","`","objects",".","Default",":","`","None","`",".",":","param","pattern",":","The","regular","expression","pattern","to","use","to","extract","the","answer","text","from","the","generator","output",".","If","not","specified",",","the","whole","string","is","used","as","the","answer",".","The","regular","expression","can","have","at","most","one","capture","group",".","If","a","capture","group","is","present",",","the","text","matched","by","the","capture","group","is","used","as","the","answer",".","If","no","capture","group","is","present",",","the","whole","match","is","used","as","the","answer",".","Examples",":","`","[","^\\n","]","+","$","`","finds","``","this","is","an","answer","''","in","a","string","``","this","is","an","argument.\\nthis","is","an","answer","''",".","`","Answer",":","(",".","*",")","`","finds","``","this","is","an","answer","''","in","a","string","``","this","is","an","argument",".","Answer",":","this","is","an","answer","''",".","Default",":","`","None","`",".",":","param","reference_pattern",":","The","regular","expression","pattern","to","use","for","parsing","the","document","references",".","We","assume","that","references","are","specified","as","indices","of","the","input","documents","and","that","indices","start","at","1",".","Example",":","`","\\","[","(","\\d+",")","\\","]","`","finds","``","1","''","in","a","string","``","this","is","an","answer","[","1","]","''",".","If","not","specified",",","no","parsing","is","done",",","and","all","documents","are","referenced",".","Default",":","`","None","`","."]
code: def run(
      self,
      query: str,
      replies: List[str],
      meta: Optional[List[Dict[str, Any]]] = None,
      documents: Optional[List[Document]] = None,
      pattern: Optional[str] = None,
      reference_pattern: Optional[str] = None,
  ):
      """
      Parse the output of a Generator to `Answer` objects using regular expressions.
  
      :param query: The query used in the prompts for the Generator as a string.
      :param replies: The output of the Generator. A list of strings.
      :param meta: The metadata returned by the Generator. An optional list of dictionaries. If not specified,
                          the generated answer will contain no metadata.
      :param documents: The documents used as input to the Generator. A list of `Document` objects. If
                        `documents` are specified, they are added to the `Answer` objects.
                        If both `documents` and `reference_pattern` are specified, the documents referenced in the
                        Generator output are extracted from the input documents and added to the `Answer` objects.
                        Default: `None`.
      :param pattern: The regular expression pattern to use to extract the answer text from the generator output.
                      If not specified, the whole string is used as the answer. The regular expression can have at
                      most one capture group. If a capture group is present, the text matched by the capture group
                      is used as the answer. If no capture group is present, the whole match is used as the answer.
                      Examples:
                          `[^\\n]+$` finds "this is an answer" in a string "this is an argument.\\nthis is an answer".
                          `Answer: (.*)` finds "this is an answer" in a string "this is an argument. Answer: this is an answer".
                      Default: `None`.
      :param reference_pattern: The regular expression pattern to use for parsing the document references.
                                We assume that references are specified as indices of the input documents and that
                                indices start at 1.
                                Example: `\\[(\\d+)\\]` finds "1" in a string "this is an answer[1]".
                                If not specified, no parsing is done, and all documents are referenced.
                                Default: `None`.
      """
      if not meta:
          meta = [{}] * len(replies)
      elif len(replies) != len(meta):
          raise ValueError(
              f"Number of replies ({len(replies)}), and metadata ({len(meta)}) must match."
          )
  
      if pattern:
          AnswerBuilder._check_num_groups_in_regex(pattern)
  
      pattern = pattern or self.pattern
      reference_pattern = reference_pattern or self.reference_pattern
  
      all_answers = []
      for reply, metadata in zip(replies, meta):
          referenced_docs = []
          if documents:
              reference_idxs = []
              if reference_pattern:
                  reference_idxs = (
                      AnswerBuilder._extract_reference_idxs(
                          reply, reference_pattern
                      )
                  )
              else:
                  reference_idxs = [
                      doc_idx for doc_idx, _ in enumerate(documents)
                  ]
  
              for idx in reference_idxs:
                  try:
                      referenced_docs.append(documents[idx])
                  except IndexError:
                      logger.warning(
                          "Document index '%s' referenced in Generator output is out of range. ",
                          idx + 1,
                      )
  
          answer_string = AnswerBuilder._extract_answer_string(
              reply, pattern
          )
          answer = GeneratedAnswer(
              data=answer_string,
              query=query,
              documents=referenced_docs,
              meta=metadata,
          )
          all_answers.append(answer)
  
      return {"answers": all_answers}
  
file_path: haystack/haystack/components/builders/answer_builder.py
imports: import logging
  import re
  from typing import List, Dict, Any, Optional
  from haystack import component, GeneratedAnswer, Document
label_desc: Use image node_id 2 for calling the AnswerBuilder obj's underlying member method code with example usage: obj.run(query, replies, meta, documents, pattern, reference_pattern) and returns: dict