2024-11-07 21:01:51 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Invokes the text completion service by specifying an LLM system prompt
|
|
|
|
|
and user prompt. Both arguments are required.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
import json
|
2025-01-02 19:49:22 +00:00
|
|
|
from trustgraph.api import Api
|
2024-11-07 21:01:51 +00:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
2024-11-07 21:01:51 +00:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
def query(url, flow_id, system, prompt):
|
2024-11-07 21:01:51 +00:00
|
|
|
|
2025-05-06 16:17:16 +01:00
|
|
|
api = Api(url).flow().id(flow_id)
|
2024-11-07 21:01:51 +00:00
|
|
|
|
2025-05-05 11:09:18 +01:00
|
|
|
resp = api.text_completion(system=system, prompt=prompt)
|
2024-11-07 21:01:51 +00:00
|
|
|
|
|
|
|
|
print(resp)
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
prog='tg-invoke-llm',
|
|
|
|
|
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-11-07 21:01:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'system',
|
|
|
|
|
nargs=1,
|
|
|
|
|
help='LLM system prompt e.g. You are a helpful assistant',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'prompt',
|
|
|
|
|
nargs=1,
|
|
|
|
|
help='LLM prompt e.g. What is 2 + 2?',
|
|
|
|
|
)
|
2025-05-03 10:39:53 +01:00
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-f', '--flow-id',
|
|
|
|
|
default="0000",
|
|
|
|
|
help=f'Flow ID (default: 0000)'
|
|
|
|
|
)
|
2024-11-07 21:01:51 +00:00
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
query(
|
2025-01-02 19:49:22 +00:00
|
|
|
url=args.url,
|
2025-05-03 11:07:17 +01:00
|
|
|
flow_id = args.flow_id,
|
2024-11-07 21:01:51 +00:00
|
|
|
system=args.system[0],
|
|
|
|
|
prompt=args.prompt[0],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
|
|
print("Exception:", e, flush=True)
|
|
|
|
|
|
|
|
|
|
main()
|
|
|
|
|
|