mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-26 08:56:21 +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
111
trustgraph-cli/scripts/tg-set-token-costs
Executable file
111
trustgraph-cli/scripts/tg-set-token-costs
Executable file
|
|
@ -0,0 +1,111 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
Dumps out the current prompts
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
from trustgraph.api import Api, ConfigKey, ConfigValue
|
||||
import json
|
||||
import tabulate
|
||||
import textwrap
|
||||
|
||||
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||
|
||||
def set_costs(api_url, model, input_costs, output_costs):
|
||||
|
||||
api = Api(api_url)
|
||||
|
||||
api.config_put([
|
||||
ConfigValue(
|
||||
type="token-costs", key=model,
|
||||
value=json.dumps({
|
||||
"input_price": input_costs / 1000000,
|
||||
"output_price": output_costs / 1000000,
|
||||
})
|
||||
),
|
||||
])
|
||||
|
||||
def set_prompt(url, id, prompt, response, schema):
|
||||
|
||||
api = Api(url)
|
||||
|
||||
values = api.config_get([
|
||||
ConfigKey(type="prompt", key="template-index")
|
||||
])
|
||||
|
||||
ix = json.loads(values[0].value)
|
||||
|
||||
object = {
|
||||
"id": id,
|
||||
"prompt": prompt,
|
||||
}
|
||||
|
||||
if response:
|
||||
object["response-type"] = response
|
||||
else:
|
||||
object["response-type"] = "text"
|
||||
|
||||
if schema:
|
||||
object["schema"] = schema
|
||||
|
||||
if id not in ix:
|
||||
ix.append(id)
|
||||
|
||||
values = api.config_put([
|
||||
ConfigValue(
|
||||
type="prompt", key="template-index", value=json.dumps(ix)
|
||||
),
|
||||
ConfigValue(
|
||||
type="prompt", key=f"template.{id}", value=json.dumps(object)
|
||||
)
|
||||
])
|
||||
|
||||
print("Prompt set.")
|
||||
|
||||
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})',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--model',
|
||||
required=True,
|
||||
help=f'Model ID',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-i', '--input-costs',
|
||||
required=True,
|
||||
type=float,
|
||||
help=f'Input costs in $ per 1M tokens',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-o', '--output-costs',
|
||||
required=True,
|
||||
type=float,
|
||||
help=f'Input costs in $ per 1M tokens',
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
||||
set_costs(**vars(args))
|
||||
|
||||
except Exception as e:
|
||||
|
||||
print("Exception:", e, flush=True)
|
||||
|
||||
main()
|
||||
|
||||
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