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-01-02 19:49:22 +00:00
|
|
|
def query(url, system, prompt):
|
2024-11-07 21:01:51 +00:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
api = Api(url)
|
2024-11-07 21:01:51 +00:00
|
|
|
|
2025-01-02 19:49:22 +00: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-02-15 11:22:48 +00:00
|
|
|
|
|
|
|
|
# parser.add_argument(
|
|
|
|
|
# '--pulsar-api-key',
|
|
|
|
|
# default=default_pulsar_api_key,
|
|
|
|
|
# help=f'Pulsar API key',
|
|
|
|
|
# )
|
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,
|
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()
|
|
|
|
|
|