跳到内容

Pydantic

输出解析器.

PydanticOutputParser #

基类:ChainableOutputParser, Generic[Model]

Pydantic 输出解析器。

参数

名称 类型 描述 默认值
output_cls BaseModel

Pydantic 输出类。

required
源代码位于 llama-index-core/llama_index/core/output_parsers/pydantic.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class PydanticOutputParser(ChainableOutputParser, Generic[Model]):
    """
    Pydantic Output Parser.

    Args:
        output_cls (BaseModel): Pydantic output class.

    """

    def __init__(
        self,
        output_cls: Type[Model],
        excluded_schema_keys_from_format: Optional[List] = None,
        pydantic_format_tmpl: str = PYDANTIC_FORMAT_TMPL,
    ) -> None:
        """Init params."""
        self._output_cls = output_cls
        self._excluded_schema_keys_from_format = excluded_schema_keys_from_format or []
        self._pydantic_format_tmpl = pydantic_format_tmpl

    @property
    def output_cls(self) -> Type[Model]:
        return self._output_cls

    @property
    def format_string(self) -> str:
        """Format string."""
        return self.get_format_string(escape_json=True)

    def get_format_string(self, escape_json: bool = True) -> str:
        """Format string."""
        schema_dict = self._output_cls.model_json_schema()
        for key in self._excluded_schema_keys_from_format:
            del schema_dict[key]

        schema_str = json.dumps(schema_dict)
        output_str = self._pydantic_format_tmpl.format(schema=schema_str)
        if escape_json:
            return output_str.replace("{", "{{").replace("}", "}}")
        else:
            return output_str

    def parse(self, text: str) -> Any:
        """Parse, validate, and correct errors programmatically."""
        json_str = extract_json_str(text)
        return self._output_cls.model_validate_json(json_str)

    def format(self, query: str) -> str:
        """Format a query with structured output formatting instructions."""
        return query + "\n\n" + self.get_format_string(escape_json=True)

format_string 属性 #

format_string: str

格式化字符串。

get_format_string #

get_format_string(escape_json: bool = True) -> str

格式化字符串。

源代码位于 llama-index-core/llama_index/core/output_parsers/pydantic.py
47
48
49
50
51
52
53
54
55
56
57
58
def get_format_string(self, escape_json: bool = True) -> str:
    """Format string."""
    schema_dict = self._output_cls.model_json_schema()
    for key in self._excluded_schema_keys_from_format:
        del schema_dict[key]

    schema_str = json.dumps(schema_dict)
    output_str = self._pydantic_format_tmpl.format(schema=schema_str)
    if escape_json:
        return output_str.replace("{", "{{").replace("}", "}}")
    else:
        return output_str

parse #

parse(text: str) -> Any

以编程方式解析、验证并纠正错误。

源代码位于 llama-index-core/llama_index/core/output_parsers/pydantic.py
60
61
62
63
def parse(self, text: str) -> Any:
    """Parse, validate, and correct errors programmatically."""
    json_str = extract_json_str(text)
    return self._output_cls.model_validate_json(json_str)

format #

format(query: str) -> str

使用结构化输出格式化指令格式化查询。

源代码位于 llama-index-core/llama_index/core/output_parsers/pydantic.py
65
66
67
def format(self, query: str) -> str:
    """Format a query with structured output formatting instructions."""
    return query + "\n\n" + self.get_format_string(escape_json=True)