def extract_examples_frm_response()

in 3_optimization-design-ptn/03_prompt-optimization/promptwizard/glue/promptopt/techniques/critique_n_refine/core_logic.py [0:0]


    def extract_examples_frm_response(self, response_with_examples: str) -> List:
        """
        Extract the elements that constitute an example in dataset viz question, reasoning for answer and the answer.
        Put these elements to list and return.

        :param response_with_examples: Response of LLM which has synthetic examples.
        :return: A list of synthetic examples
        """
        #
        synthetic_examples = []
        parsed_data = re.findall(
            DatasetSpecificProcessing.TEXT_DELIMITER_PATTERN,
            response_with_examples,
            re.DOTALL,
        )
        parsed_data = [s.strip() for s in parsed_data]

        for text in parsed_data:
            # Splitting text into question, reason, and answer
            if (
                DatasetSpecificProcessing.QUESTION_KEY_IN_PROMPT in text
                and DatasetSpecificProcessing.ANSWER_KEY_IN_PROMPT in text
            ):
                question = text[
                    text.find(DatasetSpecificProcessing.QUESTION_KEY_IN_PROMPT)
                    + len(DatasetSpecificProcessing.QUESTION_KEY_IN_PROMPT) : text.find(
                        DatasetSpecificProcessing.ANSWER_KEY_IN_PROMPT
                    )
                ].strip()
                answer_with_reason = text[
                    text.find(DatasetSpecificProcessing.ANSWER_KEY_IN_PROMPT)
                    + len(DatasetSpecificProcessing.ANSWER_KEY_IN_PROMPT) :
                ].strip()

                if self.data_processor != None:
                    final_answer = self.data_processor.extract_final_answer(
                        answer_with_reason
                    )
                else:
                    final_answer = extract_between(
                        text=answer_with_reason, start="<ANS_START>", end="<ANS_END>"
                    )

                formatted_data = {
                    DatasetSpecificProcessing.QUESTION_LITERAL: question,
                    DatasetSpecificProcessing.ANSWER_WITH_REASON_LITERAL: answer_with_reason,
                    DatasetSpecificProcessing.FINAL_ANSWER_LITERAL: final_answer,
                }

                synthetic_examples.append(formatted_data)

        return synthetic_examples