@@ -23,6 +23,10 @@ class OpenAIMonitor:
23
23
Whether to publish the data to Openlayer as soon as it is available. If True,
24
24
the Openlayer credentials must be provided (either as keyword arguments or as
25
25
environment variables).
26
+ accumulate_data : bool, False
27
+ Whether to accumulate the data in a dataframe. If False (default), only the
28
+ latest request is stored. If True, all the requests are stored in a dataframe,
29
+ accessed through the `data` attribute.
26
30
client : openai.api_client.Client, optional
27
31
The OpenAI client. It is required if you are using openai>=1.0.0.
28
32
openlayer_api_key : str, optional
@@ -101,6 +105,7 @@ def __init__(
101
105
self ,
102
106
publish : bool = False ,
103
107
client = None ,
108
+ accumulate_data : bool = False ,
104
109
openlayer_api_key : Optional [str ] = None ,
105
110
openlayer_project_name : Optional [str ] = None ,
106
111
openlayer_inference_pipeline_name : Optional [str ] = None ,
@@ -120,7 +125,7 @@ def __init__(
120
125
121
126
# OpenAI setup
122
127
self .openai_version = openai .__version__
123
- if self .openai_version .split ("." )[0 ] == "1" and client is None :
128
+ if self .openai_version .split ("." , maxsplit = 1 )[0 ] == "1" and client is None :
124
129
raise ValueError (
125
130
"You must provide the OpenAI client for as the kwarg `client` for"
126
131
" openai>=1.0.0."
@@ -134,6 +139,7 @@ def __init__(
134
139
135
140
self .df = pd .DataFrame (columns = ["input" , "output" , "tokens" , "latency" ])
136
141
self .publish = publish
142
+ self .accumulate_data = accumulate_data
137
143
self .monitoring_on = False
138
144
139
145
def _initialize_openlayer (
@@ -191,7 +197,7 @@ def _load_inference_pipeline(self) -> None:
191
197
192
198
def _initialize_openai (self ) -> None :
193
199
"""Initializes the OpenAI attributes."""
194
- if self .openai_version .split ("." )[0 ] == "0" :
200
+ if self .openai_version .split ("." , maxsplit = 1 )[0 ] == "0" :
195
201
openai_api_key = utils .get_env_variable ("OPENAI_API_KEY" )
196
202
openai .api_key = openai_api_key
197
203
self .create_chat_completion = openai .ChatCompletion .create
@@ -311,7 +317,10 @@ def _append_row_to_df(
311
317
}
312
318
]
313
319
)
314
- self .df = pd .concat ([self .df , row ], ignore_index = True )
320
+ if self .accumulate_data :
321
+ self .df = pd .concat ([self .df , row ], ignore_index = True )
322
+ else :
323
+ self .df = row
315
324
self .df = self .df .astype (
316
325
{"input" : object , "output" : object , "tokens" : int , "latency" : float }
317
326
)
@@ -350,7 +359,7 @@ def start_monitoring(self) -> None:
350
359
351
360
def _overwrite_completion_methods (self ) -> None :
352
361
"""Overwrites OpenAI's completion methods with the modified versions."""
353
- if self .openai_version .split ("." )[0 ] == "0" :
362
+ if self .openai_version .split ("." , maxsplit = 1 )[0 ] == "0" :
354
363
openai .ChatCompletion .create = self .modified_create_chat_completion
355
364
openai .Completion .create = self .modified_create_completion
356
365
else :
@@ -378,7 +387,7 @@ def stop_monitoring(self):
378
387
379
388
def _restore_completion_methods (self ) -> None :
380
389
"""Restores OpenAI's completion methods to their original versions."""
381
- if self .openai_version .split ("." )[0 ] == "0" :
390
+ if self .openai_version .split ("." , maxsplit = 1 )[0 ] == "0" :
382
391
openai .ChatCompletion .create = self .create_chat_completion
383
392
openai .Completion .create = self .create_completion
384
393
else :
0 commit comments