Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Fix: Handling local models cost lookup issues (#937)

Kilian Lieret460ef5873adf
diff --git a/docs/installation/keys.md b/docs/installation/keys.md
index f102726f..dfd816e1 100644
--- a/docs/installation/keys.md
+++ b/docs/installation/keys.md
@@ -56,13 +56,15 @@ agent:
model:
name: ollama/llama2
api_base: http://localhost:11434
+ per_instance_cost_limit: 0
+ total_cost_limit: 0
+ per_instance_call_limit: 100
```
-Please see the above note about using a config that uses the `thought_action` parser instead of the function calling parser.
-
-!!! warning "Exit conditions"
+If you do not disable the default cost limits, you will see an error because the cost calculator will not be able to find the model in the `litellm` model cost dictionary.
+Please use the `per_instance_call_limit` instead to limit the runtime per issue.
- If your local model does not have a cost assigned to, you can use `agent.model.per_instance_call_limit` to limit the runtime per issue.
+Please see the above note about using a config that uses the `thought_action` parser instead of the function calling parser.
## Complete model options
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index dff55045..5aa4f069 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -552,7 +552,7 @@ class LiteLLMModel(AbstractModel):
raise ValueError(msg)
self.model_max_input_tokens = litellm.model_cost.get(self.config.name, {}).get("max_input_tokens")
self.model_max_output_tokens = litellm.model_cost.get(self.config.name, {}).get("max_output_tokens")
- self.lm_provider = litellm.model_cost[self.config.name]["litellm_provider"]
+ self.lm_provider = litellm.model_cost.get(self.config.name, {}).get("litellm_provider")
self.logger = get_logger("swea-lm", emoji="🤖")
@property
@@ -649,7 +649,18 @@ class LiteLLMModel(AbstractModel):
raise ContextWindowExceededError from e
raise
self.logger.info(f"Response: {response}")
- cost = litellm.cost_calculator.completion_cost(response)
+ try:
+ cost = litellm.cost_calculator.completion_cost(response)
+ except Exception as e:
+ self.logger.debug(f"Error calculating cost: {e}, setting cost to 0.")
+ if self.config.per_instance_cost_limit > 0 or self.config.total_cost_limit > 0:
+ msg = (
+ f"Error calculating cost: {e} for your model {self.config.name}. If this is ok "
+ "(local models, etc.), please make sure you set `per_instance_cost_limit` and "
+ "`total_cost_limit` to 0 to disable this safety check."
+ )
+ raise ValueError(msg)
+ cost = 0
choices: litellm.types.utils.Choices = response.choices # type: ignore
n_choices = n if n is not None else 1
outputs = []