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:
cybermaggedon 2025-04-28 21:26:38 +01:00 committed by GitHub
parent 5af7909122
commit 9508ac6c69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 249 additions and 144 deletions

View 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()