Files
semantic-kernel/python/semantic_kernel/connectors/openapi_plugin/openapi_parser.py
T
Evan Mattson 51675d2607 Python: OpenAPI plugin updates to promote plugin to preview (#9670)
### Motivation and Context

We currently handle OpenAPI plugins via the kernel, but in a pretty
simple way. The changes in this PR are allowing one to define multiple
server paths for an operation as well as configure security on the
operations either at the operation level or at the "global" spec level
(these are included as metadata in the kernel function parameter
`additional_parameters` dict).

The OpenAPI plugin is graduating from an experimental state to a preview
state. This, in turn, removes the `experimental` tag from some public
facing methods. In the near future, all `experimental` tags will be
removed once we're confident there are no other underlying changes
required.

Closes #9719 

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

Adds support for OpenAPI plugin:
- allow for multiple servers for an API operation
- allows one to define security configuration at the spec level as well
as per operation level
- fixes a bug where the `excluded_operations` on the execution settings
were never checked during REST API operation building
- adds the deprecated tag to the `OpenAI plugin` code as it was
deprecated by OpenAI and we never handled it in SK Python.
- renames some model class to remove the `operation` portion of the
name.
- adds ability to specify a callback to be able to skip certain api
operations based on name, method, or description
- removes the experimental tag from public facing methods.
- allows one to pass in a parsed OpenAPI spec to the plugin add method
- adds unit tests 

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
2024-11-21 15:27:32 +00:00

282 lines
12 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import logging
from collections import OrderedDict
from collections.abc import Generator
from typing import TYPE_CHECKING, Any, Final
from prance import ResolvingParser
from semantic_kernel.connectors.openapi_plugin.models.rest_api_expected_response import (
RestApiExpectedResponse,
)
from semantic_kernel.connectors.openapi_plugin.models.rest_api_operation import RestApiOperation
from semantic_kernel.connectors.openapi_plugin.models.rest_api_parameter import RestApiParameter
from semantic_kernel.connectors.openapi_plugin.models.rest_api_parameter_location import (
RestApiParameterLocation,
)
from semantic_kernel.connectors.openapi_plugin.models.rest_api_payload import RestApiPayload
from semantic_kernel.connectors.openapi_plugin.models.rest_api_payload_property import (
RestApiPayloadProperty,
)
from semantic_kernel.connectors.openapi_plugin.models.rest_api_security_requirement import RestApiSecurityRequirement
from semantic_kernel.connectors.openapi_plugin.models.rest_api_security_scheme import RestApiSecurityScheme
from semantic_kernel.exceptions.function_exceptions import PluginInitializationError
if TYPE_CHECKING:
from semantic_kernel.connectors.openai_plugin.openai_function_execution_parameters import (
OpenAIFunctionExecutionParameters,
)
from semantic_kernel.connectors.openapi_plugin.openapi_function_execution_parameters import (
OpenAPIFunctionExecutionParameters,
)
logger: logging.Logger = logging.getLogger(__name__)
class OpenApiParser:
"""NOTE: SK Python only supports the OpenAPI Spec >=3.0.
Import an OpenAPI file.
Args:
openapi_file: The path to the OpenAPI file which can be local or a URL.
Returns:
The parsed OpenAPI file
:param openapi_file: The path to the OpenAPI file which can be local or a URL.
:return: The parsed OpenAPI file
"""
PAYLOAD_PROPERTIES_HIERARCHY_MAX_DEPTH: int = 10
SUPPORTED_MEDIA_TYPES: Final[list[str]] = ["application/json", "text/plain"]
def parse(self, openapi_document: str) -> Any | dict[str, Any] | None:
"""Parse the OpenAPI document."""
parser = ResolvingParser(openapi_document)
return parser.specification
def _parse_parameters(self, parameters: list[dict[str, Any]]):
"""Parse the parameters from the OpenAPI document."""
result: list[RestApiParameter] = []
for param in parameters:
name: str = param["name"]
if not param.get("in"):
raise PluginInitializationError(f"Parameter {name} is missing 'in' field")
if param.get("content", None) is not None:
# The schema and content fields are mutually exclusive.
raise PluginInitializationError(f"Parameter {name} cannot have a 'content' field. Expected: schema.")
location = RestApiParameterLocation(param["in"])
description: str = param.get("description", None)
is_required: bool = param.get("required", False)
default_value = param.get("default", None)
schema: dict[str, Any] | None = param.get("schema", None)
result.append(
RestApiParameter(
name=name,
type=schema.get("type", "string") if schema else "string",
location=location,
description=description,
is_required=is_required,
default_value=default_value,
schema=schema if schema else {"type": "string"},
)
)
return result
def _get_payload_properties(self, operation_id, schema, required_properties, level=0):
if schema is None:
return []
if level > OpenApiParser.PAYLOAD_PROPERTIES_HIERARCHY_MAX_DEPTH:
raise Exception(
f"Max level {OpenApiParser.PAYLOAD_PROPERTIES_HIERARCHY_MAX_DEPTH} of "
f"traversing payload properties of `{operation_id}` operation is exceeded."
)
result = []
for property_name, property_schema in schema.get("properties", {}).items():
default_value = property_schema.get("default", None)
property = RestApiPayloadProperty(
name=property_name,
type=property_schema.get("type", None),
is_required=property_name in required_properties,
properties=self._get_payload_properties(operation_id, property_schema, required_properties, level + 1),
description=property_schema.get("description", None),
schema=property_schema,
default_value=default_value,
)
result.append(property)
return result
def _create_rest_api_operation_payload(
self, operation_id: str, request_body: dict[str, Any]
) -> RestApiPayload | None:
if request_body is None or request_body.get("content") is None:
return None
content = request_body.get("content")
if content is None:
return None
media_type = next((mt for mt in OpenApiParser.SUPPORTED_MEDIA_TYPES if mt in content), None)
if media_type is None:
raise Exception(f"Neither of the media types of {operation_id} is supported.")
media_type_metadata = content[media_type]
payload_properties = self._get_payload_properties(
operation_id, media_type_metadata["schema"], media_type_metadata["schema"].get("required", set())
)
return RestApiPayload(
media_type,
payload_properties,
request_body.get("description"),
schema=media_type_metadata.get("schema", None),
)
def _create_response(self, responses: dict[str, Any]) -> Generator[tuple[str, RestApiExpectedResponse], None, None]:
for response_key, response_value in responses.items():
media_type = next(
(mt for mt in OpenApiParser.SUPPORTED_MEDIA_TYPES if mt in response_value.get("content", {})), None
)
if media_type is not None:
matching_schema = response_value["content"][media_type].get("schema", {})
description = response_value.get("description") or matching_schema.get("description", "")
yield (
response_key,
RestApiExpectedResponse(
description=description,
media_type=media_type,
schema=matching_schema if matching_schema else None,
),
)
def _parse_security_schemes(self, components: dict) -> dict[str, dict]:
security_schemes = {}
schemes = components.get("securitySchemes", {})
for scheme_name, scheme_data in schemes.items():
security_schemes[scheme_name] = scheme_data
return security_schemes
def _create_rest_api_security_scheme(self, security_scheme_data: dict) -> RestApiSecurityScheme:
return RestApiSecurityScheme(
security_scheme_type=security_scheme_data.get("type", ""),
description=security_scheme_data.get("description"),
name=security_scheme_data.get("name", ""),
in_=security_scheme_data.get("in", ""),
scheme=security_scheme_data.get("scheme", ""),
bearer_format=security_scheme_data.get("bearerFormat"),
flows=security_scheme_data.get("flows"),
open_id_connect_url=security_scheme_data.get("openIdConnectUrl", ""),
)
def _create_security_requirements(
self,
security: list[dict[str, list[str]]],
security_schemes: dict[str, dict],
) -> list[RestApiSecurityRequirement]:
security_requirements: list[RestApiSecurityRequirement] = []
for requirement in security:
for scheme_name, scopes in requirement.items():
scheme_data = security_schemes.get(scheme_name)
if not scheme_data:
raise PluginInitializationError(f"Security scheme '{scheme_name}' is not defined in components.")
scheme = self._create_rest_api_security_scheme(scheme_data)
security_requirements.append(RestApiSecurityRequirement({scheme: scopes}))
return security_requirements
def create_rest_api_operations(
self,
parsed_document: Any,
execution_settings: "OpenAIFunctionExecutionParameters | OpenAPIFunctionExecutionParameters | None" = None,
) -> dict[str, RestApiOperation]:
"""Create REST API operations from the parsed OpenAPI document.
Args:
parsed_document: The parsed OpenAPI document.
execution_settings: The execution settings.
Returns:
A dictionary of RestApiOperation instances.
"""
from semantic_kernel.connectors.openapi_plugin import OperationSelectionPredicateContext
components = parsed_document.get("components", {})
security_schemes = self._parse_security_schemes(components)
paths = parsed_document.get("paths", {})
request_objects = {}
servers = parsed_document.get("servers", [])
if execution_settings and execution_settings.server_url_override:
# Override the servers with the provided URL
server_urls = [{"url": execution_settings.server_url_override, "variables": {}}]
elif servers:
# Process servers, ensuring we capture their variables
server_urls = []
for server in servers:
server_entry = {
"url": server.get("url", "/"),
"variables": server.get("variables", {}),
"description": server.get("description", ""),
}
server_urls.append(server_entry)
else:
# Default server if none specified
server_urls = [{"url": "/", "variables": {}, "description": ""}]
for path, methods in paths.items():
for method, details in methods.items():
request_method = method.lower()
operationId = details.get("operationId", path + "_" + request_method)
summary = details.get("summary", None)
description = details.get("description", None)
context = OperationSelectionPredicateContext(operationId, path, method, description)
if (
execution_settings
and execution_settings.operation_selection_predicate
and not execution_settings.operation_selection_predicate(context)
):
logger.info(f"Skipping operation {operationId} based on custom predicate.")
continue
if execution_settings and operationId in execution_settings.operations_to_exclude:
logger.info(f"Skipping operation {operationId} as it is excluded.")
continue
parameters = details.get("parameters", [])
parsed_params = self._parse_parameters(parameters)
request_body = self._create_rest_api_operation_payload(operationId, details.get("requestBody", None))
responses = dict(self._create_response(details.get("responses", {})))
operation_security = details.get("security", [])
security_requirements = self._create_security_requirements(operation_security, security_schemes)
rest_api_operation = RestApiOperation(
id=operationId,
method=request_method,
servers=server_urls,
path=path,
params=parsed_params,
request_body=request_body,
summary=summary,
description=description,
responses=OrderedDict(responses),
security_requirements=security_requirements,
)
request_objects[operationId] = rest_api_operation
return request_objects