8000 Fix: Handling local models cost lookup issues by klieret · Pull Request #937 · SWE-agent/SWE-agent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix: Handling local models cost lookup issues #937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/installation/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 13 additions & 2 deletions sweagent/agent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def __init__(self, args: GenericAPIModelConfig, tools: ToolConfig):
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
Expand Down Expand Up @@ -649,7 +649,18 @@ def _single_query(
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 = []
Expand Down
Loading
0