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: Use correct local model tokenizer for computing token consumption

Tobias Lindenbauer34f9f687b23e
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index 86bc0351..a0098889 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -577,6 +577,7 @@ class LiteLLMModel(AbstractModel):
self.stats = InstanceStats()
self.tools = tools
self.logger = get_logger("swea-lm", emoji="🤖")
+ self.custom_tokenizer = None
if tools.use_function_calling:
if not litellm.utils.supports_function_calling(model=self.config.name):
@@ -613,7 +614,30 @@ class LiteLLMModel(AbstractModel):
"completion_kwargs to {'extra_headers': {'anthropic-beta': 'output-128k-2025-02-19'}}."
)
- self.lm_provider = litellm.model_cost.get(self.config.name, {}).get("litellm_provider")
+ self.lm_provider = litellm.model_cost.get(self.config.name, {}).get("litellm_provider", self.config.name)
+ if self.config.per_instance_cost_limit == 0 and self.config.total_cost_limit == 0: # Local model
+ if "/" in self.lm_provider:
+ from transformers import AutoTokenizer
+
+ self.custom_tokenizer = {}
+ self.custom_tokenizer["provider"] = self.lm_provider.split("/")[0]
+
+ if self.custom_tokenizer["provider"] not in litellm.provider_list:
+ self.logger.warning(
+ f"Local model {self.lm_provider} not found in LiteLLM provider list. Using default tokenizer."
+ )
+ self.custom_tokenizer = None
+ else:
+ self.custom_tokenizer["identifier"] = "/".join(self.lm_provider.split("/")[1:])
+ self.custom_tokenizer["type"] = "huggingface_tokenizer"
+ # Use backend tokenizer as workaround for litellm HF tokenizer bug
+ self.custom_tokenizer["tokenizer"] = AutoTokenizer.from_pretrained(
+ self.custom_tokenizer["identifier"]
+ ).backend_tokenizer
+ else:
+ self.logger.warning(
+ f"Local model identifier {self.lm_provider} has an unknown format. Using default tokenizer."
+ )
@property
def instance_cost_limit(self) -> float:
@@ -676,7 +700,11 @@ class LiteLLMModel(AbstractModel):
for message in messages_no_cache_control:
if "cache_control" in message:
del message["cache_control"]
- input_tokens: int = litellm.utils.token_counter(messages=messages_no_cache_control, model=self.config.name)
+ input_tokens: int = litellm.utils.token_counter(
+ messages=messages_no_cache_control,
+ model=self.custom_tokenizer["identifier"] if self.custom_tokenizer else self.config.name,
+ custom_tokenizer=self.custom_tokenizer,
+ )
if self.model_max_input_tokens is None:
msg = (
f"No max input tokens found for model {self.config.name!r}. "
@@ -737,7 +765,11 @@ class LiteLLMModel(AbstractModel):
output_tokens = 0
for i in range(n_choices):
output = choices[i].message.content or ""
- output_tokens += litellm.utils.token_counter(text=output, model=self.config.name)
+ output_tokens += litellm.utils.token_counter(
+ text=output,
+ model=self.custom_tokenizer["identifier"] if self.custom_tokenizer else self.config.name,
+ custom_tokenizer=self.custom_tokenizer,
+ )
output_dict = {"message": output}
if self.tools.use_function_calling:
if response.choices[i].message.tool_calls: # type: ignore