mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 08:26:21 +02:00
* Port a number of commands to use API gateway instead of Pulsar * Ported tg-invoke-agent to websockets API * Rename the 2 RAG commands: tg-query-... to tg-invoke-...
83 lines
1.8 KiB
Python
Executable file
83 lines
1.8 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
"""
|
|
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.
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import json
|
|
from trustgraph.api import Api
|
|
|
|
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
|
|
|
def query(url, template_id, variables):
|
|
|
|
api = Api(url)
|
|
|
|
resp = api.prompt(id=template_id, variables=variables)
|
|
|
|
if isinstance(resp, str):
|
|
print(resp)
|
|
else:
|
|
print(json.dumps(resp, indent=4))
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
prog='tg-invoke-prompt',
|
|
description=__doc__,
|
|
)
|
|
|
|
parser.add_argument(
|
|
'-u', '--url',
|
|
default=default_url,
|
|
help=f'API URL (default: {default_url})',
|
|
)
|
|
|
|
parser.add_argument(
|
|
'id',
|
|
metavar='template-id',
|
|
nargs=1,
|
|
help=f'Prompt identifier e.g. question, extract-definitions',
|
|
)
|
|
|
|
parser.add_argument(
|
|
'variable',
|
|
nargs='*',
|
|
metavar="variable=value",
|
|
help='''Prompt template terms of the form variable=value, can be
|
|
specified multiple times''',
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
variables = {}
|
|
|
|
for variable in args.variable:
|
|
|
|
toks = variable.split("=", 1)
|
|
if len(toks) != 2:
|
|
raise RuntimeError(f"Malformed variable: {variable}")
|
|
|
|
variables[toks[0]] = toks[1]
|
|
|
|
try:
|
|
|
|
query(
|
|
url=args.url,
|
|
template_id=args.id[0],
|
|
variables=variables,
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
print("Exception:", e, flush=True)
|
|
|
|
main()
|
|
|