outputs#
The most commonly used output parsers for the Generator.
Classes
Boolean output parser to parse boolean values from the string. |
|
|
|
|
List output parser to parse list of objects from the string. |
|
The abstract class for all output parsers. |
|
YAML output parser using dataclass for schema extraction. |
- class OutputParser(*args, **kwargs)[source]#
Bases:
Component
The abstract class for all output parsers.
This interface helps users customize output parsers with consistent interfaces for the Generator. Even though you don’t always need to subclass it.
LightRAG uses two core components: 1. the Prompt to format output instruction 2. A string parser component from core.string_parser for response parsing.
- 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.
- 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.
- 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.
- class BooleanOutputParser[source]#
Bases:
OutputParser
Boolean output parser to parse boolean values from the string.