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]

Enh: Allow to override max_input_tokens for local models (#992)

Kilian Lierete432182418fd
diff --git a/docs/installation/keys.md b/docs/installation/keys.md
index a2443868..e7e78c04 100644
--- a/docs/installation/keys.md
+++ b/docs/installation/keys.md
@@ -67,6 +67,7 @@ agent:
per_instance_cost_limit: 0 # (2)!
total_cost_limit: 0
per_instance_call_limit: 100
+ max_input_tokens: 0 # (3)!
tools:
# The default for obtaining actions from model outputs is function calling.
# If your local model does not support that, you can use the thought_action parser
@@ -77,6 +78,7 @@ agent:
1. Make sure that your model includes a "provider", i.e., follows the form `provider/model_name`. The model name and provider might be arbitrarily chosen.
2. We cannot track costs, so you must disable this (see below)
+3. Disable max input tokens check
in your [config file](../config/config.md).
If you're using a [litellm proxy](https://docs.litellm.ai/docs/providers/openai_compatible#usage-with-litellm-proxy-server), make sure to set your `agent.model.name` to `openai/...`
@@ -87,10 +89,11 @@ and set `agent.model.api_key` to the key you've configured for your proxy (or a
Make sure that your model name includes a "provider", i.e., follows the form `provider/model_name`. The model name and provider might be arbitrarily chosen
for local models.
-!!! warning "Cost limits"
+!!! warning "Cost/token limits"
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 make sure to the set the `per_instance_cost_limit` to 0 and use the `per_instance_call_limit` instead to limit the runtime per issue.
+ Please also make sure to set `max_input_tokens` to a non-`None` value to avoid a lot of warnings.
!!! warning "Parsing functions"
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index aa0bcd03..6603a304 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -121,6 +121,14 @@ class GenericAPIModelConfig(PydanticBaseModel):
run-batch, we use the same API key within a single-thread so that prompt caching still works.
"""
+ max_input_tokens: int | None = None
+ """If set, this will override the max input tokens for the model that we usually look
+ up from `litellm.model_cost`.
+ Use this for local models or if you want to set a custom max input token limit.
+ If this value is exceeded, a `ContextWindowExceededError` will be raised.
+ Set this to 0 to disable this check.
+ """
+
# pydantic
model_config = ConfigDict(extra="forbid")
@@ -551,7 +559,9 @@ class LiteLLMModel(AbstractModel):
"See https://swe-agent.com/latest/faq/ for more information."
)
raise ModelConfigurationError(msg)
- self.model_max_input_tokens = litellm.model_cost.get(self.config.name, {}).get("max_input_tokens")
+ self.model_max_input_tokens = self.config.max_input_tokens or 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.get(self.config.name, {}).get("litellm_provider")
self.logger = get_logger("swea-lm", emoji="🤖")
@@ -614,8 +624,12 @@ class LiteLLMModel(AbstractModel):
self._sleep()
input_tokens: int = litellm.utils.token_counter(messages=messages, model=self.config.name)
if self.model_max_input_tokens is None:
- self.logger.warning(f"No max input tokens found for model {self.config.name!r}")
- elif input_tokens > self.model_max_input_tokens:
+ msg = (
+ f"No max input tokens found for model {self.config.name!r}. "
+ "If you are using a local model, you can set `max_input_token` in the model config to override this."
+ )
+ self.logger.warning(msg)
+ elif input_tokens > self.model_max_input_tokens > 0:
msg = f"Input tokens {input_tokens} exceed max tokens {self.model_max_input_tokens}"
raise ContextWindowExceededError(msg)
extra_args = {}