mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
fix: Gracefully handling negative response latencies (#5660)
Co-authored-by: Calvin Smith <calvin@all-hands.dev>
This commit is contained in:
parent
d16842f413
commit
8488dd2a03
@ -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
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@ -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'))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user