TrainingCourseGen / components /output_validator.py
aimanzoqi's picture
activities & script content
e8edb00
Raw History Blame Contribute Delete
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
@component
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",
}
@classmethod
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
@component.output_types(
valid_replies=List[str],
invalid_replies=Optional[List[str]],
error_message=Optional[str],
)
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)}