fix: Issue where CodeAct agent was trying to log cost on local llm and throwing Undefined Model execption out of litellm (#1666)

* fix: Issue where CodeAct agent was trying to log cost on local llm and throwing Undefined Model execption out of litellm

* Review Feedback

* Missing None Check

* Review feedback and improved error handling

---------

Co-authored-by: Robert Brennan <accounts@rbren.io>
This commit is contained in:
Bart Shappee 2024-05-10 13:57:37 -04:00 committed by GitHub
parent 18e6b0b2d0
commit 78cd2e5b47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 8 deletions

View File

@ -18,7 +18,7 @@ from opendevin.events.observation import (
IPythonRunCellObservation,
NullObservation,
)
from opendevin.llm.llm import LLM, completion_cost
from opendevin.llm.llm import LLM
from opendevin.runtime.plugins import (
JupyterRequirement,
PluginRequirement,
@ -228,11 +228,9 @@ class CodeActAgent(Agent):
],
temperature=0.0,
)
cur_cost = completion_cost(completion_response=response)
self.cost_accumulator += cur_cost
logger.info(
f'Cost: {cur_cost:.2f} USD | Accumulated Cost: {self.cost_accumulator:.2f} USD'
)
self.log_cost(response)
action_str: str = parse_response(response)
state.num_of_chars += sum(
len(message['content']) for message in self.messages
@ -265,3 +263,12 @@ class CodeActAgent(Agent):
def search_memory(self, query: str) -> list[str]:
raise NotImplementedError('Implement this abstract method')
def log_cost(self, response):
cur_cost = self.llm.completion_cost(response)
self.cost_accumulator += cur_cost
logger.info(
'Cost: %.2f USD | Accumulated Cost: %.2f USD',
cur_cost,
self.cost_accumulator,
)

View File

@ -2,7 +2,7 @@ from functools import partial
import litellm
from litellm import completion as litellm_completion
from litellm import completion_cost
from litellm import completion_cost as litellm_completion_cost
from litellm.exceptions import (
APIConnectionError,
RateLimitError,
@ -19,7 +19,7 @@ from opendevin.core.config import config
from opendevin.core.logger import llm_prompt_logger, llm_response_logger
from opendevin.core.logger import opendevin_logger as logger
__all__ = ['LLM', 'completion_cost']
__all__ = ['LLM']
class LLM:
@ -207,6 +207,43 @@ class LLM:
"""
return litellm.token_counter(model=self.model_name, messages=messages)
def is_local(self):
"""
Determines if the system is using a locally running LLM.
Returns:
boolean: True if executing a local model.
"""
if self.base_url is not None:
if (
'localhost' not in self.base_url
and '127.0.0.1' not in self.base_url
and '0.0.0.0' not in self.base_url
):
return True
elif self.model_name is not None:
if self.model_name.startswith('ollama'):
return True
return False
def completion_cost(self, response):
"""
Calculate the cost of a completion response based on the model. Local models are treated as free.
Args:
response (list): A response from a model invocation.
Returns:
number: The cost of the response.
"""
if not self.is_local():
try:
cost = litellm_completion_cost(completion_response=response)
return cost
except Exception:
logger.warning('Cost calculation not supported for this model.')
return 0.0
def __str__(self):
if self.api_version:
return f'LLM(model={self.model_name}, api_version={self.api_version}, base_url={self.base_url})'