mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-05-11 00:02:37 +02:00
Expose LLM token usage across all service layers (#782)
Expose LLM token usage (in_token, out_token, model) across all service layers Propagate token counts from LLM services through the prompt, text-completion, graph-RAG, document-RAG, and agent orchestrator pipelines to the API gateway and Python SDK. All fields are Optional — None means "not available", distinguishing from a real zero count. Key changes: - Schema: Add in_token/out_token/model to TextCompletionResponse, PromptResponse, GraphRagResponse, DocumentRagResponse, AgentResponse - TextCompletionClient: New TextCompletionResult return type. Split into text_completion() (non-streaming) and text_completion_stream() (streaming with per-chunk handler callback) - PromptClient: New PromptResult with response_type (text/json/jsonl), typed fields (text/object/objects), and token usage. All callers updated. - RAG services: Accumulate token usage across all prompt calls (extract-concepts, edge-scoring, edge-reasoning, synthesis). Non-streaming path sends single combined response instead of chunk + end_of_session. - Agent orchestrator: UsageTracker accumulates tokens across meta-router, pattern prompt calls, and react reasoning. Attached to end_of_dialog. - Translators: Encode token fields when not None (is not None, not truthy) - Python SDK: RAG and text-completion methods return TextCompletionResult (non-streaming) or RAGChunk/AgentAnswer with token fields (streaming) - CLI: --show-usage flag on tg-invoke-llm, tg-invoke-prompt, tg-invoke-graph-rag, tg-invoke-document-rag, tg-invoke-agent
This commit is contained in:
parent
67cfa80836
commit
14e49d83c7
60 changed files with 1252 additions and 577 deletions
|
|
@ -753,7 +753,7 @@ def question(
|
|||
url, flow_id, question, user, collection, entity_limit, triple_limit,
|
||||
max_subgraph_size, max_path_length, edge_score_limit=50,
|
||||
edge_limit=25, streaming=True, token=None,
|
||||
explainable=False, debug=False
|
||||
explainable=False, debug=False, show_usage=False
|
||||
):
|
||||
|
||||
# Explainable mode uses the API to capture and process provenance events
|
||||
|
|
@ -798,16 +798,26 @@ def question(
|
|||
)
|
||||
|
||||
# Stream output
|
||||
last_chunk = None
|
||||
for chunk in response:
|
||||
print(chunk, end="", flush=True)
|
||||
print(chunk.content, end="", flush=True)
|
||||
last_chunk = chunk
|
||||
print() # Final newline
|
||||
|
||||
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=sys.stderr,
|
||||
)
|
||||
|
||||
finally:
|
||||
socket.close()
|
||||
else:
|
||||
# Use REST API for non-streaming
|
||||
flow = api.flow().id(flow_id)
|
||||
resp = flow.graph_rag(
|
||||
result = flow.graph_rag(
|
||||
query=question,
|
||||
user=user,
|
||||
collection=collection,
|
||||
|
|
@ -818,7 +828,15 @@ def question(
|
|||
edge_score_limit=edge_score_limit,
|
||||
edge_limit=edge_limit,
|
||||
)
|
||||
print(resp)
|
||||
print(result.text)
|
||||
|
||||
if show_usage:
|
||||
print(
|
||||
f"Input tokens: {result.in_token} "
|
||||
f"Output tokens: {result.out_token} "
|
||||
f"Model: {result.model}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
def main():
|
||||
|
||||
|
|
@ -923,6 +941,12 @@ def main():
|
|||
help='Show debug output for troubleshooting'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--show-usage',
|
||||
action='store_true',
|
||||
help='Show token usage and model on stderr'
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
|
@ -943,6 +967,7 @@ def main():
|
|||
token=args.token,
|
||||
explainable=args.explainable,
|
||||
debug=args.debug,
|
||||
show_usage=args.show_usage,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue