dataclass_parser

DataClassParser will help users interact with LLMs even better than JsonOutputParser and YamlOutputParser with DataClass.

Classes

DataClassParser(data_class[, ...])

Made the structured output even simpler compared with JsonOutputParser and YamlOutputParser.

class DataClassParser(data_class: DataClass, return_data_class: bool = False, format_type: Literal['yaml', 'json'] = 'json')[source]

Bases: DataComponent

Made the structured output even simpler compared with JsonOutputParser and YamlOutputParser.

  1. Understands __input_fields__ and __output_fields__ from the DataClass (no need to use include/exclude to decide fields).

  2. User can choose to save the task_desc in the DataClass and use it in the prompt.

Example:

@dataclass
class BasicQAOutput(adal.DataClass):
    explanation: str = field(
        metadata={"desc": "A brief explanation of the concept in one sentence."}
    )
    example: str = field(
        metadata={"desc": "An example of the concept in a sentence."}
    )
    # Control output fields order
    __output_fields__ = ["explanation", "example"]

# Define the template using jinja2 syntax
qa_template = "<SYS>
You are a helpful assistant.
<OUTPUT_FORMAT>
{{output_format_str}}
</OUTPUT_FORMAT>
</SYS>
<USER> {{input_str}} </USER>"

parser = adal.DataClassParser(data_class=BasicQAOutput, return_data_class=True)

# Set up the generator with model, template, and parser
self.generator = adal.Generator(
    model_client=model_client,
    model_kwargs=model_kwargs,
    template=qa_template,
    prompt_kwargs={"output_format_str": parser.get_output_format_str()},
    output_processors=parser,
)
get_input_format_str() str[source]

Return the formatted instructions to use in prompt for the input format.

get_output_format_str() str[source]

Return the formatted instructions to use in prompt for the output format.

get_input_str(input: DataClass) str[source]

Return the formatted input string.

get_task_desc_str() str[source]

Return the task description string.

get_examples_str(examples: List[DataClass], include: List[str] | Dict[str, List[str]] | None = None, exclude: List[str] | Dict[str, List[str]] | None = None) str[source]

Return the examples string.

call(input: str) Any[source]

Parse the output string to the desired format and return the parsed output.