Skip to content

Commit

Permalink
fixing swagger resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
codebanesr committed Jan 18, 2024
1 parent 6fccef2 commit 1bfae7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 48 deletions.
3 changes: 2 additions & 1 deletion llm-server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,5 @@ lxml==4.9.3
python-socketio==5.10.0
Flask-SocketIO==5.3.6
sentry_sdk==1.39.1
anthropic==0.8.1
anthropic==0.8.1
jsonref==1.1.0
67 changes: 20 additions & 47 deletions llm-server/utils/swagger_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json

import jsonref
from collections import defaultdict
from typing import DefaultDict, Dict, Any, cast
from typing import List
Expand All @@ -18,15 +20,15 @@

class Endpoint:
def __init__(
self,
operation_id,
endpoint_type,
name,
description,
request_body,
parameters,
response,
path,
self,
operation_id,
endpoint_type,
name,
description,
request_body,
parameters,
response,
path,
):
self.operation_id = operation_id
self.type = endpoint_type
Expand Down Expand Up @@ -181,38 +183,6 @@ def get_base_uri(self):

raise ValueError("No valid servers or host information found in Swagger data.")

def resolve_schema_references(self, schema):
"""
Resolves $ref references in the schema to their corresponding definitions.
"""
if "$ref" in schema:
ref_path = schema["$ref"]
parts = ref_path.split("/")
# Navigate through the swagger data to find the referenced schema
ref_schema = self.swagger_data
for part in parts[1:]: # Skip the first element as it's just a '#'
ref_schema = ref_schema.get(part, {})
return ref_schema
return schema

def process_payload(self, payload):
"""
Processes the payload to include full schema definitions for any $ref references.
"""
if "request_body" in payload and "content" in payload["request_body"]:
for content_type, content_data in payload["request_body"][
"content"
].items():
if "schema" in content_data:
content_data["schema"] = self.resolve_schema_references(
content_data["schema"]
)
if "parameters" in payload:
for param in payload["parameters"]:
if "schema" in param:
param["schema"] = self.resolve_schema_references(param["schema"])
return payload

def get_all_actions(self, bot_id: str):
"""
Retrieves all actions defined in the Swagger file as ActionDTO instances.
Expand All @@ -221,6 +191,7 @@ def get_all_actions(self, bot_id: str):
"""
actions: List[ActionDTO] = []
base_uri = self.get_base_uri()
self.swagger_data = jsonref.replace_refs(self.swagger_data)
paths = self.swagger_data.get("paths", {})

for path, path_data in paths.items():
Expand All @@ -230,15 +201,15 @@ def get_all_actions(self, bot_id: str):
"parameters": method_data.get("parameters", []),
}

# Process the payload to resolve any $ref references
processed_payload = self.process_payload(payload)

action_dto = ActionDTO(
api_endpoint=base_uri + path,
name=method_data.get("name", method_data.get("summary", method_data.get('description'))),
name=method_data.get(
"name",
method_data.get("summary", method_data.get("description")),
),
description=method_data.get("description"),
request_type=method.upper(),
payload=processed_payload,
payload=payload,
bot_id=bot_id,
)
actions.append(action_dto)
Expand All @@ -261,6 +232,8 @@ def gather_metadata(self, api_data: dict) -> DefaultDict[str, Dict[str, str]]:

relative_paths: DefaultDict[str, Dict[str, Any]] = defaultdict(dict)

# @todo: the case where parameters are common for all paths is a valid way of defining apis, and we are not handling it
# check postman collection in _swaggers folder
for path, path_item in api_data["paths"].items():
for http_verb, http_details in path_item.items():
summary = http_details.get("summary") or ""
Expand Down Expand Up @@ -294,7 +267,7 @@ def ingest_swagger_summary(self, bot_id: str) -> None:
for endpoint, meta in endpoints.items():
response += f"\nSummary: {meta['summary']}\nDescription: {meta['description']}"

chat = get_chat_model()
chat = get_chat_model("ingest_swagger_summary")
messages = [
SystemMessage(
content="You are an assistant that can take some text and generate a correct summary of capabilities of a bot that is equipped with these api endpoints. The summary should begin with - As an ai assistant i have the following capabilities. Keep the summary concise, try to summarize in 3 sentences or less"
Expand Down

0 comments on commit 1bfae7c

Please sign in to comment.