From 7a7008c80a7851df5e727ef51740b68405932311 Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Thu, 4 Sep 2025 15:28:41 +0100 Subject: [PATCH] API support --- trustgraph-base/trustgraph/api/flow.py | 59 ++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/trustgraph-base/trustgraph/api/flow.py b/trustgraph-base/trustgraph/api/flow.py index d0b4ca91..b3c86c4e 100644 --- a/trustgraph-base/trustgraph/api/flow.py +++ b/trustgraph-base/trustgraph/api/flow.py @@ -426,3 +426,62 @@ class FlowInstance: return result + def nlp_query(self, question, max_results=100): + """ + Convert a natural language question to a GraphQL query. + + Args: + question: Natural language question + max_results: Maximum number of results to return (default: 100) + + Returns: + dict with graphql_query, variables, detected_schemas, confidence + """ + + input = { + "question": question, + "max_results": max_results + } + + response = self.request( + "service/nlp-query", + input + ) + + # Check for system-level error + if "error" in response and response["error"]: + error_type = response["error"].get("type", "unknown") + error_message = response["error"].get("message", "Unknown error") + raise ProtocolException(f"{error_type}: {error_message}") + + return response + + def structured_query(self, question): + """ + Execute a natural language question against structured data. + Combines NLP query conversion and GraphQL execution. + + Args: + question: Natural language question + + Returns: + dict with data and optional errors + """ + + input = { + "question": question + } + + response = self.request( + "service/structured-query", + input + ) + + # Check for system-level error + if "error" in response and response["error"]: + error_type = response["error"].get("type", "unknown") + error_message = response["error"].get("message", "Unknown error") + raise ProtocolException(f"{error_type}: {error_message}") + + return response +