π OpenHands: AI-Driven Development
fix: Issue where CodeAct agent was trying to log cost on local llm and throwing Undefined Model execption out of litellm (#1666)
Bart Shappee78cd2e5b47b3
diff --git a/agenthub/codeact_agent/codeact_agent.py b/agenthub/codeact_agent/codeact_agent.pyindex 5127b9fe5a..d3058dde5d 100644--- a/agenthub/codeact_agent/codeact_agent.py+++ b/agenthub/codeact_agent/codeact_agent.py@@ -18,7 +18,7 @@ from opendevin.events.observation import (IPythonRunCellObservation,NullObservation,)-from opendevin.llm.llm import LLM, completion_cost+from opendevin.llm.llm import LLMfrom 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,+ )diff --git a/opendevin/llm/llm.py b/opendevin/llm/llm.pyindex 7f6333a22a..be7560ba9d 100644--- a/opendevin/llm/llm.py+++ b/opendevin/llm/llm.py@@ -2,7 +2,7 @@ from functools import partialimport litellmfrom litellm import completion as litellm_completion-from litellm import completion_cost+from litellm import completion_cost as litellm_completion_costfrom litellm.exceptions import (APIConnectionError,RateLimitError,@@ -19,7 +19,7 @@ from opendevin.core.config import configfrom opendevin.core.logger import llm_prompt_logger, llm_response_loggerfrom 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})'