@@ -50,6 +50,10 @@ def count_tokens(s):
50
50
class OpenAIIntegration (Integration ):
51
51
identifier = "openai"
52
52
53
+ def __init__ (self , exclude_prompts = False ):
54
+ # type: (OpenAIIntegration, bool) -> None
55
+ self .exclude_prompts = exclude_prompts
56
+
53
57
@staticmethod
54
58
def setup_once ():
55
59
# type: () -> None
@@ -122,6 +126,14 @@ def _wrap_chat_completion_create(f):
122
126
@wraps (f )
123
127
def new_chat_completion (* args , ** kwargs ):
124
128
# type: (*Any, **Any) -> Any
129
+ hub = Hub .current
130
+ if not hub :
131
+ return f (* args , ** kwargs )
132
+
133
+ integration = hub .get_integration (OpenAIIntegration ) # type: OpenAIIntegration
134
+ if not integration :
135
+ return f (* args , ** kwargs )
136
+
125
137
if "messages" not in kwargs :
126
138
# invalid call (in all versions of openai), let it return error
127
139
return f (* args , ** kwargs )
@@ -149,13 +161,13 @@ def new_chat_completion(*args, **kwargs):
149
161
raise e from None
150
162
151
163
with capture_internal_exceptions ():
152
- if _should_send_default_pii ():
164
+ if _should_send_default_pii () or not integration . exclude_prompts :
153
165
span .set_data ("ai.input_messages" , messages )
154
166
span .set_data ("ai.model_id" , model )
155
167
span .set_data ("ai.streaming" , streaming )
156
168
157
169
if hasattr (res , "choices" ):
158
- if _should_send_default_pii ():
170
+ if _should_send_default_pii () or not integration . exclude_prompts :
159
171
span .set_data (
160
172
"ai.responses" , list (map (lambda x : x .message , res .choices ))
161
173
)
@@ -186,7 +198,10 @@ def new_iterator():
186
198
all_responses = list (
187
199
map (lambda chunk : "" .join (chunk ), data_buf )
188
200
)
189
- if _should_send_default_pii ():
201
+ if (
202
+ _should_send_default_pii ()
203
+ or not integration .exclude_prompts
204
+ ):
190
205
span .set_data ("ai.responses" , all_responses )
191
206
_calculate_chat_completion_usage (
192
207
messages , res , span , all_responses
@@ -208,11 +223,22 @@ def _wrap_embeddings_create(f):
208
223
@wraps (f )
209
224
def new_embeddings_create (* args , ** kwargs ):
210
225
# type: (*Any, **Any) -> Any
226
+
227
+ hub = Hub .current
228
+ if not hub :
229
+ return f (* args , ** kwargs )
230
+
231
+ integration = hub .get_integration (OpenAIIntegration ) # type: OpenAIIntegration
232
+ if not integration :
233
+ return f (* args , ** kwargs )
234
+
211
235
with sentry_sdk .start_span (
212
236
op = consts .OP .OPENAI_EMBEDDINGS_CREATE ,
213
237
description = "OpenAI Embedding Creation" ,
214
238
) as span :
215
- if "input" in kwargs :
239
+ if "input" in kwargs and (
240
+ _should_send_default_pii () or not integration .exclude_prompts
241
+ ):
216
242
if isinstance (kwargs ["input" ], str ):
217
243
span .set_data ("ai.input_messages" , [kwargs ["input" ]])
218
244
elif (
0 commit comments