|
1 | 1 | import json |
2 | | -from typing import Any, Dict, List, Optional, Callable, Awaitable, Union |
3 | | -from langtrace.trace_attributes import ( |
4 | | - LLMSpanAttributes, |
5 | | - SpanAttributes, |
6 | | -) |
7 | | -from langtrace_python_sdk.utils import set_span_attribute |
8 | | -from langtrace_python_sdk.utils.silently_fail import silently_fail |
| 2 | +from typing import Any, Awaitable, Callable, Dict, List, Optional, Union |
| 3 | + |
| 4 | +from langtrace.trace_attributes import LLMSpanAttributes, SpanAttributes |
9 | 5 | from opentelemetry import trace |
10 | | -from opentelemetry.trace import SpanKind, Tracer, Span |
11 | | -from opentelemetry.trace.status import Status, StatusCode |
| 6 | +from opentelemetry.trace import Span, SpanKind, Tracer |
12 | 7 | from opentelemetry.trace.propagation import set_span_in_context |
13 | | -from langtrace_python_sdk.constants.instrumentation.common import ( |
14 | | - SERVICE_PROVIDERS, |
15 | | -) |
16 | | -from langtrace_python_sdk.constants.instrumentation.openai import APIS |
17 | | -from langtrace_python_sdk.utils.llm import ( |
18 | | - calculate_prompt_tokens, |
19 | | - get_base_url, |
20 | | - get_extra_attributes, |
21 | | - get_langtrace_attributes, |
22 | | - get_llm_request_attributes, |
23 | | - get_llm_url, |
24 | | - get_span_name, |
25 | | - get_tool_calls, |
26 | | - is_streaming, |
27 | | - set_event_completion, |
28 | | - StreamWrapper, |
29 | | - set_span_attributes, |
30 | | - set_usage_attributes, |
31 | | -) |
32 | | -from langtrace_python_sdk.types import NOT_GIVEN |
| 8 | +from opentelemetry.trace.status import Status, StatusCode |
33 | 9 |
|
| 10 | +from langtrace_python_sdk.constants.instrumentation.common import \ |
| 11 | + SERVICE_PROVIDERS |
| 12 | +from langtrace_python_sdk.constants.instrumentation.openai import APIS |
34 | 13 | from langtrace_python_sdk.instrumentation.openai.types import ( |
35 | | - ImagesGenerateKwargs, |
36 | | - ChatCompletionsCreateKwargs, |
37 | | - EmbeddingsCreateKwargs, |
38 | | - ImagesEditKwargs, |
39 | | - ResultType, |
40 | | - ContentItem, |
41 | | -) |
| 14 | + ChatCompletionsCreateKwargs, ContentItem, EmbeddingsCreateKwargs, |
| 15 | + ImagesEditKwargs, ImagesGenerateKwargs, ResultType) |
| 16 | +from langtrace_python_sdk.types import NOT_GIVEN |
| 17 | +from langtrace_python_sdk.utils import set_span_attribute |
| 18 | +from langtrace_python_sdk.utils.llm import (StreamWrapper, |
| 19 | + calculate_prompt_tokens, |
| 20 | + get_base_url, get_extra_attributes, |
| 21 | + get_langtrace_attributes, |
| 22 | + get_llm_request_attributes, |
| 23 | + get_llm_url, get_span_name, |
| 24 | + get_tool_calls, is_streaming, |
| 25 | + set_event_completion, |
| 26 | + set_span_attributes, |
| 27 | + set_usage_attributes) |
| 28 | +from langtrace_python_sdk.utils.silently_fail import silently_fail |
42 | 29 |
|
43 | 30 |
|
44 | 31 | def filter_valid_attributes(attributes): |
@@ -717,3 +704,8 @@ def _set_response_attributes(span: Span, result: ResultType) -> None: |
717 | 704 | SpanAttributes.LLM_USAGE_TOTAL_TOKENS, |
718 | 705 | result.usage.total_tokens, |
719 | 706 | ) |
| 707 | + set_span_attribute( |
| 708 | + span, |
| 709 | + "gen_ai.usage.cached_tokens", |
| 710 | + result.usage.prompt_tokens_details.cached_tokens if result.usage.prompt_tokens_details else 0, |
| 711 | + ) |
0 commit comments