mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-06-12 08:15:14 +02:00
Output token counts
This commit is contained in:
parent
1630346bd9
commit
d1ac03f0ce
1 changed files with 27 additions and 4 deletions
|
|
@ -10,7 +10,8 @@ from trustgraph.api import Api
|
|||
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||
default_token = os.getenv("TRUSTGRAPH_TOKEN", None)
|
||||
|
||||
def query(url, flow_id, system, prompt, streaming=True, token=None):
|
||||
def query(url, flow_id, system, prompt, streaming=True, token=None,
|
||||
show_usage=False):
|
||||
|
||||
# Create API client
|
||||
api = Api(url=url, token=token)
|
||||
|
|
@ -26,15 +27,30 @@ def query(url, flow_id, system, prompt, streaming=True, token=None):
|
|||
)
|
||||
|
||||
if streaming:
|
||||
# Stream output to stdout without newline
|
||||
last_chunk = None
|
||||
for chunk in response:
|
||||
print(chunk.content, end="", flush=True)
|
||||
# Add final newline after streaming
|
||||
last_chunk = chunk
|
||||
print()
|
||||
|
||||
if show_usage and last_chunk:
|
||||
print(
|
||||
f"Input tokens: {last_chunk.in_token} "
|
||||
f"Output tokens: {last_chunk.out_token} "
|
||||
f"Model: {last_chunk.model}",
|
||||
file=__import__('sys').stderr,
|
||||
)
|
||||
else:
|
||||
# Non-streaming: print complete response
|
||||
print(response.text)
|
||||
|
||||
if show_usage:
|
||||
print(
|
||||
f"Input tokens: {response.in_token} "
|
||||
f"Output tokens: {response.out_token} "
|
||||
f"Model: {response.model}",
|
||||
file=__import__('sys').stderr,
|
||||
)
|
||||
|
||||
finally:
|
||||
# Clean up socket connection
|
||||
socket.close()
|
||||
|
|
@ -82,6 +98,12 @@ def main():
|
|||
help='Disable streaming (default: streaming enabled)'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--show-usage',
|
||||
action='store_true',
|
||||
help='Show token usage and model on stderr'
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
|
@ -93,6 +115,7 @@ def main():
|
|||
prompt=args.prompt[0],
|
||||
streaming=not args.no_streaming,
|
||||
token=args.token,
|
||||
show_usage=args.show_usage,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue