Skip to content

Commit 88cd97e

Browse files
committed
Rename attributes
1 parent 1d473b6 commit 88cd97e

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

sentry_sdk/consts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ class OP:
794794
GEN_AI_CHAT = "gen_ai.chat"
795795
GEN_AI_EMBEDDINGS = "gen_ai.embeddings"
796796
GEN_AI_EXECUTE_TOOL = "gen_ai.execute_tool"
797+
GEN_AI_GENERATE_TEXT = "gen_ai.generate_text"
797798
GEN_AI_HANDOFF = "gen_ai.handoff"
798799
GEN_AI_PIPELINE = "gen_ai.pipeline"
799800
GEN_AI_INVOKE_AGENT = "gen_ai.invoke_agent"

sentry_sdk/integrations/huggingface_hub.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from functools import wraps
22

3-
from sentry_sdk import consts
4-
from sentry_sdk.ai.monitoring import record_token_usage
5-
from sentry_sdk.ai.utils import set_data_normalized
6-
from sentry_sdk.consts import SPANDATA
73

84
from typing import Any, Iterable, Callable
95

106
import sentry_sdk
11-
from sentry_sdk.scope import should_send_default_pii
7+
from sentry_sdk.ai.monitoring import record_token_usage
8+
from sentry_sdk.ai.utils import set_data_normalized
9+
from sentry_sdk.consts import OP, SPANDATA
1210
from sentry_sdk.integrations import DidNotEnable, Integration
11+
from sentry_sdk.scope import should_send_default_pii
1312
from sentry_sdk.utils import (
1413
capture_internal_exceptions,
1514
event_from_exception,
@@ -34,6 +33,8 @@ def __init__(self, include_prompts=True):
3433
@staticmethod
3534
def setup_once():
3635
# type: () -> None
36+
37+
# Other tasks that can be called: https://huggingface.co/docs/huggingface_hub/guides/inference#supported-providers-and-tasks
3738
huggingface_hub.inference._client.InferenceClient.text_generation = (
3839
_wrap_text_generation(
3940
huggingface_hub.inference._client.InferenceClient.text_generation
@@ -70,15 +71,22 @@ def new_text_generation(*args, **kwargs):
7071
# invalid call, let it return error
7172
return f(*args, **kwargs)
7273

73-
model = kwargs.get("model")
74+
client = args[0]
75+
model = client.model or kwargs.get("model") or ""
7476
streaming = kwargs.get("stream")
7577

7678
span = sentry_sdk.start_span(
77-
op=consts.OP.HUGGINGFACE_HUB_CHAT_COMPLETIONS_CREATE,
78-
name="Text Generation",
79+
op=OP.GEN_AI_GENERATE_TEXT,
80+
name=f"generate_text {model}",
7981
origin=HuggingfaceHubIntegration.origin,
8082
)
8183
span.__enter__()
84+
85+
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "generate_text")
86+
if model:
87+
span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, model)
88+
span.set_data(SPANDATA.GEN_AI_SYSTEM, "TODO!!!!!")
89+
8290
try:
8391
res = f(*args, **kwargs)
8492
except Exception as e:
@@ -88,16 +96,15 @@ def new_text_generation(*args, **kwargs):
8896

8997
with capture_internal_exceptions():
9098
if should_send_default_pii() and integration.include_prompts:
91-
set_data_normalized(span, SPANDATA.AI_INPUT_MESSAGES, prompt)
99+
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompt)
92100

93-
set_data_normalized(span, SPANDATA.AI_MODEL_ID, model)
94-
set_data_normalized(span, SPANDATA.AI_STREAMING, streaming)
101+
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, streaming)
95102

96103
if isinstance(res, str):
97104
if should_send_default_pii() and integration.include_prompts:
98105
set_data_normalized(
99106
span,
100-
SPANDATA.AI_RESPONSES,
107+
SPANDATA.GEN_AI_RESPONSE_TEXT,
101108
[res],
102109
)
103110
span.__exit__(None, None, None)
@@ -107,7 +114,7 @@ def new_text_generation(*args, **kwargs):
107114
if should_send_default_pii() and integration.include_prompts:
108115
set_data_normalized(
109116
span,
110-
SPANDATA.AI_RESPONSES,
117+
SPANDATA.GEN_AI_RESPONSE_TEXT,
111118
[res.generated_text],
112119
)
113120
if res.details is not None and res.details.generated_tokens > 0:
@@ -120,7 +127,6 @@ def new_text_generation(*args, **kwargs):
120127

121128
if not isinstance(res, Iterable):
122129
# we only know how to deal with strings and iterables, ignore
123-
set_data_normalized(span, "unknown_response", True)
124130
span.__exit__(None, None, None)
125131
return res
126132

@@ -145,7 +151,7 @@ def new_details_iterator():
145151
and integration.include_prompts
146152
):
147153
set_data_normalized(
148-
span, SPANDATA.AI_RESPONSES, "".join(data_buf)
154+
span, SPANDATA.GEN_AI_RESPONSE_TEXT, "".join(data_buf)
149155
)
150156
if tokens_used > 0:
151157
record_token_usage(
@@ -172,7 +178,7 @@ def new_iterator():
172178
and integration.include_prompts
173179
):
174180
set_data_normalized(
175-
span, SPANDATA.AI_RESPONSES, "".join(data_buf)
181+
span, SPANDATA.GEN_AI_RESPONSE_TEXT, "".join(data_buf)
176182
)
177183
span.__exit__(None, None, None)
178184

0 commit comments

Comments
 (0)