james-d-taboola commited on
Commit
7c6e6d4
·
1 Parent(s): 122b794

feat: modify Global TPM calculation

Browse files
Files changed (1) hide show
  1. app/utils/global_tpm_tracker.py +25 -11
app/utils/global_tpm_tracker.py CHANGED
@@ -111,24 +111,38 @@ class GlobalTPMTracker:
111
  # Calculate elapsed time
112
  elapsed_time = current_time - self._start_time
113
  elapsed_seconds = elapsed_time.total_seconds()
 
 
 
 
114
 
115
  # Get cumulative token counts
116
- cumulative_prompt = self._global_tokens['prompt_tokens']
117
- cumulative_total = self._global_tokens['total_tokens']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  print(f"Global TPM - Current usage: Prompt tokens: {current_prompt_tokens}, Total tokens: {current_total_tokens}")
120
- print(f"Global TPM - Global cumulative: Prompt tokens: {cumulative_prompt}, Total tokens: {cumulative_total}")
121
  print(f"Global TPM - Elapsed time: {elapsed_seconds:.2f} seconds")
122
 
123
  # Calculate TPM (only if more than 1 second has elapsed)
124
- if elapsed_seconds > 1:
125
- elapsed_minutes = elapsed_seconds / 60
126
- prompt_tpm = cumulative_prompt / elapsed_minutes
127
- total_tpm = cumulative_total / elapsed_minutes
128
- print(f"Global TPM - Prompt TPM: {prompt_tpm:.2f} tokens/minute")
129
- print(f"Global TPM - Total TPM: {total_tpm:.2f} tokens/minute")
130
- else:
131
- print(f"Global TPM - TPM: N/A (insufficient time elapsed)")
132
 
133
  print("-" * 50)
134
 
 
111
  # Calculate elapsed time
112
  elapsed_time = current_time - self._start_time
113
  elapsed_seconds = elapsed_time.total_seconds()
114
+
115
+
116
+ # Calculate time threshold for records within timeout window
117
+ time_threshold = current_time - timedelta(seconds=self._timeout_seconds)
118
 
119
  # Get cumulative token counts
120
+ total_cumulative_prompt = self._global_tokens['prompt_tokens']
121
+ total_cumulative_total = self._global_tokens['total_tokens']
122
+
123
+
124
+ # Get cumulative token counts from records within timeout window
125
+ cumulative_prompt = 0
126
+ cumulative_total = 0
127
+
128
+ for record in self._records:
129
+ # Parse the record time string back to datetime for comparison
130
+ record_time = datetime.strptime(record['current_time'], '%Y-%m-%d %H:%M:%S.%f')
131
+
132
+ # Check if record is within the timeout window
133
+ if record_time >= time_threshold:
134
+ cumulative_prompt += record['prompt_tokens']
135
+ cumulative_total += record['total_tokens']
136
 
137
  print(f"Global TPM - Current usage: Prompt tokens: {current_prompt_tokens}, Total tokens: {current_total_tokens}")
138
+ print(f"Global TPM - Global cumulative: Prompt tokens: {total_cumulative_prompt}, Total tokens: {total_cumulative_total}")
139
  print(f"Global TPM - Elapsed time: {elapsed_seconds:.2f} seconds")
140
 
141
  # Calculate TPM (only if more than 1 second has elapsed)
142
+ prompt_tpm = cumulative_prompt
143
+ total_tpm = cumulative_total
144
+ print(f"Global TPM - Prompt TPM: {prompt_tpm:.2f} tokens/minute")
145
+ print(f"Global TPM - Total TPM: {total_tpm:.2f} tokens/minute")
 
 
 
 
146
 
147
  print("-" * 50)
148