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]

Switch to litellm for models

Kilian Liereta904c47c4c63
diff --git a/.gitignore b/.gitignore
index 0b663c83..7c515ab9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,11 @@
cov.xml
+trajectories/**
+!trajectories/demonstrations/**
+data/**
+*.env
+
+# -------- automatically generated files --------
+
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
@@ -186,12 +193,6 @@ website/frontend/build
# demo yamls (for editing)
*.demo.yaml
-# trajectory files
-trajectories/**
-!trajectories/demonstrations/**
-data/**
-.vscode/**
-
# PyCharm
.idea/
diff --git a/requirements.txt b/requirements.txt
index aef78033..555290d6 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,24 +1,15 @@
-anthropic[bedrock]
-config
datasets
-docker >= 7.1.0 # https://github.com/princeton-nlp/SWE-agent/issues/379
-gymnasium
numpy
-openai>=1.0
pandas
rich
ruamel.yaml
-swebench>=2.0.0
tenacity
unidiff
simple-parsing
# Versions of together below 1.1.0 are not compatible. see https://github.com/princeton-nlp/SWE-agent/issues/135
-together>=1.1.0
-ollama
rich-argparse
flask
flask-cors
flask-socketio
-groq
-omegaconf
-pydantic
\ No newline at end of file
+pydantic
+python-dotenv
\ No newline at end of file
diff --git a/sweagent/agent/agents.py b/sweagent/agent/agents.py
index 1c310135..0fcb8dd7 100644
--- a/sweagent/agent/agents.py
+++ b/sweagent/agent/agents.py
@@ -19,6 +19,7 @@ from sweagent.agent.models import (
APIStats,
ContextWindowExceededError,
CostLimitExceededError,
+ HumanModel,
ModelConfig,
get_model,
)
@@ -269,10 +270,8 @@ class Agent:
self._append_history(entry)
else:
# Add demonstration as single message to history
- demo_message = self.model.history_to_messages(
- demo_history,
- is_demonstration=True,
- )
+ demo_history = [entry for entry in demo_history if entry["role"] != "system"]
+ demo_message = "\n".join([entry["content"] for entry in demo_history])
assert self.templates.demonstration_template is not None
demonstration = self.templates.demonstration_template.format(demonstration=demo_message)
self._append_history(
@@ -459,11 +458,10 @@ class Agent:
output: raw model output
"""
# Condition for handling outputs with no thought (just action)
- if self.model.name == "human":
+ if isinstance(self.model, HumanModel):
+ # Need special attention, because HumanModel doesn't know how to format actions
+ # because it depends on the action parser in tools
return "", output, output
- elif self.model.name == "human_thought":
- thought, action = self.tools.parse_actions(output)
- return thought, action, output
format_fails = blocklist_fails = 0
@@ -658,7 +656,7 @@ class Agent:
)
self.trajectory.append(trajectory_step)
model_stats: APIStats = self.model.stats
- self.info["model_stats"] = model_stats.to_dict()
+ self.info["model_stats"] = model_stats.model_dump()
self._chook.on_step_done(trajectory_step=trajectory_step, model_stats=model_stats)
return observation, done
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index d70f2145..cdcc9cb8 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -1,34 +1,35 @@
from __future__ import annotations
-import copy
import json
-import logging
-from collections import defaultdict
-from dataclasses import dataclass, fields
+from abc import ABC, abstractmethod
from pathlib import Path
-from typing import Annotated, Literal
+from typing import Annotated, Any, Literal
-import together
-from anthropic import AI_PROMPT, HUMAN_PROMPT, Anthropic, AnthropicBedrock
-from groq import Groq
-from openai import AzureOpenAI, BadRequestError, OpenAI
+import litellm
+import litellm.types.utils
from pydantic import BaseModel as PydanticBaseModel
-from pydantic import ConfigDict, Field
-from simple_parsing.helpers.serialization.serializable import Serializable
+from pydantic import ConfigDict, Field, SecretStr
from tenacity import (
- retry,
+ Retrying,
retry_if_not_exception_type,
stop_after_attempt,
wait_random_exponential,
)
from sweagent.tools.commands import Command
-from sweagent.utils.config import keys_config
+from sweagent.types import History, HistoryItem
from sweagent.utils.log import get_logger
logger = get_logger("lm", emoji="🤖")
-_MAX_RETRIES = int(keys_config.get("SWE_AGENT_MODEL_MAX_RETRIES", 10))
+
+class RetryConfig(PydanticBaseModel):
+ retries: int = 5
+ """Number of retries"""
+ min_wait: float = 1
+ """Minimum wait time between retries (random exponential wait)"""
+ max_wait: float = 15
+ """Maximum wait time between retries (random exponential wait)"""
class GenericAPIModelConfig(PydanticBaseModel):
@@ -42,13 +43,27 @@ class GenericAPIModelConfig(PydanticBaseModel):
"""Sampling temperature"""
top_p: float = 1.0
"""Sampling top-p"""
+ api_base: str | None = None
+ api_version: str | None = None
+ api_key: SecretStr | None = None
+ stop: list[str] = []
+
+ completion_kwargs: dict[str, Any] = {}
+ """Additional kwargs to pass to `litellm.completion`"""
+
+ convert_system_to_user: bool = False
+ """Whether to convert system messages to user messages. This is useful for
+ models that do not support system messages like o1.
+ """
+
+ retry: RetryConfig = RetryConfig()
# pydantic
model_config = ConfigDict(extra="forbid")
class ReplayModelConfig(GenericAPIModelConfig):
- replay_path: str | None = None
+ replay_path: Path
"""Path to replay file when using the replay model"""
name: Literal["replay"] = "replay"
@@ -66,854 +81,85 @@ class InstantEmptySubmitModelConfig(GenericAPIModelConfig):
model_config = ConfigDict(extra="forbid")
-class GenericLocalModelConfig(GenericAPIModelConfig):
- name: str
- api_base: str
- temperature: float = 1.0
- """Sampling temperature"""
- top_p: float = 1.0
- """Sampling top-p"""
+class HumanModelConfig(GenericAPIModelConfig):
+ name: Literal["human"] = "human"
+ """Do not change. Used for (de)serialization."""
+
+ model_config = ConfigDict(extra="forbid")
+
+
+class HumanThoughtModelConfig(HumanModelConfig):
+ name: Literal["human_thought"] = "human_thought"
+ """Do not change. Used for (de)serialization."""
model_config = ConfigDict(extra="forbid")
ModelConfig = Annotated[
- GenericAPIModelConfig | GenericLocalModelConfig | ReplayModelConfig | InstantEmptySubmitModelConfig,
+ ReplayModelConfig
+ | InstantEmptySubmitModelConfig
+ | HumanModelConfig
+ | HumanThoughtModelConfig
+ | GenericAPIModelConfig,
Field(union_mode="left_to_right"),
-] # noqa: F821
+]
-@dataclass
-class APIStats(Serializable):
+class APIStats(PydanticBaseModel):
total_cost: float = 0
+ """Cumulative cost for all instances so far"""
+
instance_cost: float = 0
tokens_sent: int = 0
tokens_received: int = 0
api_calls: int = 0
- def __add__(self, other):
- if not isinstance(other, APIStats):
- msg = "Can only add APIStats with APIStats"
- raise TypeError(msg)
-
+ def __add__(self, other: APIStats) -> APIStats:
return APIStats(
- **{field.name: getattr(self, field.name) + getattr(other, field.name) for field in fields(self)},
+ **{field: getattr(self, field) + getattr(other, field) for field in self.model_fields.keys()},
)
- def replace(self, other):
- if not isinstance(other, APIStats):
- msg = "Can only replace APIStats with APIStats"
- raise TypeError(msg)
-
- return APIStats(**{field.name: getattr(other, field.name) for field in fields(self)})
-
class ContextWindowExceededError(Exception):
- pass
+ """Raised when the context window of a LM is exceeded"""
class CostLimitExceededError(Exception):
- pass
-
-
-class AbstractModel:
- MODELS = {}
- SHORTCUTS = {}
-
- def __init__(self, args: ModelConfig, commands: list[Command]):
- self.args = args
- self.commands = commands
- self.model_metadata = {}
- self.stats = APIStats()
-
- # Map `model_name` to API-compatible name `api_model`
- self.api_model = self.SHORTCUTS[self.args.name] if self.args.name in self.SHORTCUTS else self.args.name
-
- # Map model name to metadata (cost, context info)
- MODELS = {
- **{dest: self.MODELS[src] for dest, src in self.SHORTCUTS.items()},
- **self.MODELS,
- }
- if args.name in MODELS:
- self.model_metadata = MODELS[args.name]
- elif args.name.startswith("ft:"):
- ft_model = args.name.split(":")[1]
- self.model_metadata = MODELS[ft_model]
- elif args.name.startswith("ollama:"):
- self.api_model = args.name.split("ollama:", 1)[1]
- self.model_metadata = self.MODELS[self.api_model]
- elif args.name.startswith("azure:"):
- azure_model = args.name.split("azure:", 1)[1]
- self.model_metadata = MODELS[azure_model]
- elif args.name.startswith("bedrock:"):
- self.api_model = args.name.split("bedrock:", 1)[1]
- self.model_metadata = MODELS[self.api_model]
- elif args.name.startswith("groq:"):
- self.api_model = args.name.split("groq:", 1)[1]
- self.model_metadata = MODELS[self.api_model]
- else:
- msg = f"Unregistered model ({args.name}). Add model name to MODELS metadata to {self.__class__}"
- raise ValueError(msg)
-
- @property
- def name(self) -> str:
- return self.args.name
-
- def reset_stats(self, other: APIStats | None = None):
- if other is None:
- self.stats = APIStats(total_cost=self.stats.total_cost)
- logger.info("Resetting model stats")
- else:
- # Make sure to copy the stats to avoid modifying the original
- self.stats = copy.deepcopy(other)
-
- def update_stats(self, input_tokens: int, output_tokens: int) -> float:
- """
- Calculates the cost of a response from the openai API.
-
- Args:
- input_tokens (int): The number of tokens in the prompt.
- output_tokens (int): The number of tokens in the response.
-
- Returns:
- float: The cost of the response.
- """
- # Calculate cost and update cost related fields
- cost = (
- self.model_metadata["cost_per_input_token"] * input_tokens
- + self.model_metadata["cost_per_output_token"] * output_tokens
- )
- self.stats.total_cost += cost
- self.stats.instance_cost += cost
- self.stats.tokens_sent += input_tokens
- self.stats.tokens_received += output_tokens
- self.stats.api_calls += 1
-
- # Log updated cost values to std. err
- logger.debug(
- f"input_tokens={input_tokens:,}, "
- f"output_tokens={output_tokens:,}, "
- f"instance_cost={self.stats.instance_cost:.2f}, "
- f"cost={cost:.2f}",
- )
- logger.debug(
- f"total_tokens_sent={self.stats.tokens_sent:,}, "
- f"total_tokens_received={self.stats.tokens_received:,}, "
- f"total_cost={self.stats.total_cost:.2f}, "
- f"total_api_calls={self.stats.api_calls:,}",
- )
-
- # Check whether total cost or instance cost limits have been exceeded
- if 0 < self.args.total_cost_limit <= self.stats.total_cost:
- logger.warning(f"Cost {self.stats.total_cost:.2f} exceeds limit {self.args.total_cost_limit:.2f}")
- msg = "Total cost limit exceeded"
- raise CostLimitExceededError(msg)
+ """Raised when we exceed a cost limit"""
- if 0 < self.args.per_instance_cost_limit <= self.stats.instance_cost:
- logger.warning(f"Cost {self.stats.instance_cost:.2f} exceeds limit {self.args.per_instance_cost_limit:.2f}")
- msg = "Instance cost limit exceeded"
- raise CostLimitExceededError(msg)
- return cost
-
- def query(self, history: list[dict[str, str]]) -> str:
- msg = "Use a subclass of BaseModel"
- raise NotImplementedError(msg)
-
- def history_to_messages(
- self,
- history: list[dict[str, str]],
- is_demonstration: bool = False,
- ) -> str | list[dict[str, str]]:
- msg = "Use a subclass of BaseModel"
- raise NotImplementedError(msg)
-
-
-class OpenAIModel(AbstractModel):
- MODELS = {
- "gpt-3.5-turbo-0125": {
- "max_context": 16_385,
- "cost_per_input_token": 5e-07,
- "cost_per_output_token": 1.5e-06,
- },
- "gpt-3.5-turbo-1106": {
- "max_context": 16_385,
- "cost_per_input_token": 1.5e-06,
- "cost_per_output_token": 2e-06,
- },
- "gpt-3.5-turbo-16k-0613": {
- "max_context": 16_385,
- "cost_per_input_token": 1.5e-06,
- "cost_per_output_token": 2e-06,
- },
- "gpt-4-32k-0613": {
- "max_context": 32_768,
- "cost_per_input_token": 6e-05,
- "cost_per_output_token": 0.00012,
- },
- "gpt-4-0613": {
- "max_context": 8_192,
- "cost_per_input_token": 3e-05,
- "cost_per_output_token": 6e-05,
- },
- "gpt-4-1106-preview": {
- "max_context": 128_000,
- "cost_per_input_token": 1e-05,
- "cost_per_output_token": 3e-05,
- },
- "gpt-4-0125-preview": {
- "max_context": 128_000,
- "cost_per_input_token": 1e-05,
- "cost_per_output_token": 3e-05,
- },
- "gpt-4-turbo-2024-04-09": {
- "max_context": 128_000,
- "cost_per_input_token": 1e-05,
- "cost_per_output_token": 3e-05,
- },
- "gpt-4o-2024-05-13": {
- "max_context": 128_000,
- "cost_per_input_token": 5e-06,
- "cost_per_output_token": 15e-06,
- },
- "gpt-4o-mini-2024-07-18": {
- "max_context": 128_000,
- "cost_per_input_token": 1.5e-07,
- "cost_per_output_token": 6e-07,
- },
- "o1-preview-2024-09-12": {
- "max_context": 128_000,
- "cost_per_input_token": 15e-06,
- "cost_per_output_token": 60e-06,
- },
- "o1-mini-2024-09-12": {
- "max_context": 128_000,
- "cost_per_input_token": 3e-6,
- "cost_per_output_token": 12e-6,
- },
- }
-
- SHORTCUTS = {
- "gpt3": "gpt-3.5-turbo-1106",
- "gpt3-legacy": "gpt-3.5-turbo-16k-0613",
- "gpt4": "gpt-4-1106-preview",
- "gpt4-legacy": "gpt-4-0613",
- "gpt4-0125": "gpt-4-0125-preview",
- "gpt3-0125": "gpt-3.5-turbo-0125",
- "gpt4-turbo": "gpt-4-turbo-2024-04-09",
- "gpt4o": "gpt-4o-2024-05-13",
- "gpt-4o-mini": "gpt-4o-mini-2024-07-18",
- "gpt4omini": "gpt-4o-mini-2024-07-18",
- "o1": "o1-preview-2024-09-12",
- "o1-mini": "o1-mini-2024-09-12",
- }
-
- def __init__(self, args: GenericAPIModelConfig, commands: list[Command]):
- super().__init__(args, commands)
-
- logging.getLogger("openai").setLevel(logging.WARNING)
- logging.getLogger("httpx").setLevel(logging.WARNING)
-
- self._setup_client()
-
- def _setup_client(self):
- if self.args.name.startswith("azure"):
- logger.warning(
- "The --model CLI argument is ignored when using the Azure GPT endpoint. "
- "The model is determined by the AZURE_OPENAI_DEPLOYMENT key/"
- "environment variable (this might change in the future).",
- )
- self.api_model = keys_config["AZURE_OPENAI_DEPLOYMENT"]
- self.client = AzureOpenAI(
- api_key=keys_config["AZURE_OPENAI_API_KEY"],
- azure_endpoint=keys_config["AZURE_OPENAI_ENDPOINT"],
- api_version=keys_config.get("AZURE_OPENAI_API_VERSION", "2024-02-01"),
- )
- else:
- api_base_url: str | None = keys_config.get("OPENAI_API_BASE_URL", None)
- self.client = OpenAI(api_key=keys_config["OPENAI_API_KEY"], base_url=api_base_url)
- def history_to_messages(
- self,
- history: list[dict[str, str]],
- is_demonstration: bool = False,
- ) -> str | list[dict[str, str]]:
- """
- Create `messages` by filtering out all keys except for role/content per `history` turn
- """
- # 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
- return [{k: v for k, v in entry.items() if k in ["role", "content"]} for entry in history]
-
- @retry(
- wait=wait_random_exponential(min=1, max=15),
- reraise=True,
- stop=stop_after_attempt(_MAX_RETRIES),
- retry=retry_if_not_exception_type((CostLimitExceededError, RuntimeError)),
- )
- def query(self, history: list[dict[str, str]]) -> str:
- """
- Query the OpenAI API with the given `history` and return the response.
- """
- try:
- # Perform OpenAI API call
- response = self.client.chat.completions.create(
- messages=self.history_to_messages(history),
- model=self.api_model,
- temperature=self.args.temperature,
- top_p=self.args.top_p,
- )
- except BadRequestError:
- msg = f"Context window ({self.model_metadata['max_context']} tokens) exceeded"
- raise ContextWindowExceededError(msg)
- # Calculate + update costs, return response
- input_tokens = response.usage.prompt_tokens
- output_tokens = response.usage.completion_tokens
- self.update_stats(input_tokens, output_tokens)
- return response.choices[0].message.content
-
-
-class DeepSeekModel(OpenAIModel):
- MODELS = {
- "deepseek-coder": {
- "max_context": 32_000,
- "cost_per_input_token": 1.4e-07,
- "cost_per_output_token": 2.8e-07,
- },
- }
- SHORTCUTS = {}
-
- def _setup_client(self) -> None:
- api_base_url: str = keys_config["DEEPSEEK_API_BASE_URL"]
- self.client = OpenAI(api_key=keys_config["DEEPSEEK_API_KEY"], base_url=api_base_url)
-
-
-class GroqModel(OpenAIModel):
- MODELS = {
- "llama3-8b-8192": {
- "max_context": 8192,
- "cost_per_input_token": 5e-08,
- "cost_per_output_token": 8e-08,
- },
- "llama3-70b-8192": {
- "max_context": 8192,
- "cost_per_input_token": 5.9e-07,
- "cost_per_output_token": 7.9e-07,
- },
- "llama-guard-3-8b": {
- "max_context": 8192,
- "cost_per_input_token": 0,
- "cost_per_output_token": 0,
- },
- "llama-3.1-8b-instant": {
- "max_context": 131_072,
- "cost_per_input_token": 0,
- "cost_per_output_token": 0,
- },
- "llama-3.1-70b-versatile": {
- "max_context": 131_072,
- "cost_per_input_token": 0,
- "cost_per_output_token": 0,
- },
- "gemma2-9b-it": {
- "max_context": 8192,
- "cost_per_input_token": 2e-07,
- "cost_per_output_token": 2e-07,
- },
- "gemma-7b-it": {
- "max_context": 8192,
- "cost_per_input_token": 5e-08,
- "cost_per_output_token": 5e-08,
- },
- "mixtral-8x7b-32768": {
- "max_context": 32_768,
- "cost_per_input_token": 2.4e-07,
- "cost_per_output_token": 2.8e-07,
- },
- }
-
- SHORTCUTS = {
- "groq/llama8": "llama3-8b-8192",
- "groq/llama70": "llama3-70b-8192",
- "groq/llamaguard8": "llama-guard-3-8b",
- "groq/llamainstant8": "llama-3.1-8b-instant",
- "groq/llamaversatile70": "llama-3.1-70b-versatile",
- "groq/gemma9it": "gemma2-9b-it",
- "groq/gemma7it": "gemma-7b-it",
- "groq/mixtral8x7": "mixtral-8x7b-32768",
- }
-
- def _setup_client(self) -> None:
- self.client = Groq(
- api_key=keys_config["GROQ_API_KEY"],
- )
-
-
-class AnthropicModel(AbstractModel):
- MODELS = {
- "claude-instant": {
- "max_context": 100_000,
- "cost_per_input_token": 1.63e-06,
- "cost_per_output_token": 5.51e-06,
- },
- "claude-2.0": {
- "max_context": 100_000,
- "cost_per_input_token": 1.102e-05,
- "cost_per_output_token": 3.268e-05,
- },
- "claude-2.1": {
- "max_context": 100_000,
- "cost_per_input_token": 1.102e-05,
- "cost_per_output_token": 3.268e-05,
- },
- "claude-3-opus-20240229": {
- "max_context": 200_000,
- "max_tokens": 4096, # Max tokens to generate for Claude 3 models
- "cost_per_input_token": 1.5e-05,
- "cost_per_output_token": 7.5e-05,
- },
- "claude-3-sonnet-20240229": {
- "max_context": 200_000,
- "max_tokens": 4096,
- "cost_per_input_token": 3e-06,
- "cost_per_output_token": 1.5e-05,
- },
- "claude-3-5-sonnet-20240620": {
- "max_context": 200_000,
- "max_tokens": 4096,
- "cost_per_input_token": 3e-06,
- "cost_per_output_token": 1.5e-05,
- },
- "claude-3-haiku-20240307": {
- "max_context": 200_000,
- "max_tokens": 4096,
- "cost_per_input_token": 2.5e-07,
- "cost_per_output_token": 1.25e-06,
- },
- }
-
- SHORTCUTS = {
- "claude-2": "claude-2.1",
- "claude-opus": "claude-3-opus-20240229",
- "claude-sonnet": "claude-3-sonnet-20240229",
- "claude-haiku": "claude-3-haiku-20240307",
- "claude-sonnet-3.5": "claude-3-5-sonnet-20240620",
- }
-
- def __init__(self, args: ModelConfig, commands: list[Command]):
- super().__init__(args, commands)
-
- # Set Anthropic key
- self.api = Anthropic(api_key=keys_config["ANTHROPIC_API_KEY"])
-
- def history_to_messages(
- self,
- history: list[dict[str, str]],
- is_demonstration: bool = False,
- ) -> str | 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
- """
- return anthropic_history_to_messages(self, history, is_demonstration)
-
- @retry(
- wait=wait_random_exponential(min=1, max=15),
- reraise=True,
- stop=stop_after_attempt(_MAX_RETRIES),
- retry=retry_if_not_exception_type((CostLimitExceededError, RuntimeError)),
- )
- def query(self, history: list[dict[str, str]]) -> str:
- """
- Query the Anthropic API with the given `history` and return the response.
- """
- return anthropic_query(self, history)
-
-
-class BedrockModel(AbstractModel):
- MODELS = {
- "anthropic.claude-instant-v1": {
- "max_context": 100_000,
- "max_tokens_to_sample": 4096,
- "cost_per_input_token": 8e-07,
- "cost_per_output_token": 2.4e-06,
- },
- "anthropic.claude-v2": {
- "max_context": 100_000,
- "max_tokens_to_sample": 4096,
- "cost_per_input_token": 8e-06,
- "cost_per_output_token": 2.4e-05,
- },
- "anthropic.claude-v2:1": {
- "max_context": 100_000,
- "max_tokens": 4096,
- "cost_per_input_token": 8e-06,
- "cost_per_output_token": 2.4e-05,
- },
- "anthropic.claude-3-opus-20240229-v1:0": {
- "max_context": 200_000,
- "max_tokens": 4096,
- "cost_per_input_token": 1.5e-05,
- "cost_per_output_token": 7.5e-05,
- },
- "anthropic.claude-3-sonnet-20240229-v1:0": {
- "max_context": 200_000,
- "max_tokens": 4096,
- "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: ModelConfig, commands: list[Command]):
- super().__init__(args, commands)
-
- # Extract provider from model ID
- # https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html
- self.model_provider = self.api_model.split(".")[0]
- if self.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()
- elif self.model_provider in ["ai21", "amazon", "cohere", "meta", "mistral"]:
- msg = f"{self.api_model} is not supported!"
- raise NotImplementedError(msg)
- else:
- msg = f"Provider {self.model_provider} is not supported by Amazon Bedrock!"
- raise ValueError(msg)
-
- def history_to_messages(
- self,
- history: list[dict[str, str]],
- is_demonstration: bool = False,
- ) -> str | list[dict[str, str]]:
- """
- Create `prompt` from the history of messages
- """
- if self.model_provider == "anthropic":
- return anthropic_history_to_messages(self, history, is_demonstration)
- else:
- msg = f"{self.api_model} is not supported!"
- raise NotImplementedError(msg)
-
- @retry(
- wait=wait_random_exponential(min=1, max=15),
- reraise=True,
- stop=stop_after_attempt(_MAX_RETRIES),
- 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.
- """
- if self.model_provider == "anthropic":
- return anthropic_query(self, history)
- else:
- msg = f"{self.api_model} is not supported!"
- raise NotImplementedError(msg)
-
-
-def anthropic_history_to_messages(
- model: AnthropicModel | BedrockModel,
- history: list[dict[str, str]],
- is_demonstration: bool = False,
-) -> str | 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
- """
- # Preserve behavior for older models
- if model.api_model in ["claude-instant", "claude-2.0"] or (
- isinstance(model, BedrockModel) and model.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
-
-
-def anthropic_query(model: AnthropicModel | BedrockModel, history: list[dict[str, str]]) -> str:
- """
- Query the Anthropic API with the given `history` and return the response.
- """
- # Preserve behavior for older models
- if model.api_model in ["claude-instant", "claude-2.0", "claude-2.1"] or (
- isinstance(model, BedrockModel) and model.api_model in ["anthropic.claude-instant-v1", "anthropic.claude-v2"]
- ):
- # Perform Anthropic API call
- prompt = anthropic_history_to_messages(model, history)
- if isinstance(model, BedrockModel):
- # Use a dummy Anthropic client since count_tokens
- # is not available in AnthropicBedrock
- # https://github.com/anthropics/anthropic-sdk-python/issues/353
- input_tokens = Anthropic().count_tokens(prompt)
- else:
- input_tokens = model.api.count_tokens(prompt)
- completion = model.api.completions.create(
- model=model.api_model,
- prompt=prompt,
- max_tokens_to_sample=model.model_metadata["max_context"] - input_tokens
- if isinstance(model, Anthropic)
- else model.model_metadata["max_tokens_to_sample"],
- temperature=model.args.temperature,
- top_p=model.args.top_p,
- )
- # Calculate + update costs, return response
- response = completion.completion
- if isinstance(model, BedrockModel):
- output_tokens = Anthropic().count_tokens(response)
- else:
- output_tokens = model.api.count_tokens(response)
- model.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 = anthropic_history_to_messages(model, history)
-
- # Perform Anthropic API call
- response = model.api.messages.create(
- messages=messages,
- max_tokens=model.model_metadata["max_tokens"],
- model=model.api_model,
- temperature=model.args.temperature,
- top_p=model.args.top_p,
- system=system_message,
- )
-
- # Calculate + update costs, return response
- model.update_stats(response.usage.input_tokens, response.usage.output_tokens)
- return "\n".join([x.text for x in response.content])
-
-
-class OllamaModel(AbstractModel):
- MODELS = defaultdict(
- lambda: {
- "max_context": 128_000,
- "cost_per_input_token": 0,
- "cost_per_output_token": 0,
- },
- )
+class InstanceCostLimitExceededError(CostLimitExceededError):
+ """Raised when we exceed the cost limit set for one task instance"""
- def __init__(self, args: ModelConfig, commands: list[Command]):
- super().__init__(args, commands)
- from ollama import Client
- self.client = Client(host=args.host_url)
+class TotalCostLimitExceededError(CostLimitExceededError):
+ """Raised when we exceed the total cost limit"""
- def history_to_messages(
- self,
- history: list[dict[str, str]],
- is_demonstration: bool = False,
- ) -> str | list[dict[str, str]]:
- """
- Create `messages` by filtering out all keys except for role/content per `history` turn
- """
- # 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
- return [{k: v for k, v in entry.items() if k in ["role", "content"]} for entry in history]
-
- @retry(
- wait=wait_random_exponential(min=1, max=15),
- reraise=True,
- stop=stop_after_attempt(_MAX_RETRIES),
- retry=retry_if_not_exception_type((CostLimitExceededError, RuntimeError)),
- )
- def query(self, history: list[dict[str, str]]) -> str:
- """
- Query the Ollama API with the given `history` and return the response.
- """
- response = self.client.chat(
- model=self.api_model,
- messages=self.history_to_messages(history),
- options={
- "temperature": self.args.temperature,
- "top_p": self.args.top_p,
- },
- )
- # Calculate + update costs, return response
- if "prompt_eval_count" in response:
- input_tokens = response["prompt_eval_count"]
- else:
- logger.warning(
- "Prompt eval count not found in response. Using 0. "
- "This might be because the prompt has been cached. "
- "See https://github.com/princeton-nlp/SWE-agent/issues/44 "
- "and https://github.com/ollama/ollama/issues/3427.",
- )
- input_tokens = 0
- output_tokens = response["eval_count"]
- self.update_stats(input_tokens, output_tokens)
- return response["message"]["content"]
-
-
-class TogetherModel(AbstractModel):
- # Check https://docs.together.ai/docs/inference-models for model names, context
- # Check https://www.together.ai/pricing for pricing
- MODELS = {
- "meta-llama/Llama-2-13b-chat-hf": {
- "max_context": 4096,
- "cost_per_input_token": 2.25e-07,
- "cost_per_output_token": 2.25e-07,
- },
- "meta-llama/Llama-2-70b-chat-hf": {
- "max_context": 4096,
- "cost_per_input_token": 9e-07,
- "cost_per_output_token": 9e-07,
- },
- "mistralai/Mistral-7B-Instruct-v0.2": {
- "max_context": 32768,
- "cost_per_input_token": 2e-07,
- "cost_per_output_token": 2e-07,
- },
- "togethercomputer/RedPajama-INCITE-7B-Chat": {
- "max_context": 2048,
- "cost_per_input_token": 2e-07,
- "cost_per_output_token": 2e-07,
- },
- "mistralai/Mixtral-8x7B-Instruct-v0.1": {
- "max_context": 32768,
- "cost_per_input_token": 6e-07,
- "cost_per_output_token": 6e-07,
- },
- }
-
- SHORTCUTS = {
- "llama13b": "meta-llama/Llama-2-13b-chat-hf",
- "llama70b": "meta-llama/Llama-2-70b-chat-hf",
- "mistral7b": "mistralai/Mistral-7B-Instruct-v0.2",
- "mixtral8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1",
- "redpajama7b": "togethercomputer/RedPajama-INCITE-7B-Chat",
- }
- def __init__(self, args: ModelConfig, commands: list[Command]):
- super().__init__(args, commands)
- assert together.version >= "1.1.0", "Please upgrade to Together SDK v1.1.0 or later."
-
- # Set Together key
- together.api_key = keys_config["TOGETHER_API_KEY"]
-
- def history_to_messages(self, history: list[dict[str, str]], is_demonstration: bool = False) -> str:
- """
- Create `prompt` by filtering out all keys except for role/content per `history` turn
- """
- # Remove system messages if it is a demonstration
- if is_demonstration:
- history = [entry for entry in history if entry["role"] != "system"]
- # Map history to TogetherAI format
- mapping = {"user": "human", "assistant": "bot", "system": "bot"}
- prompt = [f'<{mapping[d["role"]]}>: {d["content"]}' for d in history]
- prompt = "\n".join(prompt)
- return f"{prompt}\n<bot>:"
-
- @retry(
- wait=wait_random_exponential(min=1, max=15),
- reraise=True,
- stop=stop_after_attempt(_MAX_RETRIES),
- retry=retry_if_not_exception_type((CostLimitExceededError, RuntimeError)),
- )
- def query(self, history: list[dict[str, str]]) -> str:
- """
- Query the Together API with the given `history` and return the response.
- """
- # Perform Together API call
- prompt = self.history_to_messages(history)
- # Anthropic's count_tokens is convenient because it caches and utilizes huggingface/tokenizers, so we will use.
- max_tokens_to_sample = self.model_metadata["max_context"] - Anthropic().count_tokens(prompt)
- completion = together.Complete.create(
- model=self.api_model,
- prompt=prompt,
- max_tokens=max_tokens_to_sample,
- stop=["<human>"],
- temperature=self.args.temperature,
- top_p=self.args.top_p,
- )
- # Calculate + update costs, return response
- response = completion["choices"][0]["text"].split("<human>")[0]
- input_tokens = completion["usage"]["prompt_tokens"]
- output_tokens = completion["usage"]["completion_tokens"]
- self.update_stats(input_tokens, output_tokens)
- return response
+class AbstractModel(ABC):
+ def __init__(self, config: PydanticBaseModel, commands: list[Command]):
+ self.config: PydanticBaseModel
+ self.stats: APIStats
+ def reset_stats(self):
+ self.stats = APIStats()
-def _history_to_messages(history: list[dict[str, str]], is_demonstration: bool = False) -> str | list[dict[str, str]]:
- """
- Create `messages` by filtering out all keys except for role/content per `history` turn
- """
- 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
- return [{k: v for k, v in entry.items() if k in ["role", "content"]} for entry in history]
+ @abstractmethod
+ def query(self, history: History, action_prompt: str = "> ") -> str: ...
class HumanModel(AbstractModel):
- MODELS = {"human": {}}
-
- def __init__(self, args: ModelConfig, commands: list[Command]):
- super().__init__(args, commands)
+ def __init__(self, config: HumanModelConfig, commands: list[Command]):
+ self.config = config
+ self.stats = APIStats()
# Determine which commands require multi-line input
self.multi_line_command_endings = {
command.name: command.end_name for command in commands if command.end_name is not None
}
- def history_to_messages(
- self,
- history: list[dict[str, str]],
- is_demonstration: bool = False,
- ) -> str | list[dict[str, str]]:
- """
- Create `messages` by filtering out all keys except for role/content per `history` turn
- """
- return _history_to_messages(history, is_demonstration)
-
- def query(self, history: list[dict[str, str]], action_prompt: str = "> ") -> str:
- """
- Logic for handling user input to pass to SWEEnv
- """
+ def query(self, history: History, action_prompt: str = "> ") -> str:
+ """Logic for handling user input to pass to SWEEnv"""
action = input(action_prompt)
command_name = action.split()[0] if action.strip() else ""
@@ -940,12 +186,8 @@ class HumanModel(AbstractModel):
class HumanThoughtModel(HumanModel):
- MODELS = {"human_thought": {}}
-
- def query(self, history: list[dict[str, str]]) -> str:
- """
- Logic for handling user input (both thought + action) to pass to SWEEnv
- """
+ def query(self, history: History) -> str:
+ """Logic for handling user input (both thought + action) to pass to SWEEnv"""
thought_all = ""
thought = input("Thought (end w/ END_THOUGHT): ")
while True:
@@ -962,33 +204,31 @@ class HumanThoughtModel(HumanModel):
class ReplayModel(AbstractModel):
- MODELS = {"replay": {}}
-
- def __init__(self, args: ModelConfig, commands: list[Command]):
- super().__init__(args, commands)
+ def __init__(self, config: ReplayModelConfig, commands: list[Command]):
+ self.config = config
+ self.stats = APIStats()
- if self.args.replay_path is None or not Path(self.args.replay_path).exists():
- msg = "--replay_path must point to a file that exists to run a replay policy"
- raise ValueError(msg)
+ if not self.config.replay_path.exists():
+ msg = f"Replay file {self.config.replay_path} not found"
+ raise FileNotFoundError(msg)
- self.replays = [
- list(json.loads(x).values())[0] for x in Path(self.args.replay_path).read_text().splitlines(keepends=True)
+ self._replays = [
+ list(json.loads(x).values())[0] for x in Path(self.config.replay_path).read_text().splitlines(keepends=True)
]
- self.replay_idx = 0
- self.action_idx = 0
+ self._replay_idx = 0
+ self._action_idx = 0
def _next_replay(self) -> None:
"""Called after last action"""
- self.replay_idx += 1
- self.action_idx = 0
+ self._replay_idx += 1
+ self._action_idx = 0
- def query(self, history: list[dict[str, str]]) -> str:
- """
- Logic for tracking which replay action to pass to SWEEnv
- """
- actions = self.replays[self.replay_idx]
+ def query(self, history: History) -> str:
+ """Logic for tracking which replay action to pass to SWEEnv"""
+ self.stats.api_calls += 1
+ actions = self._replays[self._replay_idx]
try:
- action = actions[self.action_idx]
+ action = actions[self._action_idx]
except IndexError:
msg = (
"This seems to be an incomplete trajectory. "
@@ -998,7 +238,7 @@ class ReplayModel(AbstractModel):
logger.warning(msg)
action = "```\nsubmit\n```"
- self.action_idx += 1
+ self._action_idx += 1
# Assuming `submit` is always last action of replay trajectory
if action == "submit":
@@ -1006,15 +246,11 @@ class ReplayModel(AbstractModel):
return action
- def history_to_messages(self, history: list[dict[str, str]], *args, **kwargs) -> str | list[dict[str, str]]:
- return _history_to_messages(history, is_demonstration=True)
-
-class PredeterminedTestModel:
+class PredeterminedTestModel(AbstractModel):
def __init__(self, outputs: list[str]):
self._outputs = outputs
self._idx = -1
- self.name = "predetermined_test"
self.stats = APIStats()
def query(self, *args, **kwargs) -> str:
@@ -1028,27 +264,9 @@ class PredeterminedTestModel:
raise ContextWindowExceededError()
return output
- def history_to_messages(self, *args, **kwargs) -> list[dict[str, str]]:
- return []
-
- def reset_stats(self, *args, **kwargs) -> None:
- pass
-
- def update_stats(self, *args, **kwargs) -> None:
- pass
-
class InstantEmptySubmitTestModel(AbstractModel):
- MODELS = {
- "instant_empty_submit": {
- "max_context": 100_000,
- "max_tokens_to_sample": 4096,
- "cost_per_input_token": 0,
- "cost_per_output_token": 0,
- }
- }
-
- def __init__(self, args: ModelConfig, commands: list[Command]):
+ def __init__(self, args: InstantEmptySubmitModelConfig, commands: list[Command]):
"""This model immediately submits. Useful for testing purposes"""
super().__init__(args, commands)
self._action_idx = 0
@@ -1061,48 +279,118 @@ class InstantEmptySubmitTestModel(AbstractModel):
elif self._action_idx == 1:
self._action_idx = 0
action = "DISCUSSION\nThe task should be resolved, so let's submit the patch.\n\n```\nsubmit\n```\n"
- self.update_stats(0, 0)
+ self.stats.api_calls += 1
return action
- def history_to_messages(self, history: list[dict[str, str]], *args, **kwargs) -> list[dict[str, str]]:
- return history
+
+class LiteLLMModel(AbstractModel):
+ def __init__(self, args: ModelConfig, commands: list[Command]):
+ self.args = args
+ self.commands = commands
+ self.stats = APIStats()
+
+ def _update_stats(self, *, input_tokens: int, output_tokens: int, cost: float) -> None:
+ self.stats.total_cost += cost
+ self.stats.instance_cost += cost
+ self.stats.tokens_sent += input_tokens
+ self.stats.tokens_received += output_tokens
+ self.stats.api_calls += 1
+
+ # Log updated cost values to std. err
+ logger.debug(
+ f"input_tokens={input_tokens:,}, "
+ f"output_tokens={output_tokens:,}, "
+ f"instance_cost={self.stats.instance_cost:.2f}, "
+ f"cost={cost:.2f}",
+ )
+ logger.debug(
+ f"total_tokens_sent={self.stats.tokens_sent:,}, "
+ f"total_tokens_received={self.stats.tokens_received:,}, "
+ f"total_cost={self.stats.total_cost:.2f}, "
+ f"total_api_calls={self.stats.api_calls:,}",
+ )
+
+ # Check whether total cost or instance cost limits have been exceeded
+ if 0 < self.args.total_cost_limit <= self.stats.total_cost:
+ logger.warning(f"Cost {self.stats.total_cost:.2f} exceeds limit {self.args.total_cost_limit:.2f}")
+ msg = "Total cost limit exceeded"
+ raise TotalCostLimitExceededError(msg)
+
+ if 0 < self.args.per_instance_cost_limit <= self.stats.instance_cost:
+ logger.warning(f"Cost {self.stats.instance_cost:.2f} exceeds limit {self.args.per_instance_cost_limit:.2f}")
+ msg = "Instance cost limit exceeded"
+ raise InstanceCostLimitExceededError(msg)
+
+ def _query(self, history: History) -> str:
+ input_tokens: int = litellm.utils.token_counter(messages=history, model=self.args.name)
+ max_input_tokens: int = litellm.utils.get_max_tokens(model=self.args.name) # type: ignore
+ if input_tokens > max_input_tokens:
+ msg = f"Input tokens {input_tokens} exceed max tokens {max_input_tokens}"
+ raise ContextWindowExceededError(msg)
+ messages = self._history_to_messages(history)
+ extra_args = {}
+ if self.args.api_base:
+ # Not assigned a default value in litellm, so only pass this if it's set
+ extra_args["api_base"] = self.args.api_base
+ response: litellm.types.utils.ModelResponse = litellm.completion( # type: ignore
+ model=self.args.name,
+ messages=messages,
+ 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,
+ **self.args.completion_kwargs,
+ **extra_args,
+ )
+ choices: litellm.types.utils.Choices = response.choices # type: ignore
+ output = choices[0].message.content
+ cost = litellm.cost_calculator.completion_cost(response)
+ output_tokens = litellm.utils.token_counter(text=output, model=self.args.name)
+ self._update_stats(input_tokens=input_tokens, output_tokens=output_tokens, cost=cost)
+
+ return output
+
+ def query(self, history: History) -> str:
+ for attempt in Retrying(
+ stop=stop_after_attempt(self.args.retry.retries),
+ wait=wait_random_exponential(min=self.args.retry.min_wait, max=self.args.retry.max_wait),
+ reraise=True,
+ retry=retry_if_not_exception_type(
+ (CostLimitExceededError, RuntimeError, litellm.exceptions.UnsupportedParamsError)
+ ),
+ ):
+ with attempt:
+ result = self._query(history)
+ return result
+
+ def _history_to_messages(
+ self,
+ history: History,
+ ) -> list[dict[str, str]]:
+ def get_role(history_item: HistoryItem) -> str:
+ if history_item["role"] == "system":
+ return "user" if self.args.convert_system_to_user else "system"
+ return history_item["role"]
+
+ return [{"role": history_item["role"], "content": history_item["content"]} for history_item in history]
def get_model(args: ModelConfig, commands: list[Command] | None = None) -> AbstractModel:
- """
- Returns correct model object given arguments and commands
- """
+ """Returns correct model object given arguments and commands"""
if commands is None:
commands = []
- if args.name == "instant_empty_submit":
- return InstantEmptySubmitTestModel(args, commands)
+
if args.name == "human":
+ assert isinstance(args, HumanModelConfig)
return HumanModel(args, commands)
if args.name == "human_thought":
+ assert isinstance(args, HumanThoughtModelConfig)
return HumanThoughtModel(args, commands)
if args.name == "replay":
+ assert isinstance(args, ReplayModelConfig)
return ReplayModel(args, commands)
- elif (
- args.name.startswith("gpt")
- or args.name.startswith("ft:gpt")
- or args.name.startswith("azure:gpt")
- or args.name in OpenAIModel.SHORTCUTS
- ):
- return OpenAIModel(args, commands)
- elif args.name.startswith("claude"):
- return AnthropicModel(args, commands)
- elif args.name.startswith("bedrock"):
- return BedrockModel(args, commands)
- elif args.name.startswith("ollama"):
- return OllamaModel(args, commands)
- elif args.name.startswith("deepseek"):
- return DeepSeekModel(args, commands)
- elif args.name in TogetherModel.SHORTCUTS:
- return TogetherModel(args, commands)
- elif args.name in GroqModel.SHORTCUTS:
- return GroqModel(args, commands)
elif args.name == "instant_empty_submit":
+ assert isinstance(args, InstantEmptySubmitModelConfig)
return InstantEmptySubmitTestModel(args, commands)
- else:
- msg = f"Invalid model name: {args.name}"
- raise ValueError(msg)
+ assert isinstance(args, GenericAPIModelConfig)
+ return LiteLLMModel(args, commands)
diff --git a/sweagent/api/server.py b/sweagent/api/server.py
index 93464617..005a5716 100644
--- a/sweagent/api/server.py
+++ b/sweagent/api/server.py
@@ -1,18 +1,5 @@
from __future__ import annotations
-from omegaconf import OmegaConf
-
-from sweagent.environment.config.deployment import DeploymentConfig
-from sweagent.run.run_single import RunSingleActionConfig
-
-try:
- import flask # noqa
-except ImportError as e:
- msg = (
- "Flask not found. You probably haven't installed the dependencies for SWE-agent. "
- "Please go to the root of the repository and run `pip install -e .`"
- )
- raise RuntimeError(msg) from e
import atexit
import copy
import json
@@ -25,6 +12,7 @@ from pathlib import Path
from typing import Any
from uuid import uuid4
+import flask # noqa
import yaml
from flask import Flask, make_response, render_template, request, session
from flask_cors import CORS
@@ -35,11 +23,13 @@ from sweagent.agent.agents import AgentConfig
from sweagent.agent.models import ModelConfig
from sweagent.api.hooks import AgentUpdateHook, EnvUpdateHook, MainUpdateHook, WebUpdate
from sweagent.api.utils import AttrDict, ThreadWithExc
+from sweagent.environment.config.deployment import DockerDeploymentConfig
from sweagent.environment.swe_env import EnvironmentConfig
+from sweagent.run.run_single import RunSingleActionConfig
# baaaaaaad
sys.path.append(str(PACKAGE_DIR.parent))
-from run import Main, ScriptArguments
+from sweagent.run.run_single import RunSingle, RunSingleConfig
app = Flask(__name__, template_folder=Path(__file__).parent)
CORS(app)
@@ -63,7 +53,7 @@ def ensure_session_id_set():
class MainThread(ThreadWithExc):
- def __init__(self, settings: ScriptArguments, wu: WebUpdate):
+ def __init__(self, settings: RunSingleConfig, wu: WebUpdate):
super().__init__()
self._wu = wu
self._settings = settings
@@ -73,11 +63,11 @@ class MainThread(ThreadWithExc):
with redirect_stdout(self._wu.log_stream):
with redirect_stderr(self._wu.log_stream):
try:
- main = Main(self._settings)
+ main = RunSingle.from_config(self._settings)
main.add_hook(MainUpdateHook(self._wu))
main.agent.add_hook(AgentUpdateHook(self._wu))
main.env.add_hook(EnvUpdateHook(self._wu))
- main.main()
+ main.run()
except Exception as e:
short_msg = str(e)
max_len = 350
@@ -155,10 +145,10 @@ def run():
if test_run:
model_name = "instant_empty_submit"
agent_config = OmegaConf.load(CONFIG_DIR / "default_from_url.yaml")
- defaults = ScriptArguments(
+ defaults = RunSingleConfig(
suffix="",
environment=EnvironmentConfig(
- deployment=DeploymentConfig(),
+ deployment=DockerDeploymentConfig(),
data_path=run.environment.data_path,
base_commit=run.environment.base_commit,
split="dev",
@@ -169,7 +159,7 @@ def run():
),
agent=AgentConfig(),
skip_existing=False,
- actions=RunSingleActionConfig(open_pr=False, skip_if_commits_reference_issue=True),
+ actions=RunSingleActionConfig(),
raise_exceptions=True,
)
defaults.agent.model = ModelConfig(
diff --git a/sweagent/environment/config/problem_statement.py b/sweagent/environment/config/problem_statement.py
index 959de445..f1868f61 100644
--- a/sweagent/environment/config/problem_statement.py
+++ b/sweagent/environment/config/problem_statement.py
@@ -1,11 +1,11 @@
import hashlib
+import os
import uuid
from pathlib import Path
from typing import Any, Literal, Protocol
from pydantic import BaseModel, ConfigDict, Field
-from sweagent.utils.config import keys_config
from sweagent.utils.github import _get_problem_statement_from_github_issue, _parse_gh_issue_url
from sweagent.utils.log import get_logger
@@ -87,9 +87,7 @@ class GithubIssue(BaseModel):
def get_problem_statement(self) -> str:
owner, repo, issue_number = _parse_gh_issue_url(self.url)
- return _get_problem_statement_from_github_issue(
- owner, repo, issue_number, token=keys_config.get("GITHUB_TOKEN")
- )
+ return _get_problem_statement_from_github_issue(owner, repo, issue_number, token=os.getenv("GITHUB_TOKEN"))
ProblemStatementConfig = TextProblemStatement | GithubIssue | EmptyProblemStatement | FileProblemStatement
diff --git a/sweagent/environment/config/repo.py b/sweagent/environment/config/repo.py
index 9bda9c74..3a8e443a 100644
--- a/sweagent/environment/config/repo.py
+++ b/sweagent/environment/config/repo.py
@@ -10,7 +10,6 @@ from pydantic import BaseModel
from swerex.deployment.abstract import AbstractDeployment
from swerex.runtime.abstract import Command, UploadRequest
-from sweagent.utils.config import keys_config
from sweagent.utils.log import get_logger
logger = get_logger("Config", emoji="⚙️")
@@ -61,6 +60,9 @@ class GithubRepoConfig(BaseModel):
base_commit: str = "HEAD"
+ clone_timeout: float = 500
+ """Timeout for git clone operation."""
+
type: Literal["github"] = "github"
"""Discriminator for (de)serialization/CLI. Do not change."""
@@ -82,7 +84,7 @@ class GithubRepoConfig(BaseModel):
def copy(self, deployment: AbstractDeployment):
base_commit = self.base_commit
- github_token = keys_config.get("GITHUB_TOKEN", "")
+ github_token = os.getenv("GITHUB_TOKEN", "")
url = self._get_url_with_token(github_token)
asyncio.run(
deployment.runtime.execute(
@@ -98,7 +100,7 @@ class GithubRepoConfig(BaseModel):
"cd ..",
)
),
- timeout=float(keys_config.get("SWE_AGENT_ENV_LONG_TIMEOUT", 500)),
+ timeout=self.clone_timeout,
shell=True,
check=True,
)
diff --git a/sweagent/run/run_batch.py b/sweagent/run/run_batch.py
index 8c52f6e8..998c9847 100644
--- a/sweagent/run/run_batch.py
+++ b/sweagent/run/run_batch.py
@@ -8,7 +8,6 @@ import traceback
from pathlib import Path
from typing import Self
-from pydantic import Field
from pydantic_settings import BaseSettings
from sweagent.agent.agents import Agent, AgentConfig
@@ -17,15 +16,18 @@ from sweagent.run.batch_instances import BatchInstance, BatchInstanceSourceConfi
from sweagent.run.common import BasicCLI, save_predictions
from sweagent.run.hooks.abstract import CombinedRunHooks, RunHook
from sweagent.run.hooks.apply_patch import SaveApplyPatchHook
+from sweagent.utils.config import load_environment_variables
from sweagent.utils.log import get_logger
class RunBatchConfig(BaseSettings, cli_implicit_flags=True):
instances: BatchInstanceSourceConfig
- agent: AgentConfig = Field(default_factory=AgentConfig)
+ agent: AgentConfig
traj_dir: Path = Path(".")
raise_exceptions: bool = False
redo_existing: bool = False
+ env_var_path: Path | None = None
+ """Path to a .env file to load environment variables from."""
class _BreakLoop(Exception):
@@ -62,6 +64,7 @@ class RunBatch:
@classmethod
def from_config(cls, config: RunBatchConfig) -> Self:
+ load_environment_variables(config.env_var_path)
logger = get_logger("RunBatch", emoji="🏃")
logger.debug("Loading instances from %s", f"{config.instances!r}")
instances = config.instances.get_instance_configs()
diff --git a/sweagent/run/run_replay.py b/sweagent/run/run_replay.py
index 4a548370..1801dcc5 100644
--- a/sweagent/run/run_replay.py
+++ b/sweagent/run/run_replay.py
@@ -20,6 +20,7 @@ from sweagent.environment.config.repo import RepoConfig
from sweagent.environment.swe_env import SWEEnv
from sweagent.run.common import BasicCLI
from sweagent.run.run_single import RunSingle
+from sweagent.utils.config import load_environment_variables
from sweagent.utils.log import get_logger
@@ -29,6 +30,8 @@ class RunReplayConfig(BaseSettings):
deployment: DeploymentConfig = Field(default_factory=DockerDeploymentConfig)
repo: RepoConfig | None = None
output_dir: str = "."
+ env_var_path: Path | None = None
+ """Path to a .env file to load environment variables from."""
class RunReplay:
@@ -59,6 +62,7 @@ class RunReplay:
@classmethod
def from_config(cls, config: RunReplayConfig, **kwargs) -> Self:
+ load_environment_variables(config.env_var_path)
return cls(
traj_path=config.traj_path,
config_path=config.config_path,
diff --git a/sweagent/run/run_single.py b/sweagent/run/run_single.py
index de90d7be..98ac68a5 100644
--- a/sweagent/run/run_single.py
+++ b/sweagent/run/run_single.py
@@ -19,6 +19,7 @@ from sweagent.run.common import BasicCLI, save_predictions
from sweagent.run.hooks.abstract import CombinedRunHooks, RunHook
from sweagent.run.hooks.apply_patch import SaveApplyPatchHook
from sweagent.run.hooks.open_pr import OpenPRConfig, OpenPRHook
+from sweagent.utils.config import load_environment_variables
from sweagent.utils.log import get_logger
@@ -34,7 +35,7 @@ class RunSingleActionConfig(BaseModel, cli_implicit_flags=True):
class RunSingleConfig(BaseSettings, cli_implicit_flags=True):
env: EnvironmentConfig = Field(default_factory=EnvironmentConfig)
- agent: AgentConfig = Field(default_factory=AgentConfig)
+ agent: AgentConfig
problem_statement: ProblemStatementConfig = Field(default_factory=EmptyProblemStatement)
traj_dir: Path = Path(".")
actions: RunSingleActionConfig = Field(default_factory=RunSingleActionConfig)
@@ -42,6 +43,9 @@ class RunSingleConfig(BaseSettings, cli_implicit_flags=True):
print_config: bool = False
"""Print config at the beginning of the run."""
+ env_var_path: Path | None = None
+ """Path to a .env file to load environment variables from."""
+
# pydantic config
model_config = ConfigDict(extra="forbid") # type: ignore
@@ -83,6 +87,7 @@ class RunSingle:
if config.print_config:
config_str = yaml.dump(config.model_dump())
logger.info(f"Config:\n {config_str}")
+ load_environment_variables(config.env_var_path)
self = cls(
env=SWEEnv.from_config(config.env),
agent=Agent.from_config(config.agent),
diff --git a/sweagent/types.py b/sweagent/types.py
index 90d833c5..994a5336 100644
--- a/sweagent/types.py
+++ b/sweagent/types.py
@@ -26,10 +26,10 @@ class TrajectoryStep(TypedDict):
class _HistoryItem(TypedDict):
role: str
+ content: str
class HistoryItem(_HistoryItem, total=False):
- content: str | None
agent: str
is_demo: bool
thought: str
diff --git a/sweagent/utils/config.py b/sweagent/utils/config.py
index 8f67fee9..8fa5c0c4 100644
--- a/sweagent/utils/config.py
+++ b/sweagent/utils/config.py
@@ -2,13 +2,13 @@ from __future__ import annotations
import os
from pathlib import Path
-from typing import Any
-import config as config_file
+from dotenv import load_dotenv
+
from sweagent import REPO_ROOT
from sweagent.utils.log import get_logger
-logger = get_logger("config")
+logger = get_logger("config", emoji="⚙️")
def _convert_path_to_abspath(path: Path | str) -> Path:
@@ -17,7 +17,7 @@ def _convert_path_to_abspath(path: Path | str) -> Path:
REPO_ROOT as base.
"""
path = Path(path)
- root = Path(keys_config.get("SWE_AGENT_CONFIG_ROOT", REPO_ROOT))
+ root = Path(os.getenv("SWE_AGENT_CONFIG_ROOT", REPO_ROOT))
assert root.is_dir()
if not path.is_absolute():
path = root / path
@@ -29,57 +29,24 @@ def _convert_paths_to_abspath(paths: list[Path] | list[str]) -> list[Path]:
return [_convert_path_to_abspath(p) for p in paths]
-class Config:
- def __init__(self, *, keys_cfg_path: Path | None = None):
- """This wrapper class is used to load keys from environment variables or keys.cfg file.
- Whenever both are presents, the environment variable is used.
- """
- if keys_cfg_path is None:
- # Defer import to avoid circular import
- from sweagent import PACKAGE_DIR
-
- keys_cfg_path = PACKAGE_DIR.parent / "keys.cfg"
- self._keys_cfg = None
- if keys_cfg_path.exists():
- try:
- self._keys_cfg = config_file.Config(str(keys_cfg_path))
- except Exception as e:
- msg = f"Error loading keys.cfg from {keys_cfg_path}. Please check the file."
- raise RuntimeError(msg) from e
+def load_environment_variables(path: Path | None = None):
+ """Load environment variables from a .env file.
+ If path is not provided, we first look for a .env file in the current working
+ directory and then in the repository root.
+ """
+ if path is None:
+ cwd_path = Path.cwd() / ".env"
+ repo_path = REPO_ROOT / ".env"
+ if cwd_path.exists():
+ path = cwd_path
+ elif repo_path.exists():
+ path = REPO_ROOT / ".env"
else:
- logger.error(f"keys.cfg not found in {PACKAGE_DIR}")
-
- def get(self, key: str, default=None, choices: list[Any] | None = None) -> Any:
- """Get a key from environment variables or keys.cfg.
-
- Args:
- key: The key to retrieve.
- default: The default value to return if the key is not found.
- choices: If provided, the value must be one of the choices.
- """
-
- def check_choices(value):
- if choices is not None and value not in choices:
- msg = f"Value {value} for key {key} not in {choices}"
- raise ValueError(msg)
- return value
-
- if key in os.environ:
- return check_choices(os.environ[key])
- if self._keys_cfg is not None and key in self._keys_cfg:
- return check_choices(self._keys_cfg[key])
- return check_choices(default)
-
- def __getitem__(self, key: str) -> Any:
- if key in os.environ:
- return os.environ[key]
- if self._keys_cfg is not None and key in self._keys_cfg:
- return self._keys_cfg[key]
- msg = f"Key {key} not found in environment variables or keys.cfg (if existing)"
- raise KeyError(msg)
-
- def __contains__(self, key: str) -> bool:
- return key in os.environ or (self._keys_cfg is not None and key in self._keys_cfg)
-
-
-keys_config = Config()
+ logger.debug("No .env file found")
+ return
+ if not path.is_file():
+ msg = f"No .env file found at {path}"
+ raise FileNotFoundError(msg)
+ anything_loaded = load_dotenv(dotenv_path=path)
+ if anything_loaded:
+ logger.info(f"Loaded environment variables from {path}")
diff --git a/tests/conftest.py b/tests/conftest.py
index 7e0789ad..9beb85fd 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -98,8 +98,11 @@ def dummy_env_args() -> EnvironmentConfig:
@pytest.fixture
-def dummy_env(dummy_env_args) -> SWEEnv:
- return SWEEnv.from_config(dummy_env_args)
+def dummy_env(dummy_env_args) -> Generator[SWEEnv, None, None]:
+ env = SWEEnv.from_config(dummy_env_args)
+ env.start()
+ yield env
+ env.close()
@contextmanager
diff --git a/tests/test_env_utils.py b/tests/test_env_utils.py
index fa28f1f9..53cff369 100644
--- a/tests/test_env_utils.py
+++ b/tests/test_env_utils.py
@@ -6,7 +6,6 @@ import subprocess
import pytest
from sweagent.run.hooks.open_pr import _remove_triple_backticks, format_trajectory_markdown
-from sweagent.utils.config import keys_config
from sweagent.utils.github import (
InvalidGithubURL,
_get_associated_commit_urls,
@@ -16,8 +15,6 @@ from sweagent.utils.github import (
_parse_gh_repo_url,
)
-_TOKEN = {"token": keys_config["GITHUB_TOKEN"]}
-
def test_format_trajectory_markdown(test_trajectory):
formatted = format_trajectory_markdown(test_trajectory["trajectory"])
diff --git a/tests/test_models.py b/tests/test_models.py
index 6db6b864..9f2cb595 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -1,62 +1,17 @@
from __future__ import annotations
-from unittest.mock import MagicMock, Mock, patch
+from pydantic import SecretStr
-import pytest
+from sweagent.agent.models import GenericAPIModelConfig, get_model
+from sweagent.types import History
-from sweagent.agent.models import GenericAPIModelConfig, GroqModel, OpenAIModel, TogetherModel
-
-def openai_mock_client():
- model = Mock()
- response = Mock()
- choice = Mock()
- choice.message.content = "test"
- response.choices = [choice]
- response.usage.prompt_tokens = 10
- response.usage.completion_tokens = 10
- model.chat.completions.create = MagicMock(return_value=response)
-
- return model
-
-
-def mock_together_response():
- return {
- "choices": [{"text": "<human>Hello</human>"}],
- "usage": {"prompt_tokens": 10, "completion_tokens": 10},
- }
-
-
-TEST_HISTORY = [{"role": "system", "content": "Hello, how are you?"}]
-
-
-def test_openai_model(openai_mock_client):
- for model_name in list(OpenAIModel.MODELS) + list(OpenAIModel.SHORTCUTS):
- model_args = GenericAPIModelConfig(name=model_name)
- with patch("sweagent.agent.models.keys_config"), patch("sweagent.agent.models.OpenAI"):
- model = OpenAIModel(model_args, [])
- model.client = openai_mock_client
- model.query(TEST_HISTORY)
-
-
-# Test groq models
-def test_groq_model(openai_mock_client):
- for model_name in list(GroqModel.MODELS) + list(GroqModel.SHORTCUTS):
- model_args = GenericAPIModelConfig(name=model_name)
- with patch("sweagent.agent.models.keys_config"), patch("sweagent.agent.models.Groq"):
- model = GroqModel(model_args, [])
- model.client = openai_mock_client
- model.query(TEST_HISTORY)
-
-
[email protected]("model_name", list(TogetherModel.MODELS) + list(TogetherModel.SHORTCUTS))
-def test_together_model(mock_together_response, model_name):
- with patch("sweagent.agent.models.keys_config"), patch("sweagent.agent.models.together") as mock_together:
- mock_together.version = "1.1.0"
- mock_together.Complete.create.return_value = mock_together_response
-
- model_args = GenericAPIModelConfig(name=model_name)
- model = TogetherModel(model_args, [])
- model.query(TEST_HISTORY)
+def test_litellm_mock():
+ model = get_model(
+ GenericAPIModelConfig(
+ name="anthropic/o1-preview",
+ completion_kwargs={"mock_response": "Hello, world!"},
+ api_key=SecretStr("dummy_key"),
+ )
+ )
+ assert model.query(History([{"role": "user", "content": "Hello, world!"}])) == "Hello, world!"
diff --git a/tests/test_utils.py b/tests/test_utils.py
index e019c61e..9b25e7ed 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,58 +1,9 @@
from __future__ import annotations
from pathlib import Path
-from unittest import mock
-
-import pytest
from sweagent import REPO_ROOT
-from sweagent.utils.config import Config, _convert_path_to_abspath, _convert_paths_to_abspath
-
-
-def test_config_retrieval_fails():
- config = Config()
- with pytest.raises(KeyError):
- config["DOESNTEXIST"]
-
-
-def test_config_retrieval_get():
- config = Config()
- assert config.get("asdfasdf", "default") == "default"
-
-
-def test_retrieve_from_file(tmp_path):
- tmp_keys_cfg = tmp_path / "keys.cfg"
- tmp_keys_cfg.write_text("MY_KEY: 'VALUE'\n")
- config = Config(keys_cfg_path=tmp_keys_cfg)
- assert config["MY_KEY"] == "VALUE"
-
-
-def test_retrieve_from_env(tmp_path):
- with mock.patch.dict("os.environ", {"MY_KEY": "VALUE"}):
- tmp_keys_cfg = tmp_path / "keys.cfg"
- tmp_keys_cfg.write_text("MY_KEY: 'other VALUE'\n")
- config = Config(keys_cfg_path=tmp_keys_cfg)
- assert config["MY_KEY"] == "VALUE"
-
-
-def test_retrieve_choices():
- """Check that a valueerror is raised if the value is not in the choices."""
- match = "Value.*not in.*"
- config = Config()
- with pytest.raises(ValueError, match=match):
- config.get("DOESNTEXIST", default="x", choices=["a", "b", "c"])
- with pytest.raises(ValueError, match=match):
- with mock.patch.dict("os.environ", {"MY_KEY": "VALUE"}):
- config.get("DOESNTEXIST", choices=["a", "b", "c"])
-
-
-def test_retrieve_choices_config_file(tmp_path):
- match = "Value.*not in.*"
- tmp_keys_cfg = tmp_path / "keys.cfg"
- tmp_keys_cfg.write_text("MY_KEY: 'VALUE'\n")
- config = Config(keys_cfg_path=tmp_keys_cfg)
- with pytest.raises(ValueError, match=match):
- config.get("MY_KEY", choices=["a", "b", "c"])
+from sweagent.utils.config import _convert_path_to_abspath, _convert_paths_to_abspath
def test_convert_path_to_abspath():