output_parsers

Submodules


class YamlOutputParser(data_class: DataClass, examples: List[DataClass] = None, include_fields: List[str] | Dict[str, List[str]] | None = None, exclude_fields: List[str] | Dict[str, List[str]] | None = None, return_data_class: bool = False)[source]

Bases: OutputParser

YAML output parser using dataclass for schema extraction.

Note

Only use yaml for simple dataclass objects. For complex objects, use JSON.

Parameters:
  • data_class (Type) – The dataclass to extract the schema for the YAML output.

  • example (Type, optional) – The example dataclass object to show in the prompt. Defaults to None.

  • yaml_output_format_template (str, optional) – The template for the YAML output format. Defaults to YAML_OUTPUT_FORMAT.

  • output_processors (Component, optional) – The output processors to parse the YAML string to JSON object. Defaults to YamlParser().

Examples:

>>> from prompts.outputs import YamlOutputParser
>>> from dataclasses import dataclass, field
>>> from typing import List
>>>
>>> @dataclass
>>> class ThoughtAction:
>>>     thought: str = field(metadata={"description": "Reasoning behind the answer"}) # required field
>>>     answer: str = field(metadata={"description": "Your answer to the question"}, default=None) # optional field
>>>
>>> # If you want to parse it back to the dataclass, you can add a from_dict method to the dataclass
>>> # def from_dict(self, d: Dict[str, Any]) -> "ThoughtAction":
>>> #     return ThoughtAction(**d)
>>>
>>> yaml_parser = YamlOutputParser(data_class_for_yaml=ThoughtAction)
>>> yaml_format_instructions = yaml_parser.format_instructions()
>>> print(yaml_format_instructions)
>>> yaml_str = '''The output should be formatted as a standard YAML instance with the following JSON schema:
>>> ```
>>> 'thought': {'type': 'str', 'description': 'Reasoning behind the answer', 'required': True}, 'answer': {'type': 'str', 'description': '
>>> Your answer to the question', 'required': False, 'default': None}
>>> ```
>>> -Make sure to always enclose the YAML output in triple backticks (```). Please do not add anything other than valid YAML output!
>>> -Follow the YAML formatting conventions with an indent of 2 spaces.
>>> '''
>>> # use it in the generator
>>> task_desc_str = "You are a helpful assistant who answers user query. "+yaml_format_instructions
>>> generator = Generator(output_processors=yaml_parser, ..., preset_prompt_kwargs={"task_desc_str": task_desc_str})
>>> generator("Should i be a doctor?")
format_instructions(format_type: DataClassFormatType | None = None) str[source]

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

Parameters:
  • format_type (DataClassFormatType, optional) – The format type to show in the prompt. Defaults to DataClassFormatType.SIGNATURE_YAML for less token usage. Options: DataClassFormatType.SIGNATURE_YAML, DataClassFormatType.SIGNATURE_JSON, DataClassFormatType.SCHEMA.

  • exclude (List[str], optional) – The fields to exclude from the schema of the data class.

call(input: str) Dict[str, Any][source]

Parse the YAML string to JSON object and return the JSON object.

class JsonOutputParser(data_class: DataClass, examples: List[DataClass] = None, include_fields: List[str] | Dict[str, List[str]] | None = None, exclude_fields: List[str] | Dict[str, List[str]] | None = None, return_data_class: bool = False)[source]

Bases: OutputParser

format_instructions(format_type: DataClassFormatType | None = None) str[source]

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

Parameters:

format_type (DataClassFormatType, optional) – The format type to show in the prompt. Defaults to DataClassFormatType.SIGNATURE_JSON for less token usage compared with DataClassFormatType.SCHEMA. Options: DataClassFormatType.SIGNATURE_YAML, DataClassFormatType.SIGNATURE_JSON, DataClassFormatType.SCHEMA.

call(input: str) Any[source]

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

class ListOutputParser(list_output_format_template: str = 'Your output should be formatted as a standard Python list.\n- Start the list with \'[\' and end with \']\'\n- DO NOT mistaken the "properties" and "type" in the schema as the actual fields in the list output.\n')[source]

Bases: OutputParser

List output parser to parse list of objects from the string.

format_instructions() str[source]

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

call(input: str) list[source]

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

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.