Skip to content

Commit

Permalink
More linting
Browse files Browse the repository at this point in the history
  • Loading branch information
vblagoje committed May 28, 2024
1 parent 9eb6bfb commit 151e803
Showing 1 changed file with 10 additions and 32 deletions.
42 changes: 10 additions & 32 deletions haystack_experimental/util/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from requests.adapters import HTTPAdapter
from urllib3 import Retry

VALID_HTTP_METHODS = ["get", "put", "post", "delete", "options", "head","patch","trace"]
VALID_HTTP_METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"]

MIN_REQUIRED_OPENAPI_SPEC_VERSION = 3

Expand Down Expand Up @@ -185,13 +185,7 @@ class HttpClientError(Exception):

class Operation:
""" Represents an operation in an OpenAPI specification."""
def __init__(
self,
path: str,
method: str,
operation_dict: Dict[str, Any],
spec_dict: Dict[str, Any],
):
def __init__(self, path: str, method: str, operation_dict: Dict[str, Any], spec_dict: Dict[str, Any]):
if method.lower() not in VALID_HTTP_METHODS:
raise ValueError(f"Invalid HTTP method: {method}")
self.path = path
Expand Down Expand Up @@ -393,9 +387,7 @@ def to_dict(self, *, resolve_references: Optional[bool] = False) -> Dict[str, An
:param resolve_references: If True, resolve references in the specification.
:return: A dictionary representation of the OpenAPI specification, optionally fully resolved.
"""
if resolve_references:
return jsonref.replace_refs(self.spec_dict, proxies=False)
return self.spec_dict
return jsonref.replace_refs(self.spec_dict, proxies=False) if resolve_references else self.spec_dict


class ClientConfiguration:
Expand Down Expand Up @@ -463,24 +455,17 @@ def get_tools_definitions(self) -> List[Dict[str, Any]]:
"""
Get the tools definitions used as tools LLM parameter.
"""
provider_to_converter = {
"anthropic": anthropic_converter,
"cohere": cohere_converter,
}
provider_to_converter = {"anthropic": anthropic_converter, "cohere": cohere_converter}
converter = provider_to_converter.get(self.llm_provider, openai_converter)
return converter(self.openapi_spec)

def get_payload_extractor(self):
"""
Get the payload extractor for the LLM provider.
"""
provider_to_arguments_field_name = {
"anthropic": "input",
"cohere": "parameters"
# add more providers here
}
arguments_field_name = provider_to_arguments_field_name.get(self.llm_provider,
"arguments") # default to OpenAI "arguments"
provider_to_arguments_field_name = {"anthropic": "input", "cohere": "parameters"} # add more providers here
# default to OpenAI "arguments"
arguments_field_name = provider_to_arguments_field_name.get(self.llm_provider, "arguments")
return LLMFunctionPayloadExtractor(arguments_field_name=arguments_field_name)

def _create_authentication_from_string(
Expand Down Expand Up @@ -579,8 +564,7 @@ class ClientConfigurationBuilder:
ClientConfigurationBuilder provides a fluent interface for constructing a `ClientConfiguration`.
This builder allows for the step-by-step configuration of all necessary components to interact with an
API defined by an OpenAPI specification. The builder ensures that all configurations are set before building
a valid `ClientConfiguration` instance.
API defined by an OpenAPI specification.
"""

def __init__(self):
Expand Down Expand Up @@ -667,10 +651,7 @@ def build(self) -> ClientConfiguration:

class RequestBuilder:
""" Builds an HTTP request based on an OpenAPI operation"""
def __init__(
self,
client_config: ClientConfiguration,
):
def __init__(self, client_config: ClientConfiguration):
self.openapi_parser = client_config.get_openapi_spec()
self.http_client = client_config.get_http_client()
self.auth_config = client_config.get_auth_config() or PassThroughAuthentication()
Expand Down Expand Up @@ -758,10 +739,7 @@ class OpenAPIServiceClient:
simplifies the process of (LLMs) with services defined by OpenAPI specifications.
"""

def __init__(
self,
client_config: ClientConfiguration,
):
def __init__(self, client_config: ClientConfiguration):
self.openapi_spec = client_config.get_openapi_spec()
self.http_client = client_config.get_http_client()
self.request_builder = RequestBuilder(client_config)
Expand Down

0 comments on commit 151e803

Please sign in to comment.