mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-05-18 03:45:12 +02:00
Port metering to new API, not tested. (#354)
- Port metering to new API - Moved price list to configuration - Added tg-set-token-costs and tg-show-token-costs utils.
This commit is contained in:
parent
5af7909122
commit
9508ac6c69
6 changed files with 249 additions and 144 deletions
79
trustgraph-cli/scripts/tg-show-token-costs
Executable file
79
trustgraph-cli/scripts/tg-show-token-costs
Executable file
|
|
@ -0,0 +1,79 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Dumps out the current prompts
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
from trustgraph.api import Api, ConfigKey
|
||||
import json
|
||||
import tabulate
|
||||
import textwrap
|
||||
|
||||
tabulate.PRESERVE_WHITESPACE = True
|
||||
|
||||
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||
|
||||
def show_config(url):
|
||||
|
||||
api = Api(url)
|
||||
|
||||
models = api.config_list("token-costs")
|
||||
|
||||
costs = []
|
||||
|
||||
def fmt(x):
|
||||
return "{price:.3f}".format(price = 1000000 * x)
|
||||
|
||||
for model in models:
|
||||
|
||||
try:
|
||||
values = json.loads(api.config_get([
|
||||
ConfigKey(type="token-costs", key=model),
|
||||
])[0].value)
|
||||
costs.append((
|
||||
model,
|
||||
fmt(values.get("input_price")),
|
||||
fmt(values.get("output_price")),
|
||||
))
|
||||
except:
|
||||
costs.append((
|
||||
model, "-", "-"
|
||||
))
|
||||
|
||||
print(tabulate.tabulate(
|
||||
costs,
|
||||
tablefmt = "pretty",
|
||||
headers = ["model", "input, $/Mt", "output, $/Mt"],
|
||||
colalign = ["left", "right", "right"],
|
||||
# stralign = ["left", "decimal", "decimal"]
|
||||
))
|
||||
|
||||
def main():
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='tg-show-prompts',
|
||||
description=__doc__,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-u', '--api-url',
|
||||
default=default_url,
|
||||
help=f'API URL (default: {default_url})',
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
||||
show_config(
|
||||
url=args.api_url,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
print("Exception:", e, flush=True)
|
||||
|
||||
main()
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue