radicalxdev / kai-ai-backend

This is the Kai Teaching Assistant ai repo.
MIT License
12 stars 46 forks source link

Quiz Generate Validation #30

Closed mikhailocampo closed 2 weeks ago

mikhailocampo commented 1 month ago
def validate_response(self, response: Dict) -> bool:
        try:
            # Assuming the response is already a dictionary
            if isinstance(response, dict):
                if 'question' in response and 'choices' in response and 'answer' in response and 'explanation' in response:
                    choices = response['choices']
                    if isinstance(choices, dict):
                        for key, value in choices.items():
                            if not isinstance(key, str) or not isinstance(value, str):
                                return False
                        return True
            return False
        except TypeError as e:
            if self.verbose:
                logger.error(f"TypeError during response validation: {e}")
            return False

    def format_choices(self, choices: Dict[str, str]) -> List[Dict[str, str]]:
        return [{"key": k, "value": v} for k, v in choices.items()]

image The above error comes from the validate_response in which we are destructuring the response from the model. We need a way to remove the backticks JSON and then continue with the rest of the validation as normal

AaronSosaRamos commented 1 month ago

Please, can you specify more the WARNING - Invalid response format? Because it is not always the backticks. image

mikhailocampo commented 3 weeks ago

image

AaronSosaRamos commented 3 weeks ago

@mikhailocampo It works normally in my local environment: image image

AaronSosaRamos commented 3 weeks ago

@mikhailocampo Have you cloned PR #31 with the last commits?