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]
Added support for Bedrock-provided Claude models
Joao Galego978f2fb51264
diff --git a/Dockerfile b/Dockerfileindex 23f6e2c6..549886cd 100644--- a/Dockerfile+++ b/Dockerfile@@ -4,7 +4,7 @@ FROM python:3.9WORKDIR /app# Install Python dependencies-RUN pip install anthropic config datasets docker gymnasium numpy openai pandas rich ruamel.yaml swebench tenacity unidiff simple-parsing together ollama+RUN pip install anthropic[bedrock] config datasets docker gymnasium numpy openai pandas rich ruamel.yaml swebench tenacity unidiff simple-parsing together ollama# Install Docker CLI using the official Docker installation scriptRUN curl -fsSL https://get.docker.com -o get-docker.sh && \diff --git a/docker/requirements.txt b/docker/requirements.txtindex 04cf09e8..16d7c321 100644--- a/docker/requirements.txt+++ b/docker/requirements.txt@@ -1,3 +1,3 @@-anthropic+anthropic[bedrock]configopenai\ No newline at end of filediff --git a/environment.yml b/environment.ymlindex 19a5c8a4..45524339 100644--- a/environment.yml+++ b/environment.yml@@ -3,7 +3,7 @@ dependencies:- python=3.9- pip- pip:- - anthropic+ - anthropic[bedrock]- config- datasets- dockerdiff --git a/setup.py b/setup.pyindex af00bc3f..cae7c8b8 100644--- a/setup.py+++ b/setup.py@@ -27,7 +27,7 @@ setuptools.setup(],python_requires='>=3.9',install_requires=[- 'anthropic',+ 'anthropic[bedrock]','config','datasets','docker',diff --git a/sweagent/agent/README.md b/sweagent/agent/README.mdindex c74c1eb4..7101bbcc 100644--- a/sweagent/agent/README.md+++ b/sweagent/agent/README.md@@ -24,9 +24,10 @@ This file defines the abstraction for custom commands (non-native functions that#### `models.py`This file defines the abstraction for running inference on API models. In addition, the `BaseModel` abstraction also defines a set of cost-related fields for tracking instance-level and total expenses accumulated across a single model run.- `AnthropicModel`: Handles inference + cost logging for Anthropic Models+- `BedrockModel`: handles inference + cost logging for Amazon Bedrock-provided models (Anthropic Claude only)- `APIStats`: Cost tracking fields that are updated per model inference- `BaseModel`: Abstract class that defines the common logic for updating cost stats-- `get_model`: Returns initialized `[Anthropic|Human|OpenAI]Model` based on given arguments + commands+- `get_model`: Returns initialized `[Anthropic|Bedrock|Human|OpenAI]Model` based on given arguments + commands- `HumanModel`: Handles inference for human task worker- `ModelArguments`: Model name, hyperparameter, and cost limit arguments- `OpenAIModel`: Handles inference + cost logging for OpenAI modelsdiff --git a/sweagent/agent/models.py b/sweagent/agent/models.pyindex 23646e02..b97d9e7b 100644--- a/sweagent/agent/models.py+++ b/sweagent/agent/models.py@@ -5,7 +5,7 @@ import osimport togetherfrom collections import defaultdict-from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT+from anthropic import Anthropic, AnthropicBedrock, HUMAN_PROMPT, AI_PROMPTfrom dataclasses import dataclass, fieldsfrom openai import BadRequestError, OpenAI, AzureOpenAIfrom simple_parsing.helpers.serialization.serializable import FrozenSerializable, Serializable@@ -101,6 +101,9 @@ class BaseModel:elif args.model_name.startswith("azure:"):azure_model = args.model_name.split("azure:", 1)[1]self.model_metadata = MODELS[azure_model]+ elif args.model_name.startswith("bedrock:"):+ self.api_model = args.model_name.split("bedrock:", 1)[1]+ self.model_metadata = MODELS[self.api_model]else:raise ValueError(f"Unregistered model ({args.model_name}). Add model name to MODELS metadata to {self.__class__}")@@ -425,6 +428,157 @@ class AnthropicModel(BaseModel):response = "\n".join([x.text for x in response.content])return response+class BedrockModel(BaseModel):+ MODELS = {+ "anthropic.claude-instant-v1": {+ "max_context": 100_000,+ "cost_per_input_token": 8e-07,+ "cost_per_output_token": 2.4e-06,+ },+ "anthropic.claude-v2": {+ "max_context": 100_000,+ "cost_per_input_token": 8e-06,+ "cost_per_output_token": 2.4e-05,+ },+ "anthropic.claude-v2:1": {+ "max_context": 100_000,+ "cost_per_input_token": 8e-06,+ "cost_per_output_token": 2.4e-05,+ },+ "anthropic.claude-3-sonnet-20240229-v1:0": {+ "max_context": 200_000,+ "max_tokens": 4096, # Max tokens to generate for Claude 3 models+ "cost_per_input_token": 3e-06,+ "cost_per_output_token": 1.5e-05,+ },+ "anthropic.claude-3-haiku-20240307-v1:0": {+ "max_context": 200_000,+ "max_tokens": 4096,+ "cost_per_input_token": 2.5e-07,+ "cost_per_output_token": 1.25e-06,+ },+ }++ def __init__(self, args: ModelArguments, commands: list[Command]):+ super().__init__(args, commands)++ model_provider = self.api_model.split('.')[0]+ if model_provider == "anthropic":+ # Note: this assumes AWS credentials are already configured.+ # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html+ self.api = AnthropicBedrock()+ else:+ raise NotImplementedError++ def history_to_messages(+ self, history: list[dict[str, str]], is_demonstration: bool = False+ ) -> list[dict[str, str]]:+ """+ Create `prompt` by filtering out all keys except for role/content per `history` turn+ Reference: https://docs.anthropic.com/claude/reference/complete_post+ """+ model_provider = self.api_model.split('.')[0]+ # Adapted from the AnthropicModel implementation+ if model_provider == "anthropic":+ # Preserve behavior for older models+ if self.api_model in ["anthropic.claude-instant-v1", "anthropic.claude-v2"]:+ # Remove system messages if it is a demonstration+ if is_demonstration:+ history = [entry for entry in history if entry["role"] != "system"]+ # Map history to Claude format+ prompt = "\n\n"+ for entry in history:+ if entry["role"] in {"user", "system"}:+ prompt += f'{HUMAN_PROMPT} {entry["content"]}\n\n'+ elif entry["role"] == "assistant":+ prompt += f'{AI_PROMPT} {entry["content"]}\n\n'+ prompt += AI_PROMPT+ return prompt++ # Remove system messages if it is a demonstration+ if is_demonstration:+ history = [entry for entry in history if entry["role"] != "system"]+ return '\n'.join([entry["content"] for entry in history])++ # Return history components with just role, content fields (no system message)+ messages = [+ {+ k: v for k, v in entry.items()+ if k in ["role", "content"]+ }+ for entry in history if entry["role"] != "system"+ ]+ compiled_messages = [] # Combine messages from the same role+ last_role = None+ for message in reversed(messages):+ if last_role == message["role"]:+ compiled_messages[-1]["content"] = message["content"] + "\n" + compiled_messages[-1]["content"]+ else:+ compiled_messages.append(message)+ last_role = message["role"]+ compiled_messages = list(reversed(compiled_messages))+ # Replace any empty content values with a "(No output)"+ for message in compiled_messages:+ if message["content"].strip() == "":+ message["content"] = "(No output)"+ return compiled_messages+ else:+ raise NotImplementedError++ @retry(+ wait=wait_random_exponential(min=1, max=15),+ reraise=True,+ stop=stop_after_attempt(3),+ retry=retry_if_not_exception_type((CostLimitExceededError, RuntimeError)),+ )+ def query(self, history: list[dict[str, str]]) -> str:+ """+ Query Amazon Bedrock with the given `history` and return the response.+ """+ model_provider = self.api_model.split('.')[0]+ if model_provider == "anthropic":+ # Preserve behavior for older models+ if self.api_model in ["anthropic.claude-instant-v1", "anthropic.claude-v2"]:+ # Perform Amazon Bedrock API call+ prompt = self.history_to_messages(history)+ input_tokens = self.api.count_tokens(prompt)+ completion = self.api.completions.create(+ model=self.api_model,+ prompt=prompt,+ max_tokens_to_sample=self.model_metadata["max_context"] - input_tokens,+ temperature=self.args.temperature,+ top_p=self.args.top_p,+ )+ # Calculate + update costs, return response+ response = completion.completion+ output_tokens = self.api.count_tokens(response)+ self.update_stats(input_tokens, output_tokens)+ return response++ # Get system message(s)+ system_message = "\n".join([+ entry["content"] for entry in history if entry["role"] == "system"+ ])+ messages = self.history_to_messages(history)+ # Perform Amazon Bedrock API call+ response = self.api.messages.create(+ messages=messages,+ max_tokens=self.model_metadata["max_tokens"],+ model=self.api_model,+ temperature=self.args.temperature,+ top_p=self.args.top_p,+ system=system_message,+ )++ # Calculate + update costs, return response+ self.update_stats(+ response.usage.input_tokens,+ response.usage.output_tokens+ )+ response = "\n".join([x.text for x in response.content])+ return response+ else:+ raise NotImplementedErrorclass OllamaModel(BaseModel):MODELS = defaultdict(lambda: {@@ -708,6 +862,8 @@ def get_model(args: ModelArguments, commands: Optional[list[Command]] = None):return OpenAIModel(args, commands)elif args.model_name.startswith("claude"):return AnthropicModel(args, commands)+ elif args.model_name.startswith("bedrock"):+ return BedrockModel(args, commands)elif args.model_name.startswith("ollama"):return OllamaModel(args, commands)elif args.model_name in TogetherModel.SHORTCUTS: