Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata to tracing #375

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions kai/kai_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ def llm_result(
with open(result_file_path, "w") as f:
f.write(result.pretty_repr())

@enabled_check
def llm_token_usage(
self, current_batch_count: int, retry_count: int, token_usage: dict
):
token_usage_file_path = os.path.join(
self.trace_dir,
f"{current_batch_count}",
f"{retry_count}",
"token_usage.json",
)
os.makedirs(os.path.dirname(token_usage_file_path), exist_ok=True)
with open(token_usage_file_path, "w") as f:
f.write(json.dumps(token_usage, indent=4, default=str))

@enabled_check
def exception(
self,
Expand Down
9 changes: 9 additions & 0 deletions kai/service/kai_application/kai_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ def get_incident_solutions_for_file(
):
llm_result = self.model_provider.llm.invoke(prompt)
trace.llm_result(count, retry_attempt_count, llm_result)
try:
token_usage = llm_result.response_metadata["token_usage"]
trace.llm_token_usage(
count, retry_attempt_count, token_usage
)
except KeyError as e:
KAI_LOG.warning(
f"Key does not exist in the dictionary: {e}"
)

content = parse_file_solution_content(
src_file_language, str(llm_result.content)
Expand Down
Loading