2024-10-27 17:15:10 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
"""
|
2024-11-08 18:14:14 +00:00
|
|
|
Invokes the LLM prompt service by specifying the prompt template to use
|
|
|
|
|
and values for the variables in the prompt template. The
|
|
|
|
|
prompt template is identified by its template identifier e.g.
|
|
|
|
|
question, extract-definitions. Template variable values are specified
|
|
|
|
|
using key=value arguments on the command line, and these replace
|
|
|
|
|
{{key}} placeholders in the template.
|
2024-10-27 17:15:10 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
import json
|
2025-01-02 19:49:22 +00:00
|
|
|
from trustgraph.api import Api
|
2024-10-27 17:15:10 +00:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
2024-10-27 17:15:10 +00:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
def query(url, template_id, variables):
|
2024-10-27 17:15:10 +00:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
api = Api(url)
|
2024-10-27 17:15:10 +00:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
resp = api.prompt(id=template_id, variables=variables)
|
2024-10-27 17:15:10 +00:00
|
|
|
|
|
|
|
|
if isinstance(resp, str):
|
|
|
|
|
print(resp)
|
|
|
|
|
else:
|
|
|
|
|
print(json.dumps(resp, indent=4))
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2024-11-07 21:01:51 +00:00
|
|
|
prog='tg-invoke-prompt',
|
2024-10-27 17:15:10 +00: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-10-27 17:15:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'id',
|
2024-11-08 18:14:14 +00:00
|
|
|
metavar='template-id',
|
2024-10-27 17:15:10 +00:00
|
|
|
nargs=1,
|
2024-11-08 18:14:14 +00:00
|
|
|
help=f'Prompt identifier e.g. question, extract-definitions',
|
2024-10-27 17:15:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
2024-11-08 18:14:14 +00:00
|
|
|
'variable',
|
2024-10-27 17:15:10 +00:00
|
|
|
nargs='*',
|
2024-11-08 18:14:14 +00:00
|
|
|
metavar="variable=value",
|
|
|
|
|
help='''Prompt template terms of the form variable=value, can be
|
|
|
|
|
specified multiple times''',
|
2024-10-27 17:15:10 +00:00
|
|
|
)
|
2025-02-15 11:22:48 +00:00
|
|
|
|
|
|
|
|
# parser.add_argument(
|
|
|
|
|
# '--pulsar-api-key',
|
|
|
|
|
# default=default_pulsar_api_key,
|
|
|
|
|
# help=f'Pulsar API key',
|
|
|
|
|
# )
|
2024-10-27 17:15:10 +00:00
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
2024-11-08 18:14:14 +00:00
|
|
|
variables = {}
|
2024-10-27 17:15:10 +00:00
|
|
|
|
2024-11-08 18:14:14 +00:00
|
|
|
for variable in args.variable:
|
2024-10-27 17:15:10 +00:00
|
|
|
|
2024-11-08 18:14:14 +00:00
|
|
|
toks = variable.split("=", 1)
|
2024-10-27 17:15:10 +00:00
|
|
|
if len(toks) != 2:
|
2024-11-08 18:14:14 +00:00
|
|
|
raise RuntimeError(f"Malformed variable: {variable}")
|
2024-10-27 17:15:10 +00:00
|
|
|
|
2024-11-08 18:14:14 +00:00
|
|
|
variables[toks[0]] = toks[1]
|
2024-10-27 17:15:10 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
query(
|
2025-01-02 19:49:22 +00:00
|
|
|
url=args.url,
|
2024-11-08 18:14:14 +00:00
|
|
|
template_id=args.id[0],
|
|
|
|
|
variables=variables,
|
2024-10-27 17:15:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
|
|
print("Exception:", e, flush=True)
|
|
|
|
|
|
|
|
|
|
main()
|
|
|
|
|
|