def improve_rule_graph()

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


    def improve_rule_graph(self, task: str, rules: str, option: str) -> str:
        """
        Improves the rule by adding a filter to it.

        :param task: Description of what you would like to do
        :param rules: Rules to improve
        :param option: User or general improvements
        :return: The improved rule
        :raises PiranhaAgentError: If unable to improve the rule
        """

        try:
            chat_interactions = self.create_chats(rules)
        except BaseException as e:
            logger.debug(f"Failed to create chat: {e}")
            raise PiranhaAgentError(str(e)) from e

        if option == "general":
            return self.general_improvement(chat_interactions)

        graph = RawRuleGraph.from_toml(toml.loads(rules))
        for chat in chat_interactions:
            try:
                updated_rules, explanations = self.improve_rules(graph.rules, task, chat)
                return self.validate_improved_rules(updated_rules, explanations)
            except Exception 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))

        logger.debug(f"Unable to improve rule {rules}.")
        raise PiranhaAgentError("Unable to improve rule.")