def get_model_selection()

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


    def get_model_selection(self, task_description: str, options: list) -> str:
        """Send the task description to the chat model and get the course of action the model has chosen.

        :param str task_description: Description of the task to be sent to the model.
        :param list options: List of valid options for the user's response.
        :return: The user's response as returned by the model.
        :rtype: str
        """
        self.chat.append_user_followup(task_description)
        n_tries = 3

        for _ in range(n_tries):
            try:
                completion = self.chat.get_model_response()
                completion = json.loads(completion)
                answer = completion.get("answer")
                if answer not in options:
                    raise ControllerError(
                        f"Invalid answer: {answer}. Expected one of: {', '.join(options)}."
                    )
                return answer
            except json.JSONDecodeError as e:
                self.chat.append_user_followup(
                    f"Error: {e}. Please respond in format: {JSON_FILE_FORMAT}\n"
                    f"Valid 'answer' options: {options}"
                )
        raise ControllerError(f"Failed to get valid answer after {n_tries} tries.")