Download components/output_validator.py from ZoqiAndrewSeniorProject/TrainingCourseGen: direct link, hf CLI and curl.
- Browser
- Download file 2.33 kB
-
https://huggingface.co/spaces/ZoqiAndrewSeniorProject/TrainingCourseGen/resolve/main/components/output_validator.py
- Command line
-
hf download hf://spaces/ZoqiAndrewSeniorProject/TrainingCourseGen/components/output_validator.py
-
curl -L -o output_validator.py https://huggingface.co/spaces/ZoqiAndrewSeniorProject/TrainingCourseGen/resolve/main/components/output_validator.py
2.33 kB
| import json | |
| from haystack.core.serialization import import_class_by_name | |
| import pydantic | |
| from pydantic import ValidationError | |
| from typing import Optional, List | |
| from colorama import Fore | |
| from haystack import component | |
| """ | |
| output_validator.py | |
| Purpose: Ensures that a JSON output generated by an LLM model is formatted correctly. | |
| Copied from https://haystack.deepset.ai/tutorials/28_structured_output_with_loop ! | |
| ; | |
| """ | |
| # Define the component input parameters | |
| class OutputValidator: | |
| def __init__(self, pydantic_model: type[pydantic.BaseModel]): | |
| self.pydantic_model = pydantic_model | |
| self.iteration_counter = 0 | |
| def to_dict(self): | |
| return { | |
| "pydantic_model": f"{self.pydantic_model.__module__}.{self.pydantic_model.__name__}", | |
| "type": "components.output_validator.OutputValidator", | |
| } | |
| def from_dict(cls, data): | |
| print(Fore.YELLOW + "Inside from_dict...") | |
| model_class = import_class_by_name(data["pydantic_model"]) | |
| return cls( | |
| pydantic_model=model_class, | |
| ) | |
| # Define the component output | |
| def run(self, body: str): | |
| self.iteration_counter += 1 | |
| ## Try to parse the LLM's reply ## | |
| # If the LLM's reply is a valid object, return `"valid_replies"` | |
| try: | |
| output_dict = json.loads(body) | |
| self.pydantic_model.model_validate(output_dict) | |
| print( | |
| Fore.GREEN | |
| + f"OutputValidator at Iteration {self.iteration_counter}: Valid JSON from LLM - No need for looping: {body}" | |
| ) | |
| return {"valid_replies": body} | |
| # If the LLM's reply is corrupted or not valid, return "invalid_replies" and the "error_message" for LLM to try again | |
| except (ValueError, ValidationError) as e: | |
| print( | |
| Fore.RED | |
| + f"OutputValidator at Iteration {self.iteration_counter}: Invalid JSON from LLM - Let's try again.\n" | |
| f"Output from LLM:\n {body} \n" | |
| f"Error from OutputValidator: {e}" | |
| ) | |
| return {"invalid_replies": body, "error_message": str(e)} | |