mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-24 20:51:02 +02:00
Collection+user fields in structured query
This commit is contained in:
parent
a92050c411
commit
8a01790936
5 changed files with 33 additions and 10 deletions
|
|
@ -456,20 +456,24 @@ class FlowInstance:
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def structured_query(self, question):
|
def structured_query(self, question, user="trustgraph", collection="default"):
|
||||||
"""
|
"""
|
||||||
Execute a natural language question against structured data.
|
Execute a natural language question against structured data.
|
||||||
Combines NLP query conversion and GraphQL execution.
|
Combines NLP query conversion and GraphQL execution.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
question: Natural language question
|
question: Natural language question
|
||||||
|
user: Cassandra keyspace identifier (default: "trustgraph")
|
||||||
|
collection: Data collection identifier (default: "default")
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict with data and optional errors
|
dict with data and optional errors
|
||||||
"""
|
"""
|
||||||
|
|
||||||
input = {
|
input = {
|
||||||
"question": question
|
"question": question,
|
||||||
|
"user": user,
|
||||||
|
"collection": collection
|
||||||
}
|
}
|
||||||
|
|
||||||
response = self.request(
|
response = self.request(
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,16 @@ class StructuredQueryRequestTranslator(MessageTranslator):
|
||||||
|
|
||||||
def to_pulsar(self, data: Dict[str, Any]) -> StructuredQueryRequest:
|
def to_pulsar(self, data: Dict[str, Any]) -> StructuredQueryRequest:
|
||||||
return StructuredQueryRequest(
|
return StructuredQueryRequest(
|
||||||
question=data.get("question", "")
|
question=data.get("question", ""),
|
||||||
|
user=data.get("user", "trustgraph"), # Default fallback
|
||||||
|
collection=data.get("collection", "default") # Default fallback
|
||||||
)
|
)
|
||||||
|
|
||||||
def from_pulsar(self, obj: StructuredQueryRequest) -> Dict[str, Any]:
|
def from_pulsar(self, obj: StructuredQueryRequest) -> Dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"question": obj.question
|
"question": obj.question,
|
||||||
|
"user": obj.user,
|
||||||
|
"collection": obj.collection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ from ..core.topic import topic
|
||||||
|
|
||||||
class StructuredQueryRequest(Record):
|
class StructuredQueryRequest(Record):
|
||||||
question = String()
|
question = String()
|
||||||
|
user = String() # Cassandra keyspace identifier
|
||||||
|
collection = String() # Data collection identifier
|
||||||
|
|
||||||
class StructuredQueryResponse(Record):
|
class StructuredQueryResponse(Record):
|
||||||
error = Error()
|
error = Error()
|
||||||
|
|
|
||||||
|
|
@ -79,11 +79,11 @@ def format_table_data(rows, table_name, output_format):
|
||||||
else:
|
else:
|
||||||
return json.dumps({table_name: rows}, indent=2)
|
return json.dumps({table_name: rows}, indent=2)
|
||||||
|
|
||||||
def structured_query(url, flow_id, question, output_format='table'):
|
def structured_query(url, flow_id, question, user='trustgraph', collection='default', output_format='table'):
|
||||||
|
|
||||||
api = Api(url).flow().id(flow_id)
|
api = Api(url).flow().id(flow_id)
|
||||||
|
|
||||||
resp = api.structured_query(question=question)
|
resp = api.structured_query(question=question, user=user, collection=collection)
|
||||||
|
|
||||||
# Check for errors
|
# Check for errors
|
||||||
if "error" in resp and resp["error"]:
|
if "error" in resp and resp["error"]:
|
||||||
|
|
@ -132,6 +132,18 @@ def main():
|
||||||
help='Natural language question to execute',
|
help='Natural language question to execute',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--user',
|
||||||
|
default='trustgraph',
|
||||||
|
help='Cassandra keyspace identifier (default: trustgraph)'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--collection',
|
||||||
|
default='default',
|
||||||
|
help='Data collection identifier (default: default)'
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--format',
|
'--format',
|
||||||
choices=['table', 'json', 'csv'],
|
choices=['table', 'json', 'csv'],
|
||||||
|
|
@ -147,6 +159,8 @@ def main():
|
||||||
url=args.url,
|
url=args.url,
|
||||||
flow_id=args.flow_id,
|
flow_id=args.flow_id,
|
||||||
question=args.question,
|
question=args.question,
|
||||||
|
user=args.user,
|
||||||
|
collection=args.collection,
|
||||||
output_format=args.format,
|
output_format=args.format,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -111,11 +111,10 @@ class Processor(FlowProcessor):
|
||||||
else:
|
else:
|
||||||
variables_as_strings[key] = str(value)
|
variables_as_strings[key] = str(value)
|
||||||
|
|
||||||
# Use standard TrustGraph user/collection values
|
# Use user/collection values from request
|
||||||
# These should eventually come from authentication/context
|
|
||||||
objects_request = ObjectsQueryRequest(
|
objects_request = ObjectsQueryRequest(
|
||||||
user="trustgraph", # Standard TrustGraph user
|
user=request.user,
|
||||||
collection="default", # Standard default collection
|
collection=request.collection,
|
||||||
query=nlp_response.graphql_query,
|
query=nlp_response.graphql_query,
|
||||||
variables=variables_as_strings,
|
variables=variables_as_strings,
|
||||||
operation_name=None
|
operation_name=None
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue