@@ -36,13 +36,15 @@ def setup_once():
36
36
37
37
# Other tasks that can be called: https://huggingface.co/docs/huggingface_hub/guides/inference#supported-providers-and-tasks
38
38
huggingface_hub .inference ._client .InferenceClient .text_generation = (
39
- _wrap_text_generation (
40
- huggingface_hub .inference ._client .InferenceClient .text_generation
39
+ _wrap_huggingface_task (
40
+ huggingface_hub .inference ._client .InferenceClient .text_generation ,
41
+ OP .GEN_AI_GENERATE_TEXT ,
41
42
)
42
43
)
43
44
huggingface_hub .inference ._client .InferenceClient .chat_completion = (
44
- _wrap_text_generation (
45
- huggingface_hub .inference ._client .InferenceClient .chat_completion
45
+ _wrap_huggingface_task (
46
+ huggingface_hub .inference ._client .InferenceClient .chat_completion ,
47
+ OP .GEN_AI_CHAT ,
46
48
)
47
49
)
48
50
@@ -57,8 +59,8 @@ def _capture_exception(exc):
57
59
sentry_sdk .capture_event (event , hint = hint )
58
60
59
61
60
- def _wrap_text_generation ( f ):
61
- # type: (Callable[..., Any]) -> Callable[..., Any]
62
+ def _wrap_huggingface_task ( f , op ):
63
+ # type: (Callable[..., Any], str ) -> Callable[..., Any]
62
64
@wraps (f )
63
65
def new_text_generation (* args , ** kwargs ):
64
66
# type: (*Any, **Any) -> Any
@@ -81,21 +83,23 @@ def new_text_generation(*args, **kwargs):
81
83
client = args [0 ]
82
84
model = client .model or kwargs .get ("model" ) or ""
83
85
streaming = kwargs .get ("stream" )
86
+ operation_name = op .split ("." )[- 1 ]
84
87
85
88
span = sentry_sdk .start_span (
86
- op = OP . GEN_AI_GENERATE_TEXT ,
87
- name = f"generate_text { model } " ,
89
+ op = op ,
90
+ name = f"{ operation_name } { model } " ,
88
91
origin = HuggingfaceHubIntegration .origin ,
89
92
)
90
93
span .__enter__ ()
91
94
92
- span .set_data (SPANDATA .GEN_AI_OPERATION_NAME , "generate_text" )
95
+ span .set_data (SPANDATA .GEN_AI_OPERATION_NAME , operation_name )
93
96
if model :
94
97
span .set_data (SPANDATA .GEN_AI_REQUEST_MODEL , model )
95
98
96
99
try :
97
100
res = f (* args , ** kwargs )
98
101
except Exception as e :
102
+ span .set_status ("error" )
99
103
_capture_exception (e )
100
104
span .__exit__ (None , None , None )
101
105
raise e from None
@@ -120,9 +124,6 @@ def new_text_generation(*args, **kwargs):
120
124
121
125
if isinstance (res , TextGenerationOutput ):
122
126
if should_send_default_pii () and integration .include_prompts :
123
- import ipdb
124
-
125
- ipdb .set_trace ()
126
127
set_data_normalized (
127
128
span ,
128
129
SPANDATA .GEN_AI_RESPONSE_TEXT ,
0 commit comments