File size: 2,331 Bytes
f02d14f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)}