Skip to content

Openapi

OpenAPIReader #

Bases: BaseReader

OpenAPI读取器。

读取OpenAPI规范,提供解析它们的选项。

Parameters:

Name Type Description Default
depth Optional[int]

在拆分JSON之前潜入的深度。

1
exclude Optional[List[str]]

要排除的JSON路径,用'.'分隔。例如:'components.pets'将从OpenAPI规范中排除组件'pets'。用于从OpenAPI规范中删除不需要的信息。

None

Returns:

Type Description

List[Document]: 文档列表。

Source code in llama_index/readers/openapi/base.py
11
12
13
14
15
16
17
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class OpenAPIReader(BaseReader):
    """OpenAPI读取器。

    读取OpenAPI规范,提供解析它们的选项。

    Args:
        depth (Optional[int]): 在拆分JSON之前潜入的深度。
        exclude (Optional[List[str]]): 要排除的JSON路径,用'.'分隔。例如:'components.pets'将从OpenAPI规范中排除组件'pets'。用于从OpenAPI规范中删除不需要的信息。

    Returns:
        List[Document]: 文档列表。"""

    def __init__(
        self, depth: Optional[int] = 1, exclude: Optional[List[str]] = None
    ) -> None:
        super().__init__()
        self.exclude = exclude
        self.depth = depth

    @classmethod
    def class_name(cls) -> str:
        """获取类的名称标识符。"""
        return "OpenAPIReader"

    def _should_exclude(self, path: str) -> bool:
        """检查路径是否应该被排除。"""
        return self.exclude and any(
            re.match(exclude_path, path) for exclude_path in self.exclude
        )

    def _build_docs_from_attributes(
        self,
        key: str,
        value: Any,
        extra_info: Dict,
        path: str = "$",
        level: int = 0,
    ) -> List[Document]:
        """从OAS JSON的属性构建文档。"""
        if not path and self._should_exclude(path):
            return []

        if self.depth == level or not isinstance(value, dict):
            return [
                Document(
                    text=f"{key}: {value}", metadata={"json_path": path, **extra_info}
                )
            ]

        return [
            doc
            for k, v in value.items()
            for doc in self._build_docs_from_attributes(
                k, v, extra_info, f"{path}.{key}", level + 1
            )
        ]

    def load_data(
        self, input_file: str, extra_info: Optional[Dict] = {}
    ) -> List[Document]:
        """从输入文件中加载数据。"""
        try:
            with open(input_file, encoding="utf-8") as f:
                data = json.load(f)
        except json.JSONDecodeError:
            raise ValueError(f"The file {input_file} is not a valid JSON file.")

        return [
            doc
            for key, value in data.items()
            for doc in self._build_docs_from_attributes(key, value, extra_info)
        ]

class_name classmethod #

class_name() -> str

获取类的名称标识符。

Source code in llama_index/readers/openapi/base.py
30
31
32
33
@classmethod
def class_name(cls) -> str:
    """获取类的名称标识符。"""
    return "OpenAPIReader"

load_data #

load_data(
    input_file: str, extra_info: Optional[Dict] = {}
) -> List[Document]

从输入文件中加载数据。

Source code in llama_index/readers/openapi/base.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def load_data(
    self, input_file: str, extra_info: Optional[Dict] = {}
) -> List[Document]:
    """从输入文件中加载数据。"""
    try:
        with open(input_file, encoding="utf-8") as f:
            data = json.load(f)
    except json.JSONDecodeError:
        raise ValueError(f"The file {input_file} is not a valid JSON file.")

    return [
        doc
        for key, value in data.items()
        for doc in self._build_docs_from_attributes(key, value, extra_info)
    ]