def general_improvement()

in experimental/piranha_playground/rule_inference/piranha_agent.py [0:0]


    def general_improvement(self, chat_interactions: List[PiranhaGPTChat]) -> str:
        """
        Find a rule generated by chat models that complies with the user specified example.

        :param chat_interactions: List of chat sessions for the inference engine to use
        :type chat_interactions: List[PiranhaGPTChat]
        :return: The rule inferred from GPT
        :rtype: str
        :raises PiranhaAgentError: If the agent fails to generate a rule after 10 rounds of interaction with GPT-4.
        """
        max_rounds = 10
        for i in range(max_rounds):
            for chat in chat_interactions:
                try:
                    completion = chat.get_model_response()
                    _, toml_block, explanation = self.validate_rule(completion)
                    self.explanation = explanation
                    return toml_block
                except PiranhaAgentError as e:
                    logger.debug(
                        f"GPT-4 failed to generate a rule. Following up the next round with {e}. Trying again...\n"
                    )
                    chat.append_user_followup(str(e))
                except PiranhaChatException as e:
                    logger.debug(
                        f"Chat completion failed with {e}. Trying again with a new chat...\n"
                    )
        raise PiranhaAgentError(
            f"Failed to generate a rule after {max_rounds} rounds of interaction with GPT-4.",
        )