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: Component

This is similar to Dspy’s signature but more controllable and flexible.

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.