|
1 | 1 | import sys |
2 | 2 | from functools import wraps |
3 | 3 |
|
4 | | -from sentry_sdk.ai.monitoring import record_token_usage |
5 | 4 | from sentry_sdk.consts import OP, SPANDATA |
6 | | -from sentry_sdk.ai.span_config import set_request_span_data |
| 5 | +from sentry_sdk.ai.span_config import set_request_span_data, set_response_span_data |
7 | 6 |
|
8 | 7 | from typing import TYPE_CHECKING |
9 | 8 |
|
@@ -42,6 +41,12 @@ def _normalize_embedding_input(texts): |
42 | 41 | ), |
43 | 42 | "message_target": SPANDATA.GEN_AI_EMBEDDINGS_INPUT, |
44 | 43 | "truncation_fn": None, |
| 44 | + "response": { |
| 45 | + "usage": { |
| 46 | + "input_tokens": [("meta", "billed_units", "input_tokens")], |
| 47 | + "total_tokens": [("meta", "billed_units", "input_tokens")], |
| 48 | + }, |
| 49 | + }, |
45 | 50 | } |
46 | 51 |
|
47 | 52 |
|
@@ -102,16 +107,9 @@ def new_embed(*args, **kwargs): |
102 | 107 | _capture_exception(e) |
103 | 108 | reraise(*exc_info) |
104 | 109 |
|
105 | | - if ( |
106 | | - hasattr(res, "meta") |
107 | | - and hasattr(res.meta, "billed_units") |
108 | | - and hasattr(res.meta.billed_units, "input_tokens") |
109 | | - ): |
110 | | - record_token_usage( |
111 | | - span, |
112 | | - input_tokens=res.meta.billed_units.input_tokens, |
113 | | - total_tokens=res.meta.billed_units.input_tokens, |
114 | | - ) |
| 110 | + set_response_span_data( |
| 111 | + span, res, False, COHERE_EMBED_CONFIG["response"] |
| 112 | + ) |
115 | 113 | return res |
116 | 114 |
|
117 | 115 | return new_embed |
0 commit comments