opentelemetry-instrumentation-openai 0.42.0__tar.gz → 0.43.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of opentelemetry-instrumentation-openai might be problematic. Click here for more details.
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/PKG-INFO +1 -1
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/__init__.py +6 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/chat_wrappers.py +37 -28
- opentelemetry_instrumentation_openai-0.43.1/opentelemetry/instrumentation/openai/version.py +1 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/pyproject.toml +1 -1
- opentelemetry_instrumentation_openai-0.42.0/opentelemetry/instrumentation/openai/version.py +0 -1
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/README.md +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/__init__.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/completion_wrappers.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/config.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/embeddings_wrappers.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/event_emitter.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/event_models.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/image_gen_wrappers.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/shared/span_utils.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/utils.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/v0/__init__.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/v1/__init__.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/v1/assistant_wrappers.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/v1/event_handler_wrapper.py +0 -0
- {opentelemetry_instrumentation_openai-0.42.0 → opentelemetry_instrumentation_openai-0.43.1}/opentelemetry/instrumentation/openai/v1/responses_wrappers.py +0 -0
|
@@ -370,6 +370,12 @@ def get_token_count_from_string(string: str, model_name: str):
|
|
|
370
370
|
f"Failed to get tiktoken encoding for model_name {model_name}, error: {str(ex)}"
|
|
371
371
|
)
|
|
372
372
|
return None
|
|
373
|
+
except Exception as ex:
|
|
374
|
+
# Other exceptions in tiktok
|
|
375
|
+
logger.warning(
|
|
376
|
+
f"Failed to get tiktoken encoding for model_name {model_name}, error: {str(ex)}"
|
|
377
|
+
)
|
|
378
|
+
return None
|
|
373
379
|
|
|
374
380
|
tiktoken_encodings[model_name] = encoding
|
|
375
381
|
else:
|
|
@@ -516,43 +516,45 @@ def _set_completions(span, choices):
|
|
|
516
516
|
def _set_streaming_token_metrics(
|
|
517
517
|
request_kwargs, complete_response, span, token_counter, shared_attributes
|
|
518
518
|
):
|
|
519
|
-
# use tiktoken calculate token usage
|
|
520
519
|
if not should_record_stream_token_usage():
|
|
521
520
|
return
|
|
522
521
|
|
|
523
|
-
# kwargs={'model': 'gpt-3.5', 'messages': [{'role': 'user', 'content': '...'}], 'stream': True}
|
|
524
522
|
prompt_usage = -1
|
|
525
523
|
completion_usage = -1
|
|
526
524
|
|
|
527
|
-
#
|
|
528
|
-
if
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
525
|
+
# First, try to get usage from API response
|
|
526
|
+
if complete_response.get("usage"):
|
|
527
|
+
usage = complete_response["usage"]
|
|
528
|
+
if usage.get("prompt_tokens"):
|
|
529
|
+
prompt_usage = usage["prompt_tokens"]
|
|
530
|
+
if usage.get("completion_tokens"):
|
|
531
|
+
completion_usage = usage["completion_tokens"]
|
|
532
|
+
|
|
533
|
+
# If API response doesn't have usage, fallback to tiktoken calculation
|
|
534
|
+
if prompt_usage == -1 or completion_usage == -1:
|
|
532
535
|
model_name = (
|
|
533
536
|
complete_response.get("model") or request_kwargs.get("model") or "gpt-4"
|
|
534
537
|
)
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
#
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
)
|
|
538
|
+
|
|
539
|
+
# Calculate prompt tokens if not available from API
|
|
540
|
+
if prompt_usage == -1 and request_kwargs and request_kwargs.get("messages"):
|
|
541
|
+
prompt_content = ""
|
|
542
|
+
for msg in request_kwargs.get("messages"):
|
|
543
|
+
if msg.get("content"):
|
|
544
|
+
prompt_content += msg.get("content")
|
|
545
|
+
if model_name and should_record_stream_token_usage():
|
|
546
|
+
prompt_usage = get_token_count_from_string(prompt_content, model_name)
|
|
547
|
+
|
|
548
|
+
# Calculate completion tokens if not available from API
|
|
549
|
+
if completion_usage == -1 and complete_response.get("choices"):
|
|
550
|
+
completion_content = ""
|
|
551
|
+
for choice in complete_response.get("choices"):
|
|
552
|
+
if choice.get("message") and choice.get("message").get("content"):
|
|
553
|
+
completion_content += choice["message"]["content"]
|
|
554
|
+
if model_name and should_record_stream_token_usage():
|
|
555
|
+
completion_usage = get_token_count_from_string(
|
|
556
|
+
completion_content, model_name
|
|
557
|
+
)
|
|
556
558
|
|
|
557
559
|
# span record
|
|
558
560
|
_set_span_stream_usage(span, prompt_usage, completion_usage)
|
|
@@ -971,6 +973,13 @@ def _accumulate_stream_items(item, complete_response):
|
|
|
971
973
|
complete_response["model"] = item.get("model")
|
|
972
974
|
complete_response["id"] = item.get("id")
|
|
973
975
|
|
|
976
|
+
# capture usage information from the last stream chunks
|
|
977
|
+
if item.get("usage"):
|
|
978
|
+
complete_response["usage"] = item.get("usage")
|
|
979
|
+
elif item.get("choices") and item["choices"][0].get("usage"):
|
|
980
|
+
# Some LLM providers like moonshot mistakenly place token usage information within choices[0], handle this.
|
|
981
|
+
complete_response["usage"] = item["choices"][0].get("usage")
|
|
982
|
+
|
|
974
983
|
# prompt filter results
|
|
975
984
|
if item.get("prompt_filter_results"):
|
|
976
985
|
complete_response["prompt_filter_results"] = item.get("prompt_filter_results")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.43.1"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.42.0"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|