fix: Gracefully handling negative response latencies (#5660)

Co-authored-by: Calvin Smith <calvin@all-hands.dev>
This commit is contained in:
Calvin Smith 2024-12-17 17:43:41 -07:00 committed by GitHub
parent d16842f413
commit 8488dd2a03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -55,11 +55,9 @@ class Metrics:
self._costs.append(Cost(cost=value, model=self.model_name))
def add_response_latency(self, value: float, response_id: str) -> None:
if value < 0:
raise ValueError('Response latency cannot be negative.')
self._response_latencies.append(
ResponseLatency(
latency=value, model=self.model_name, response_id=response_id
latency=max(0.0, value), model=self.model_name, response_id=response_id
)
)

View File

@ -125,6 +125,15 @@ def test_response_latency_tracking(mock_time, mock_litellm_completion):
assert response['id'] == 'test-response-123'
assert response['choices'][0]['message']['content'] == 'Test response'
# To make sure the metrics fail gracefully, set the start/end time to go backwards.
mock_time.side_effect = [1000.0, 999.0]
llm.completion(messages=[{'role': 'user', 'content': 'Hello!'}])
# There should now be 2 latencies, the last of which has the value clipped to 0
assert len(llm.metrics.response_latencies) == 2
latency_record = llm.metrics.response_latencies[-1]
assert latency_record.latency == 0.0 # Should be lifted to 0 instead of being -1!
def test_llm_reset():
llm = LLM(LLMConfig(model='gpt-4o-mini', api_key='test_key'))