llama_index [问题]:OpenAPIToolSpec - RecursionError:在处理大型OpenAPISpec yaml文件时超过了最大递归深度,

egdjgwm8  于 2个月前  发布在  其他
关注(0)|答案(3)|浏览(37)

问题验证

  • 我已在文档和Discord中寻找答案。

问题

如何使OpenAPIToolSpec在处理大型开放API规范YAML文件时高效工作?
将递归限制设置得更高似乎不是这里的可行解决方案 - sys.setrecursionlimit(15000)

oyt4ldly

oyt4ldly1#

嘿,@vaibhavtupe!我在这里帮助你解决任何bug、问题或贡献。我是一个机器人,旨在协助你在等待人类维护者时提供帮助。
要使用OpenAPIToolSpec高效处理大型OpenAPI规范YAML文件,而不会遇到RecursionError,你可以尝试以下方法:

  1. 增加递归限制:虽然你提到设置更高的递归限制不可行,但仍然值得注意的是,这是一个常见的解决方案。然而,如果这对你不起作用,请继续进行下一步。
  2. 使用迭代解析:与其依赖于递归解析,你可以修改dereference_openapi函数以使用迭代方法。这有助于避免触及递归限制。
    以下是如何修改dereference_openapi函数以使用迭代方法的示例:
def dereference_openapi(openapi_doc):
    """Dereferences a Swagger/OpenAPI document by resolving all $ref pointers."""
    try:
        import jsonschema
    except ImportError:
        raise ImportError(
            "The jsonschema library is required to parse OpenAPI documents. "
            "Please install it with `pip install jsonschema`."
        )

    resolver = jsonschema.RefResolver.from_schema(openapi_doc)
    stack = [openapi_doc]

    while stack:
        obj = stack.pop()
        if isinstance(obj, dict):
            if "$ref" in obj:
                with resolver.resolving(obj["$ref"]) as resolved:
                    stack.append(resolved)
            else:
                stack.extend(obj.values())
        elif isinstance(obj, list):
            stack.extend(obj)

    return openapi_doc
  1. 优化OpenAPI规范:简化并减少OpenAPI规范,以创建更简洁、高效的表示。这可以通过关注关键细节并删除不必要的信息来实现。
    以下是如何优化OpenAPI规范的示例:
def process_api_spec(self, spec: dict) -> dict:
    """Perform simplification and reduction on an OpenAPI specification.

The goal is to create a more concise and efficient representation
for retrieval purposes.
"""
    def reduce_details(details: dict) -> dict:
        reduced = {}
        if details.get("description"):
            reduced["description"] = details.get("description")
        if details.get("parameters"):
            reduced["parameters"] = [
                param
                for param in details.get("parameters", [])
                if param.get("required")
            ]
        if "200" in details["responses"]:
            reduced["responses"] = details["responses"]["200"]
        return reduced

    spec = dereference_openapi(spec)
    endpoints = []
    for route, operations in spec["paths"].items():
        for operation, details in operations.items():
            if operation in ["get", "post", "patch"]:
                endpoint_name = f"{operation.upper()} {route}"
                description = details.get("description")
                endpoints.append(
                    (endpoint_name, description, reduce_details(details))
                )

    return {
        "servers": spec["servers"],
        "description": spec["info"].get("description"),
        "endpoints": endpoints,
    }

通过使用迭代方法进行反引用和优化OpenAPI规范,你可以更有效地处理大型文件,而不会触及递归错误1(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/tools/llama-index-tools-openapi/llama_index/tools/openapi/base.py)。

  • 要继续与Dosu交谈,请提及@dosu。*
qgzx9mmu

qgzx9mmu2#

如果你能分享一下规格,那可能会有助于调试。

fcg9iug3

fcg9iug33#

以下是文本内容的翻译结果:

@logan-markewich 这里是API规范: https://app.swaggerhub.com/apis/equinix-api/fabric/4.14
增加递归限制会使代码无限运行。[无限递归]

相关问题