2024-09-05 16:45:22 +01:00
|
|
|
"""
|
2025-05-06 13:43:17 +01:00
|
|
|
Uses the DocumentRAG service to answer a question
|
2024-09-05 16:45:22 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
2025-01-02 19:49:22 +00:00
|
|
|
from trustgraph.api import Api
|
2024-09-05 16:45:22 +01:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
2025-12-04 17:38:57 +00:00
|
|
|
default_token = os.getenv("TRUSTGRAPH_TOKEN", None)
|
2024-10-02 18:14:29 +01:00
|
|
|
default_user = 'trustgraph'
|
|
|
|
|
default_collection = 'default'
|
2025-03-13 00:38:18 +00:00
|
|
|
default_doc_limit = 10
|
2024-09-05 16:45:22 +01:00
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
def question(url, flow_id, question, user, collection, doc_limit, streaming=True, token=None):
|
2025-11-26 19:47:39 +00:00
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
# Create API client
|
|
|
|
|
api = Api(url=url, token=token)
|
2025-11-26 19:47:39 +00:00
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
if streaming:
|
|
|
|
|
# Use socket client for streaming
|
|
|
|
|
socket = api.socket()
|
|
|
|
|
flow = socket.flow(flow_id)
|
2025-11-26 19:47:39 +00:00
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
try:
|
|
|
|
|
response = flow.document_rag(
|
2025-12-17 21:40:43 +00:00
|
|
|
query=question,
|
2025-12-04 17:38:57 +00:00
|
|
|
user=user,
|
|
|
|
|
collection=collection,
|
|
|
|
|
doc_limit=doc_limit,
|
|
|
|
|
streaming=True
|
|
|
|
|
)
|
2024-09-05 16:45:22 +01:00
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
# Stream output
|
|
|
|
|
for chunk in response:
|
2025-12-04 21:11:56 +00:00
|
|
|
print(chunk, end="", flush=True)
|
2025-12-04 17:38:57 +00:00
|
|
|
print() # Final newline
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
socket.close()
|
|
|
|
|
else:
|
|
|
|
|
# Use REST API for non-streaming
|
|
|
|
|
flow = api.flow().id(flow_id)
|
|
|
|
|
resp = flow.document_rag(
|
2025-12-17 21:40:43 +00:00
|
|
|
query=question,
|
2025-12-04 17:38:57 +00:00
|
|
|
user=user,
|
|
|
|
|
collection=collection,
|
|
|
|
|
doc_limit=doc_limit,
|
|
|
|
|
)
|
|
|
|
|
print(resp)
|
2024-09-05 16:45:22 +01:00
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2025-01-02 19:49:22 +00:00
|
|
|
prog='tg-invoke-document-rag',
|
2024-09-05 16:45:22 +01:00
|
|
|
description=__doc__,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
2025-01-02 19:49:22 +00:00
|
|
|
'-u', '--url',
|
|
|
|
|
default=default_url,
|
|
|
|
|
help=f'API URL (default: {default_url})',
|
2024-09-05 16:45:22 +01:00
|
|
|
)
|
|
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-t', '--token',
|
|
|
|
|
default=default_token,
|
|
|
|
|
help='Authentication token (default: $TRUSTGRAPH_TOKEN)',
|
|
|
|
|
)
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-f', '--flow-id',
|
2025-05-24 12:27:56 +01:00
|
|
|
default="default",
|
|
|
|
|
help=f'Flow ID (default: default)'
|
2025-05-03 10:39:53 +01:00
|
|
|
)
|
2025-02-15 11:22:48 +00:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
parser.add_argument(
|
2025-03-13 00:38:18 +00:00
|
|
|
'-q', '--question',
|
2024-09-05 16:45:22 +01:00
|
|
|
required=True,
|
2025-01-02 19:49:22 +00:00
|
|
|
help=f'Question to answer',
|
2024-09-05 16:45:22 +01:00
|
|
|
)
|
|
|
|
|
|
2024-10-02 18:14:29 +01:00
|
|
|
parser.add_argument(
|
2025-01-02 19:49:22 +00:00
|
|
|
'-U', '--user',
|
2024-10-02 18:14:29 +01:00
|
|
|
default=default_user,
|
|
|
|
|
help=f'User ID (default: {default_user})'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
2025-01-02 19:49:22 +00:00
|
|
|
'-C', '--collection',
|
2024-10-02 18:14:29 +01:00
|
|
|
default=default_collection,
|
|
|
|
|
help=f'Collection ID (default: {default_collection})'
|
|
|
|
|
)
|
|
|
|
|
|
2025-03-13 00:38:18 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-d', '--doc-limit',
|
2025-12-04 17:38:57 +00:00
|
|
|
type=int,
|
2025-03-13 00:38:18 +00:00
|
|
|
default=default_doc_limit,
|
|
|
|
|
help=f'Document limit (default: {default_doc_limit})'
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-26 19:47:39 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--no-streaming',
|
|
|
|
|
action='store_true',
|
|
|
|
|
help='Disable streaming (use non-streaming mode)'
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
2025-12-04 17:38:57 +00:00
|
|
|
question(
|
|
|
|
|
url=args.url,
|
|
|
|
|
flow_id=args.flow_id,
|
|
|
|
|
question=args.question,
|
|
|
|
|
user=args.user,
|
|
|
|
|
collection=args.collection,
|
|
|
|
|
doc_limit=args.doc_limit,
|
|
|
|
|
streaming=not args.no_streaming,
|
|
|
|
|
token=args.token,
|
|
|
|
|
)
|
2024-09-05 16:45:22 +01:00
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
|
|
print("Exception:", e, flush=True)
|
|
|
|
|
|
2025-07-23 21:22:08 +01:00
|
|
|
if __name__ == "__main__":
|
2025-12-04 17:38:57 +00:00
|
|
|
main()
|