-
Notifications
You must be signed in to change notification settings - Fork 62
[openai] Record cache-write and modality token usage #681
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
base: main
Are you sure you want to change the base?
Changes from all commits
7ba7531
6fdd9f7
dd9d211
bfe2bf0
7ff833e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Record cache-write and modality token usage from OpenAI Chat Completions responses and streams. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
|
|
||
| import openai | ||
| from openai import NotGiven | ||
| from openai.types import CompletionUsage | ||
|
|
||
| from opentelemetry.semconv._incubating.attributes import ( | ||
| gen_ai_attributes as GenAIAttributes, | ||
|
|
@@ -65,6 +66,33 @@ def get_property_value(obj, property_name): | |
| return getattr(obj, property_name, None) | ||
|
|
||
|
|
||
| def set_chat_usage_details( | ||
| invocation: InferenceInvocation, usage: CompletionUsage | ||
| ) -> None: | ||
| prompt_details: object = get_property_value(usage, "prompt_tokens_details") | ||
| completion_details: object = get_property_value( | ||
| usage, "completion_tokens_details" | ||
| ) | ||
| invocation.cache_write_input_tokens = get_property_value( | ||
| prompt_details, "cache_write_tokens" | ||
| ) | ||
| invocation.text_input_tokens = get_property_value( | ||
| prompt_details, "text_tokens" | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OpenAI Chat Completions does not expose
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| invocation.image_input_tokens = get_property_value( | ||
| prompt_details, "image_tokens" | ||
| ) | ||
| invocation.audio_input_tokens = get_property_value( | ||
| prompt_details, "audio_tokens" | ||
| ) | ||
| invocation.text_output_tokens = get_property_value( | ||
| completion_details, "text_tokens" | ||
| ) | ||
| invocation.audio_output_tokens = get_property_value( | ||
| completion_details, "audio_tokens" | ||
| ) | ||
|
|
||
|
|
||
| def get_server_address_and_port( | ||
| client_instance, | ||
| ) -> tuple[str | None, int | None]: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The token-detail extraction is duplicated between this helper and the stream wrapper. Please share extraction across both paths, buffering the values until stream cleanup.
Please also wait for #674 to land, then reuse its
set_input_tokens()andset_output_tokens()helpers for modality breakdowns. Keep the aggregate cache-write count separate.