数据类解析器#
DataClassParser 将帮助用户与 LLMs 进行交互,比使用 JsonOutputParser 和 YamlOutputParser 与 DataClass 更好。
类
|
与JsonOutputParser和YamlOutputParser相比,使结构化输出更加简单。 |
- class DataClassParser(data_class: DataClass, return_data_class: bool = False, format_type: Literal['yaml', 'json'] = 'json')[source]#
基础:
Component与JsonOutputParser和YamlOutputParser相比,使结构化输出更加简单。
理解来自 DataClass 的 __input_fields__ 和 __output_fields__(无需使用 include/exclude 来决定字段)。
用户可以选择将task_desc保存在DataClass中,并在提示中使用它。
示例:
@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, )