def _generate_suggestions()

in bugbug/tools/code_review.py [0:0]


    def _generate_suggestions(self, patch: Patch):
        formatted_patch = format_patch_set(patch.patch_set)
        if formatted_patch == "":
            return None

        output_summarization = self.summarization_chain.invoke(
            {"patch": formatted_patch},
            return_only_outputs=True,
        )["text"]

        if self.verbose:
            GenerativeModelTool._print_answer(output_summarization)

        if self.function_search is not None:
            line_code_list = self.further_context_chain.run(
                patch=formatted_patch, summarization=output_summarization
            ).split("\n")

            if self.verbose:
                GenerativeModelTool._print_answer(line_code_list)

            requested_context_lines = request_for_context_lines(
                self.function_search,
                patch.base_commit_hash,
                line_code_list,
                formatted_patch,
            )

            function_list = [
                function_name.strip()
                for function_name in self.further_info_chain.run(
                    patch=formatted_patch, summarization=output_summarization
                ).split("\n")
            ]

            if self.verbose:
                GenerativeModelTool._print_answer(function_list)

            requested_functions = request_for_function_declarations(
                self.function_search,
                patch.base_commit_hash,
                function_list,
                patch.patch_set,
            )

        output = ""
        for comment_gen_llm in self.comment_gen_llms:
            memory = ConversationBufferMemory()
            conversation_chain = ConversationChain(
                llm=comment_gen_llm,
                memory=memory,
                verbose=self.verbose,
            )

            experience_scope = (
                f"the {self.target_software} source code"
                if self.target_software
                else "a software project"
            )
            memory.save_context(
                {
                    "input": f"You are an expert reviewer for {experience_scope}, with experience on source code reviews."
                },
                {
                    "output": f"Sure, I'm aware of source code practices in {self.target_software or 'the development community'}."
                },
            )
            memory.save_context(
                {
                    "input": 'Please, analyze the code provided and report a summarization about the new changes; for that, focus on the code added represented by lines that start with "+".\n'
                    + patch.raw_diff
                },
                {"output": output_summarization},
            )

            if self.function_search is not None and len(requested_functions) > 0:
                function_declaration_text = get_structured_functions(
                    "Function Name", requested_functions
                )

                memory.save_context(
                    {
                        "input": "Attached, you can find some function definitions that are used in the current patch and might be useful to you to have more context about the code under analysis. These functions already exist in the codebase before the patch, and can't be modified. "
                        + function_declaration_text
                    },
                    {
                        "output": "Okay, I will consider the provided function definitions as additional context to the given patch."
                    },
                )

            if self.function_search is not None and len(requested_context_lines) > 0:
                context_text = get_structured_functions(
                    "Requested Context for Line", requested_context_lines
                )

                memory.save_context(
                    {
                        "input": "Attached, you can also have more context of the target code under analysis."
                        + context_text
                    },
                    {
                        "output": "Okay, I will also consider the code as additional context to the given patch."
                    },
                )

            created_before = patch.date_created if self.is_experiment_env else None

            cur_output = conversation_chain.predict(
                input=PROMPT_TEMPLATE_REVIEW.format(
                    patch=formatted_patch,
                    comment_examples=self._get_comment_examples(patch, created_before),
                    approved_examples=self._get_generated_examples(
                        patch, created_before
                    ),
                    target_code_consistency=self.target_software or "rest of the",
                )
            )
            output += cur_output

            if self.verbose:
                GenerativeModelTool._print_answer(cur_output)

            memory.clear()

        if len(self.comment_gen_llms) > 1:
            output = self.deduplicating_chain.invoke(
                {"review": output},
                return_only_outputs=True,
            )["text"]

            if self.verbose:
                GenerativeModelTool._print_answer(output)

        return output