@@ -23,6 +23,10 @@ class OpenAIMonitor:
2323 Whether to publish the data to Openlayer as soon as it is available. If True,
2424 the Openlayer credentials must be provided (either as keyword arguments or as
2525 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.
2630 client : openai.api_client.Client, optional
2731 The OpenAI client. It is required if you are using openai>=1.0.0.
2832 openlayer_api_key : str, optional
@@ -101,6 +105,7 @@ def __init__(
101105 self ,
102106 publish : bool = False ,
103107 client = None ,
108+ accumulate_data : bool = False ,
104109 openlayer_api_key : Optional [str ] = None ,
105110 openlayer_project_name : Optional [str ] = None ,
106111 openlayer_inference_pipeline_name : Optional [str ] = None ,
@@ -120,7 +125,7 @@ def __init__(
120125
121126 # OpenAI setup
122127 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 :
124129 raise ValueError (
125130 "You must provide the OpenAI client for as the kwarg `client` for"
126131 " openai>=1.0.0."
@@ -134,6 +139,7 @@ def __init__(
134139
135140 self .df = pd .DataFrame (columns = ["input" , "output" , "tokens" , "latency" ])
136141 self .publish = publish
142+ self .accumulate_data = accumulate_data
137143 self .monitoring_on = False
138144
139145 def _initialize_openlayer (
@@ -191,7 +197,7 @@ def _load_inference_pipeline(self) -> None:
191197
192198 def _initialize_openai (self ) -> None :
193199 """Initializes the OpenAI attributes."""
194- if self .openai_version .split ("." )[0 ] == "0" :
200+ if self .openai_version .split ("." , maxsplit = 1 )[0 ] == "0" :
195201 openai_api_key = utils .get_env_variable ("OPENAI_API_KEY" )
196202 openai .api_key = openai_api_key
197203 self .create_chat_completion = openai .ChatCompletion .create
@@ -311,7 +317,10 @@ def _append_row_to_df(
311317 }
312318 ]
313319 )
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
315324 self .df = self .df .astype (
316325 {"input" : object , "output" : object , "tokens" : int , "latency" : float }
317326 )
@@ -350,7 +359,7 @@ def start_monitoring(self) -> None:
350359
351360 def _overwrite_completion_methods (self ) -> None :
352361 """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" :
354363 openai .ChatCompletion .create = self .modified_create_chat_completion
355364 openai .Completion .create = self .modified_create_completion
356365 else :
@@ -378,7 +387,7 @@ def stop_monitoring(self):
378387
379388 def _restore_completion_methods (self ) -> None :
380389 """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" :
382391 openai .ChatCompletion .create = self .create_chat_completion
383392 openai .Completion .create = self .create_completion
384393 else :
0 commit comments