Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #5383: [Bug]: LLM Cost is added to the metrics twice #5396

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions openhands/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ def wrapper(*args, **kwargs):
)
resp.choices[0].message = fn_call_response_message

message_back: str = resp['choices'][0]['message']['content'] or ''
tool_calls = resp['choices'][0]['message'].get('tool_calls', [])
if tool_calls:
for tool_call in tool_calls:
fn_name = tool_call.function.name
fn_args = tool_call.function.arguments
message_back += f'\nFunction call: {fn_name}({fn_args})'

# log the LLM response
self.log_response(message_back)

# post-process the response first to calculate cost
self._post_completion(resp)
enyst marked this conversation as resolved.
Show resolved Hide resolved

# log for evals or other scripts that need the raw completion
if self.config.log_completions:
assert self.config.log_completions_folder is not None
Expand All @@ -228,13 +242,15 @@ def wrapper(*args, **kwargs):
f'{self.metrics.model_name.replace("/", "__")}-{time.time()}.json',
)

# Get cost from metrics instead of recalculating
cost = self.metrics.costs[-1]['cost'] if self.metrics.costs else 0.0
_d = {
'messages': messages,
'response': resp,
'args': args,
'kwargs': {k: v for k, v in kwargs.items() if k != 'messages'},
'timestamp': time.time(),
'cost': self._completion_cost(resp),
'cost': cost,
}
if mock_function_calling:
# Overwrite response as non-fncall to be consistent with `messages``
Expand All @@ -245,20 +261,6 @@ def wrapper(*args, **kwargs):
with open(log_file, 'w') as f:
f.write(json.dumps(_d))

message_back: str = resp['choices'][0]['message']['content'] or ''
tool_calls = resp['choices'][0]['message'].get('tool_calls', [])
if tool_calls:
for tool_call in tool_calls:
fn_name = tool_call.function.name
fn_args = tool_call.function.arguments
message_back += f'\nFunction call: {fn_name}({fn_args})'

# log the LLM response
self.log_response(message_back)

# post-process the response
self._post_completion(resp)

return resp
except APIError as e:
if 'Attention Required! | Cloudflare' in str(e):
Expand Down Expand Up @@ -534,7 +536,8 @@ def _completion_cost(self, response) -> float:
cost = litellm_completion_cost(
completion_response=response, **extra_kwargs
)
self.metrics.add_cost(cost)
# Only add cost to metrics if we're calculating it for the first time
self.metrics.add_cost(cost)
return cost
enyst marked this conversation as resolved.
Show resolved Hide resolved
except Exception:
self.cost_metric_supported = False
Expand Down
Loading