feat(token-tracking): add Token usage section to summary.md
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -35,6 +35,23 @@ def _table_from_frame(frame: pd.DataFrame) -> str:
|
||||
return "\n".join([header, separator, *body])
|
||||
|
||||
|
||||
def _token_usage_section(token_usage: dict[str, dict[str, int]]) -> list[str]:
|
||||
"""Render the '## Token 用量' section as a list of markdown lines."""
|
||||
lines = ["", "## Token 用量", ""]
|
||||
if not token_usage:
|
||||
lines.append("未记录 token 用量。")
|
||||
return lines
|
||||
lines.append("| 模型 | input_tokens | output_tokens | 调用次数 |")
|
||||
lines.append("|---|---|---|---|")
|
||||
for model in sorted(token_usage):
|
||||
usage = token_usage[model]
|
||||
lines.append(
|
||||
f"| {model} | {usage.get('input_tokens', 0)} "
|
||||
f"| {usage.get('output_tokens', 0)} | {usage.get('calls', 0)} |"
|
||||
)
|
||||
return lines
|
||||
|
||||
|
||||
def build_summary_markdown(result: EvaluationResult) -> str:
|
||||
"""Build the human-readable markdown summary written for each evaluation run."""
|
||||
total = len(result.valid_samples) + len(result.invalid_samples)
|
||||
@@ -57,6 +74,7 @@ def build_summary_markdown(result: EvaluationResult) -> str:
|
||||
|
||||
if scores.empty:
|
||||
lines.append("No valid samples were scored.")
|
||||
lines.extend(_token_usage_section(result.token_usage))
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
score_rows_list = scores.to_dict(orient="records")
|
||||
@@ -97,4 +115,5 @@ def build_summary_markdown(result: EvaluationResult) -> str:
|
||||
_table_from_frame(detail),
|
||||
"```",
|
||||
])
|
||||
lines.extend(_token_usage_section(result.token_usage))
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
Reference in New Issue
Block a user