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]
Models: Allow to rotate keys
Kilian Lieretb24d6748b264
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.pyindex 3321370c..4e3f096b 100644--- a/sweagent/agent/models.py+++ b/sweagent/agent/models.py@@ -1,6 +1,7 @@from __future__ import annotationsimport json+import osimport randomimport shleximport time@@ -67,6 +68,9 @@ class GenericAPIModelConfig(PydanticBaseModel):api_key: SecretStr | None = None"""API key to the model. We recommend using environment variables to set this insteador putting your environment variables in a `.env` file.+ If value starts with `$`, we will interpret it as an environment variable.+ You can concatenate more than one environment variable by separating them with `:`, e.g.,+ `$key1:$key2`."""stop: list[str] = []"""Custom stop sequences"""@@ -99,6 +103,26 @@ class GenericAPIModelConfig(PydanticBaseModel):# pydanticmodel_config = ConfigDict(extra="forbid")+ def get_api_keys(self) -> list[str]:+ if self.api_key is None:+ return []+ v = self.api_key.get_secret_value()+ if "$" not in v:+ return [v]+ individual_keys = v.split(":")+ if not all(k.startswith("$") for k in individual_keys):+ msg = "All keys must start with $ when concatenating keys"+ raise ValueError(msg)+ keys = []+ for k in individual_keys:+ k = k.removeprefix("$")+ v = os.getenv(k)+ if v is None:+ msg = f"Environment variable {k} not set"+ raise ValueError(msg)+ keys.append(v)+ return keys+@propertydef id(self) -> str:return f"{self.name}__t-{self.temperature:.2f}__p-{self.top_p:.2f}__c-{self.per_instance_cost_limit:.2f}"@@ -462,6 +486,12 @@ class LiteLLMModel(AbstractModel):msg = "Instance cost limit exceeded"raise InstanceCostLimitExceededError(msg)+ def _get_api_key(self) -> str | None:+ api_keys = self.args.get_api_keys()+ if not api_keys:+ return None+ return random.choice(api_keys)+def _query(self, messages: list[dict[str, str]]) -> dict:input_tokens: int = litellm.utils.token_counter(messages=messages, model=self.args.name)if self.model_max_input_tokens is None:@@ -485,7 +515,7 @@ class LiteLLMModel(AbstractModel):temperature=self.args.temperature,top_p=self.args.top_p,api_version=self.args.api_version,- api_key=self.args.api_key.get_secret_value() if self.args.api_key else None,+ api_key=self._get_api_key(),fallbacks=self.args.fallbacks,**completion_kwargs,**extra_args,