Skip to content

Commit

Permalink
Merge pull request #276 from SmartAPI/update-mkg-parser
Browse files Browse the repository at this point in the history
MetaKG Parser/Mapping update
  • Loading branch information
newgene authored Nov 1, 2024
2 parents 5e384a3 + 838b61d commit 07f6f0d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/controller/smartapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ def edge_consolidation_build(cls):
"subject": edge["_source"]["subject"],
"object": edge["_source"]["object"],
"predicate": edge["_source"]["predicate"],
"api": [edge_api]
"api": [edge_api],
**{k: edge["_source"][k] for k in ["subject_prefix", "object_prefix"] if k in edge["_source"]}
}

processed_edges += 1
Expand Down
6 changes: 6 additions & 0 deletions src/model/metakg.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"mapping": {"type": "object", "enabled": False},
}
},
{
"ignore_testExamples_field": {
"path_match": "*bte.query_operation.testExamples",
"mapping": {"type": "object", "enabled": False},
}
},
{
"ignore_response_mapping_field": {
"path_match": "*bte.response_mapping",
Expand Down
26 changes: 14 additions & 12 deletions src/utils/metakg/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def construct_query_operation(self, data):
query_operation.server = server
query_operation.path = self.path
query_operation.tags = self.api_meta_data["tags"]

if "agent_type" in op:
query_operation.agent_type = op["agent_type"]
if "knowledge_level" in op:
query_operation.knowledge_level = op["knowledge_level"]
if "testExamples" in op:
query_operation.testExamples = op["testExamples"]
if "useTemplating" in op:
query_operation.useTemplating = op["useTemplating"]

return query_operation

def remove_bio_link_prefix(self, _input):
Expand Down Expand Up @@ -66,7 +76,6 @@ def construct_association(self, input, output, op):
def construct_response_mapping(self, op):
if "responseMapping" in op:
op["response_mapping"] = op["responseMapping"]

return {f"{op['predicate']}": self.resolve_ref_if_provided(op.get("response_mapping"))}

def parse_individual_operation(self, op, method, path_params):
Expand All @@ -81,18 +90,11 @@ def parse_individual_operation(self, op, method, path_params):
"association": association,
"response_mapping": response_mapping,
"tags": query_operation.tags,
"agent_type": query_operation.agent_type,
"knowledge_level": query_operation.knowledge_level,
"testExamples": query_operation.testExamples,
"useTemplating": query_operation.useTemplating,
}

# Add additional fields to update_info if they exist in the operation
if "agent_type" in op:
update_info["agent_type"] = op["agent_type"]
if "knowledge_level" in op:
update_info["knowledge_level"] = op["knowledge_level"]
if "testExamples" in op:
update_info["testExamples"] = op["testExamples"]
if "useTemplating" in op:
update_info["useTemplating"] = op["useTemplating"]

res.append(update_info)
return res

Expand Down
9 changes: 5 additions & 4 deletions src/utils/metakg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,14 @@ def get_ops_from_metakg_endpoint(self, metadata, extra_log_msg=""):

def extract_metakgedges(self, ops, extra_data=None):
extra_data = extra_data or {}

metakg_edges = []
for op in ops:
smartapi_data = op["association"]["smartapi"]
url = (smartapi_data.get("meta") or {}).get("url") or extra_data.get("url")
_id = smartapi_data.get("id") or extra_data.get("id")

edge = {
"subject": op["association"]["input_type"],
"object": op["association"]["output_type"],
"subject_prefix": op["association"]["input_id"],
"object_prefix": op["association"]["output_id"],
"predicate": op["association"]["predicate"],
"api": {
"name": op["association"]["api_name"],
Expand All @@ -169,6 +165,11 @@ def extract_metakgedges(self, ops, extra_data=None):
# "username": (smartapi_data.get("meta") or {}).get("username"),
},
}
# Conditionally add subject_prefix and object_prefix if they exist
if "input_id" in op["association"]:
edge["subject_prefix"] = op["association"]["input_id"]
if "output_id" in op["association"]:
edge["object_prefix"] = op["association"]["output_id"]
# include bte-specific edge metadata
bte = {}
for attr in ["query_operation", "response_mapping"]:
Expand Down
40 changes: 40 additions & 0 deletions src/utils/metakg/query_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class QueryOperationObject:
_server = ""
_tags = []
_path_params = []
_agent_type = None
_knowledge_level = None
_testExamples = []
_useTemplating = None

@property
def xBTEKGSOperation(self):
Expand All @@ -26,6 +30,38 @@ def xBTEKGSOperation(self, new_op):
self._support_batch = new_op.get("supportBatch")
self._input_separator = new_op.get("inputSeparator")

@property
def agent_type(self):
return self._agent_type

@agent_type.setter
def agent_type(self, new_agent_type):
self._agent_type = new_agent_type

@property
def knowledge_level(self):
return self._knowledge_level

@knowledge_level.setter
def knowledge_level(self, new_knowledge_level):
self._knowledge_level = new_knowledge_level

@property
def testExamples(self):
return self._testExamples

@testExamples.setter
def testExamples(self, new_testExamples):
self._testExamples = new_testExamples

@property
def useTemplating(self):
return self._useTemplating

@useTemplating.setter
def useTemplating(self, new_useTemplating):
self._useTemplating = new_useTemplating

@property
def params(self):
return self._params
Expand Down Expand Up @@ -94,6 +130,10 @@ def to_dict(self):
"tags",
"support_batch",
"input_separator",
"agent_type",
"knowledge_level",
"testExamples",
"useTemplating",
]:
val = getattr(self, attr, None)
if val:
Expand Down

0 comments on commit 07f6f0d

Please sign in to comment.