config.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. import argparse
  2. import os
  3. import pathlib
  4. import platform
  5. import uuid
  6. from dataclasses import dataclass, field, fields, is_dataclass
  7. from enum import Enum
  8. from types import UnionType
  9. from typing import Any, ClassVar, MutableMapping, get_args, get_origin
  10. import toml
  11. from dotenv import load_dotenv
  12. from opendevin.core import logger
  13. from opendevin.core.utils import Singleton
  14. load_dotenv()
  15. LLM_SENSITIVE_FIELDS = ['api_key', 'aws_access_key_id', 'aws_secret_access_key']
  16. _DEFAULT_AGENT = 'CodeActAgent'
  17. _MAX_ITERATIONS = 100
  18. @dataclass
  19. class LLMConfig:
  20. """Configuration for the LLM model.
  21. Attributes:
  22. model: The model to use.
  23. api_key: The API key to use.
  24. base_url: The base URL for the API. This is necessary for local LLMs. It is also used for Azure embeddings.
  25. api_version: The version of the API.
  26. embedding_model: The embedding model to use.
  27. embedding_base_url: The base URL for the embedding API.
  28. embedding_deployment_name: The name of the deployment for the embedding API. This is used for Azure OpenAI.
  29. aws_access_key_id: The AWS access key ID.
  30. aws_secret_access_key: The AWS secret access key.
  31. aws_region_name: The AWS region name.
  32. num_retries: The number of retries to attempt.
  33. retry_multiplier: The multiplier for the exponential backoff.
  34. retry_min_wait: The minimum time to wait between retries, in seconds. This is exponential backoff minimum. For models with very low limits, this can be set to 15-20.
  35. retry_max_wait: The maximum time to wait between retries, in seconds. This is exponential backoff maximum.
  36. timeout: The timeout for the API.
  37. max_message_chars: The approximate max number of characters in the content of an event included in the prompt to the LLM. Larger observations are truncated.
  38. temperature: The temperature for the API.
  39. top_p: The top p for the API.
  40. custom_llm_provider: The custom LLM provider to use. This is undocumented in opendevin, and normally not used. It is documented on the litellm side.
  41. max_input_tokens: The maximum number of input tokens. Note that this is currently unused, and the value at runtime is actually the total tokens in OpenAI (e.g. 128,000 tokens for GPT-4).
  42. max_output_tokens: The maximum number of output tokens. This is sent to the LLM.
  43. input_cost_per_token: The cost per input token. This will available in logs for the user to check.
  44. output_cost_per_token: The cost per output token. This will available in logs for the user to check.
  45. ollama_base_url: The base URL for the OLLAMA API.
  46. drop_params: Drop any unmapped (unsupported) params without causing an exception.
  47. """
  48. model: str = 'gpt-4o'
  49. api_key: str | None = None
  50. base_url: str | None = None
  51. api_version: str | None = None
  52. embedding_model: str = 'local'
  53. embedding_base_url: str | None = None
  54. embedding_deployment_name: str | None = None
  55. aws_access_key_id: str | None = None
  56. aws_secret_access_key: str | None = None
  57. aws_region_name: str | None = None
  58. num_retries: int = 10
  59. retry_multiplier: float = 2
  60. retry_min_wait: int = 3
  61. retry_max_wait: int = 300
  62. timeout: int | None = None
  63. max_message_chars: int = 10_000 # maximum number of characters in an observation's content when sent to the llm
  64. temperature: float = 0
  65. top_p: float = 0.5
  66. custom_llm_provider: str | None = None
  67. max_input_tokens: int | None = None
  68. max_output_tokens: int | None = None
  69. input_cost_per_token: float | None = None
  70. output_cost_per_token: float | None = None
  71. ollama_base_url: str | None = None
  72. drop_params: bool | None = None
  73. def defaults_to_dict(self) -> dict:
  74. """Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""
  75. result = {}
  76. for f in fields(self):
  77. result[f.name] = get_field_info(f)
  78. return result
  79. def __str__(self):
  80. attr_str = []
  81. for f in fields(self):
  82. attr_name = f.name
  83. attr_value = getattr(self, f.name)
  84. if attr_name in LLM_SENSITIVE_FIELDS:
  85. attr_value = '******' if attr_value else None
  86. attr_str.append(f'{attr_name}={repr(attr_value)}')
  87. return f"LLMConfig({', '.join(attr_str)})"
  88. def __repr__(self):
  89. return self.__str__()
  90. def to_safe_dict(self):
  91. """Return a dict with the sensitive fields replaced with ******."""
  92. ret = self.__dict__.copy()
  93. for k, v in ret.items():
  94. if k in LLM_SENSITIVE_FIELDS:
  95. ret[k] = '******' if v else None
  96. return ret
  97. def set_missing_attributes(self):
  98. """Set any missing attributes to their default values."""
  99. for field_name, field_obj in self.__dataclass_fields__.items():
  100. if not hasattr(self, field_name):
  101. setattr(self, field_name, field_obj.default)
  102. @dataclass
  103. class AgentConfig:
  104. """Configuration for the agent.
  105. Attributes:
  106. memory_enabled: Whether long-term memory (embeddings) is enabled.
  107. memory_max_threads: The maximum number of threads indexing at the same time for embeddings.
  108. llm_config: The name of the llm config to use. If specified, this will override global llm config.
  109. """
  110. memory_enabled: bool = False
  111. memory_max_threads: int = 2
  112. llm_config: str | None = None
  113. def defaults_to_dict(self) -> dict:
  114. """Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""
  115. result = {}
  116. for f in fields(self):
  117. result[f.name] = get_field_info(f)
  118. return result
  119. @dataclass
  120. class SandboxConfig(metaclass=Singleton):
  121. """Configuration for the sandbox.
  122. Attributes:
  123. api_hostname: The hostname for the EventStream Runtime API.
  124. container_image: The container image to use for the sandbox.
  125. user_id: The user ID for the sandbox.
  126. timeout: The timeout for the sandbox.
  127. enable_auto_lint: Whether to enable auto-lint.
  128. use_host_network: Whether to use the host network.
  129. initialize_plugins: Whether to initialize plugins.
  130. od_runtime_extra_deps: The extra dependencies to install in the runtime image (typically used for evaluation).
  131. This will be rendered into the end of the Dockerfile that builds the runtime image.
  132. It can contain any valid shell commands (e.g., pip install numpy).
  133. The path to the interpreter is available as $OD_INTERPRETER_PATH,
  134. which can be used to install dependencies for the OD-specific Python interpreter.
  135. od_runtime_startup_env_vars: The environment variables to set at the launch of the runtime.
  136. This is a dictionary of key-value pairs.
  137. This is useful for setting environment variables that are needed by the runtime.
  138. For example, for specifying the base url of website for browsergym evaluation.
  139. browsergym_eval_env: The BrowserGym environment to use for evaluation.
  140. Default is None for general purpose browsing. Check evaluation/miniwob and evaluation/webarena for examples.
  141. """
  142. api_hostname: str = 'localhost'
  143. container_image: str = 'nikolaik/python-nodejs:python3.11-nodejs22' # default to nikolaik/python-nodejs:python3.11-nodejs22 for eventstream runtime
  144. user_id: int = os.getuid() if hasattr(os, 'getuid') else 1000
  145. timeout: int = 120
  146. enable_auto_lint: bool = (
  147. False # once enabled, OpenDevin would lint files after editing
  148. )
  149. use_host_network: bool = False
  150. initialize_plugins: bool = True
  151. od_runtime_extra_deps: str | None = None
  152. od_runtime_startup_env_vars: dict[str, str] = field(default_factory=dict)
  153. browsergym_eval_env: str | None = None
  154. def defaults_to_dict(self) -> dict:
  155. """Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""
  156. dict = {}
  157. for f in fields(self):
  158. dict[f.name] = get_field_info(f)
  159. return dict
  160. def __str__(self):
  161. attr_str = []
  162. for f in fields(self):
  163. attr_name = f.name
  164. attr_value = getattr(self, f.name)
  165. attr_str.append(f'{attr_name}={repr(attr_value)}')
  166. return f"SandboxConfig({', '.join(attr_str)})"
  167. def __repr__(self):
  168. return self.__str__()
  169. class UndefinedString(str, Enum):
  170. UNDEFINED = 'UNDEFINED'
  171. @dataclass
  172. class AppConfig(metaclass=Singleton):
  173. """Configuration for the app.
  174. Attributes:
  175. llms: A dictionary of name -> LLM configuration. Default config is under 'llm' key.
  176. agents: A dictionary of name -> Agent configuration. Default config is under 'agent' key.
  177. default_agent: The name of the default agent to use.
  178. sandbox: The sandbox configuration.
  179. runtime: The runtime environment.
  180. file_store: The file store to use.
  181. file_store_path: The path to the file store.
  182. workspace_base: The base path for the workspace. Defaults to ./workspace as an absolute path.
  183. workspace_mount_path: The path to mount the workspace. This is set to the workspace base by default.
  184. workspace_mount_path_in_sandbox: The path to mount the workspace in the sandbox. Defaults to /workspace.
  185. workspace_mount_rewrite: The path to rewrite the workspace mount path to.
  186. cache_dir: The path to the cache directory. Defaults to /tmp/cache.
  187. run_as_devin: Whether to run as devin.
  188. max_iterations: The maximum number of iterations.
  189. max_budget_per_task: The maximum budget allowed per task, beyond which the agent will stop.
  190. e2b_api_key: The E2B API key.
  191. disable_color: Whether to disable color. For terminals that don't support color.
  192. debug: Whether to enable debugging.
  193. enable_cli_session: Whether to enable saving and restoring the session when run from CLI.
  194. file_uploads_max_file_size_mb: Maximum file size for uploads in megabytes. 0 means no limit.
  195. file_uploads_restrict_file_types: Whether to restrict file types for file uploads. Defaults to False.
  196. file_uploads_allowed_extensions: List of allowed file extensions for uploads. ['.*'] means all extensions are allowed.
  197. """
  198. llms: dict[str, LLMConfig] = field(default_factory=dict)
  199. agents: dict = field(default_factory=dict)
  200. default_agent: str = _DEFAULT_AGENT
  201. sandbox: SandboxConfig = field(default_factory=SandboxConfig)
  202. runtime: str = 'eventstream'
  203. file_store: str = 'memory'
  204. file_store_path: str = '/tmp/file_store'
  205. # TODO: clean up workspace path after the removal of ServerRuntime
  206. workspace_base: str = os.path.join(os.getcwd(), 'workspace')
  207. workspace_mount_path: str | None = (
  208. UndefinedString.UNDEFINED # this path should always be set when config is fully loaded
  209. ) # when set to None, do not mount the workspace
  210. workspace_mount_path_in_sandbox: str = '/workspace'
  211. workspace_mount_rewrite: str | None = None
  212. cache_dir: str = '/tmp/cache'
  213. run_as_devin: bool = True
  214. confirmation_mode: bool = False
  215. max_iterations: int = _MAX_ITERATIONS
  216. max_budget_per_task: float | None = None
  217. e2b_api_key: str = ''
  218. disable_color: bool = False
  219. jwt_secret: str = uuid.uuid4().hex
  220. debug: bool = False
  221. enable_cli_session: bool = False
  222. file_uploads_max_file_size_mb: int = 0
  223. file_uploads_restrict_file_types: bool = False
  224. file_uploads_allowed_extensions: list[str] = field(default_factory=lambda: ['.*'])
  225. defaults_dict: ClassVar[dict] = {}
  226. def get_llm_config(self, name='llm') -> LLMConfig:
  227. """Llm is the name for default config (for backward compatibility prior to 0.8)"""
  228. if name in self.llms:
  229. return self.llms[name]
  230. if name is not None and name != 'llm':
  231. logger.opendevin_logger.warning(
  232. f'llm config group {name} not found, using default config'
  233. )
  234. if 'llm' not in self.llms:
  235. self.llms['llm'] = LLMConfig()
  236. return self.llms['llm']
  237. def set_llm_config(self, value: LLMConfig, name='llm'):
  238. self.llms[name] = value
  239. def get_agent_config(self, name='agent') -> AgentConfig:
  240. """Agent is the name for default config (for backward compability prior to 0.8)"""
  241. if name in self.agents:
  242. return self.agents[name]
  243. if 'agent' not in self.agents:
  244. self.agents['agent'] = AgentConfig()
  245. return self.agents['agent']
  246. def set_agent_config(self, value: AgentConfig, name='agent'):
  247. self.agents[name] = value
  248. def get_agent_to_llm_config_map(self) -> dict[str, LLMConfig]:
  249. """Get a map of agent names to llm configs."""
  250. return {name: self.get_llm_config_from_agent(name) for name in self.agents}
  251. def get_llm_config_from_agent(self, name='agent') -> LLMConfig:
  252. agent_config: AgentConfig = self.get_agent_config(name)
  253. llm_config_name = agent_config.llm_config
  254. return self.get_llm_config(llm_config_name)
  255. def __post_init__(self):
  256. """Post-initialization hook, called when the instance is created with only default values."""
  257. AppConfig.defaults_dict = self.defaults_to_dict()
  258. def defaults_to_dict(self) -> dict:
  259. """Serialize fields to a dict for the frontend, including type hints, defaults, and whether it's optional."""
  260. result = {}
  261. for f in fields(self):
  262. field_value = getattr(self, f.name)
  263. # dataclasses compute their defaults themselves
  264. if is_dataclass(type(field_value)):
  265. result[f.name] = field_value.defaults_to_dict()
  266. else:
  267. result[f.name] = get_field_info(f)
  268. return result
  269. def __str__(self):
  270. attr_str = []
  271. for f in fields(self):
  272. attr_name = f.name
  273. attr_value = getattr(self, f.name)
  274. if attr_name in [
  275. 'e2b_api_key',
  276. 'github_token',
  277. 'jwt_secret',
  278. ]:
  279. attr_value = '******' if attr_value else None
  280. attr_str.append(f'{attr_name}={repr(attr_value)}')
  281. return f"AppConfig({', '.join(attr_str)}"
  282. def __repr__(self):
  283. return self.__str__()
  284. def get_field_info(f):
  285. """Extract information about a dataclass field: type, optional, and default.
  286. Args:
  287. f: The field to extract information from.
  288. Returns: A dict with the field's type, whether it's optional, and its default value.
  289. """
  290. field_type = f.type
  291. optional = False
  292. # for types like str | None, find the non-None type and set optional to True
  293. # this is useful for the frontend to know if a field is optional
  294. # and to show the correct type in the UI
  295. # Note: this only works for UnionTypes with None as one of the types
  296. if get_origin(field_type) is UnionType:
  297. types = get_args(field_type)
  298. non_none_arg = next((t for t in types if t is not type(None)), None)
  299. if non_none_arg is not None:
  300. field_type = non_none_arg
  301. optional = True
  302. # type name in a pretty format
  303. type_name = (
  304. field_type.__name__ if hasattr(field_type, '__name__') else str(field_type)
  305. )
  306. # default is always present
  307. default = f.default
  308. # return a schema with the useful info for frontend
  309. return {'type': type_name.lower(), 'optional': optional, 'default': default}
  310. def load_from_env(cfg: AppConfig, env_or_toml_dict: dict | MutableMapping[str, str]):
  311. """Reads the env-style vars and sets config attributes based on env vars or a config.toml dict.
  312. Compatibility with vars like LLM_BASE_URL, AGENT_MEMORY_ENABLED, SANDBOX_TIMEOUT and others.
  313. Args:
  314. cfg: The AppConfig object to set attributes on.
  315. env_or_toml_dict: The environment variables or a config.toml dict.
  316. """
  317. def get_optional_type(union_type: UnionType) -> Any:
  318. """Returns the non-None type from a Union."""
  319. types = get_args(union_type)
  320. return next((t for t in types if t is not type(None)), None)
  321. # helper function to set attributes based on env vars
  322. def set_attr_from_env(sub_config: Any, prefix=''):
  323. """Set attributes of a config dataclass based on environment variables."""
  324. for field_name, field_type in sub_config.__annotations__.items():
  325. # compute the expected env var name from the prefix and field name
  326. # e.g. LLM_BASE_URL
  327. env_var_name = (prefix + field_name).upper()
  328. if is_dataclass(field_type):
  329. # nested dataclass
  330. nested_sub_config = getattr(sub_config, field_name)
  331. set_attr_from_env(nested_sub_config, prefix=field_name + '_')
  332. elif env_var_name in env_or_toml_dict:
  333. # convert the env var to the correct type and set it
  334. value = env_or_toml_dict[env_var_name]
  335. # skip empty config values (fall back to default)
  336. if not value:
  337. continue
  338. try:
  339. # if it's an optional type, get the non-None type
  340. if get_origin(field_type) is UnionType:
  341. field_type = get_optional_type(field_type)
  342. # Attempt to cast the env var to type hinted in the dataclass
  343. if field_type is bool:
  344. cast_value = str(value).lower() in ['true', '1']
  345. else:
  346. cast_value = field_type(value)
  347. setattr(sub_config, field_name, cast_value)
  348. except (ValueError, TypeError):
  349. logger.opendevin_logger.error(
  350. f'Error setting env var {env_var_name}={value}: check that the value is of the right type'
  351. )
  352. # Start processing from the root of the config object
  353. set_attr_from_env(cfg)
  354. # load default LLM config from env
  355. default_llm_config = cfg.get_llm_config()
  356. set_attr_from_env(default_llm_config, 'LLM_')
  357. # load default agent config from env
  358. default_agent_config = cfg.get_agent_config()
  359. set_attr_from_env(default_agent_config, 'AGENT_')
  360. def load_from_toml(cfg: AppConfig, toml_file: str = 'config.toml'):
  361. """Load the config from the toml file. Supports both styles of config vars.
  362. Args:
  363. cfg: The AppConfig object to update attributes of.
  364. toml_file: The path to the toml file. Defaults to 'config.toml'.
  365. """
  366. # try to read the config.toml file into the config object
  367. try:
  368. with open(toml_file, 'r', encoding='utf-8') as toml_contents:
  369. toml_config = toml.load(toml_contents)
  370. except FileNotFoundError:
  371. return
  372. except toml.TomlDecodeError as e:
  373. logger.opendevin_logger.warning(
  374. f'Cannot parse config from toml, toml values have not been applied.\nError: {e}',
  375. exc_info=False,
  376. )
  377. return
  378. # if there was an exception or core is not in the toml, try to use the old-style toml
  379. if 'core' not in toml_config:
  380. # re-use the env loader to set the config from env-style vars
  381. load_from_env(cfg, toml_config)
  382. return
  383. core_config = toml_config['core']
  384. # load llm configs and agent configs
  385. for key, value in toml_config.items():
  386. if isinstance(value, dict):
  387. try:
  388. if key is not None and key.lower() == 'agent':
  389. logger.opendevin_logger.info(
  390. 'Attempt to load default agent config from config toml'
  391. )
  392. non_dict_fields = {
  393. k: v for k, v in value.items() if not isinstance(v, dict)
  394. }
  395. agent_config = AgentConfig(**non_dict_fields)
  396. cfg.set_agent_config(agent_config, 'agent')
  397. for nested_key, nested_value in value.items():
  398. if isinstance(nested_value, dict):
  399. logger.opendevin_logger.info(
  400. f'Attempt to load group {nested_key} from config toml as agent config'
  401. )
  402. agent_config = AgentConfig(**nested_value)
  403. cfg.set_agent_config(agent_config, nested_key)
  404. if key is not None and key.lower() == 'llm':
  405. logger.opendevin_logger.info(
  406. 'Attempt to load default LLM config from config toml'
  407. )
  408. non_dict_fields = {
  409. k: v for k, v in value.items() if not isinstance(v, dict)
  410. }
  411. llm_config = LLMConfig(**non_dict_fields)
  412. cfg.set_llm_config(llm_config, 'llm')
  413. for nested_key, nested_value in value.items():
  414. if isinstance(nested_value, dict):
  415. logger.opendevin_logger.info(
  416. f'Attempt to load group {nested_key} from config toml as llm config'
  417. )
  418. llm_config = LLMConfig(**nested_value)
  419. cfg.set_llm_config(llm_config, nested_key)
  420. except (TypeError, KeyError) as e:
  421. logger.opendevin_logger.warning(
  422. f'Cannot parse config from toml, toml values have not been applied.\n Error: {e}',
  423. exc_info=False,
  424. )
  425. try:
  426. # set sandbox config from the toml file
  427. sandbox_config = cfg.sandbox
  428. # migrate old sandbox configs from [core] section to sandbox config
  429. keys_to_migrate = [key for key in core_config if key.startswith('sandbox_')]
  430. for key in keys_to_migrate:
  431. new_key = key.replace('sandbox_', '')
  432. if new_key in sandbox_config.__annotations__:
  433. # read the key in sandbox and remove it from core
  434. setattr(sandbox_config, new_key, core_config.pop(key))
  435. else:
  436. logger.opendevin_logger.warning(f'Unknown sandbox config: {key}')
  437. # the new style values override the old style values
  438. if 'sandbox' in toml_config:
  439. sandbox_config = SandboxConfig(**toml_config['sandbox'])
  440. # update the config object with the new values
  441. AppConfig(sandbox=sandbox_config, **core_config)
  442. except (TypeError, KeyError) as e:
  443. logger.opendevin_logger.warning(
  444. f'Cannot parse config from toml, toml values have not been applied.\nError: {e}',
  445. exc_info=False,
  446. )
  447. def finalize_config(cfg: AppConfig):
  448. """More tweaks to the config after it's been loaded."""
  449. # Set workspace_mount_path if not set by the user
  450. if cfg.workspace_mount_path is UndefinedString.UNDEFINED:
  451. cfg.workspace_mount_path = os.path.abspath(cfg.workspace_base)
  452. cfg.workspace_base = os.path.abspath(cfg.workspace_base)
  453. if cfg.workspace_mount_rewrite: # and not config.workspace_mount_path:
  454. # TODO why do we need to check if workspace_mount_path is None?
  455. base = cfg.workspace_base or os.getcwd()
  456. parts = cfg.workspace_mount_rewrite.split(':')
  457. cfg.workspace_mount_path = base.replace(parts[0], parts[1])
  458. for llm in cfg.llms.values():
  459. if llm.embedding_base_url is None:
  460. llm.embedding_base_url = llm.base_url
  461. if cfg.sandbox.use_host_network and platform.system() == 'Darwin':
  462. logger.opendevin_logger.warning(
  463. 'Please upgrade to Docker Desktop 4.29.0 or later to use host network mode on macOS. '
  464. 'See https://github.com/docker/roadmap/issues/238#issuecomment-2044688144 for more information.'
  465. )
  466. # make sure cache dir exists
  467. if cfg.cache_dir:
  468. pathlib.Path(cfg.cache_dir).mkdir(parents=True, exist_ok=True)
  469. # Utility function for command line --group argument
  470. def get_llm_config_arg(
  471. llm_config_arg: str, toml_file: str = 'config.toml'
  472. ) -> LLMConfig | None:
  473. """Get a group of llm settings from the config file.
  474. A group in config.toml can look like this:
  475. ```
  476. [llm.gpt-3.5-for-eval]
  477. model = 'gpt-3.5-turbo'
  478. api_key = '...'
  479. temperature = 0.5
  480. num_retries = 10
  481. ...
  482. ```
  483. The user-defined group name, like "gpt-3.5-for-eval", is the argument to this function. The function will load the LLMConfig object
  484. with the settings of this group, from the config file, and set it as the LLMConfig object for the app.
  485. Note that the group must be under "llm" group, or in other words, the group name must start with "llm.".
  486. Args:
  487. llm_config_arg: The group of llm settings to get from the config.toml file.
  488. Returns:
  489. LLMConfig: The LLMConfig object with the settings from the config file.
  490. """
  491. # keep only the name, just in case
  492. llm_config_arg = llm_config_arg.strip('[]')
  493. # truncate the prefix, just in case
  494. if llm_config_arg.startswith('llm.'):
  495. llm_config_arg = llm_config_arg[4:]
  496. logger.opendevin_logger.info(f'Loading llm config from {llm_config_arg}')
  497. # load the toml file
  498. try:
  499. with open(toml_file, 'r', encoding='utf-8') as toml_contents:
  500. toml_config = toml.load(toml_contents)
  501. except FileNotFoundError as e:
  502. logger.opendevin_logger.error(f'Config file not found: {e}')
  503. return None
  504. except toml.TomlDecodeError as e:
  505. logger.opendevin_logger.error(
  506. f'Cannot parse llm group from {llm_config_arg}. Exception: {e}'
  507. )
  508. return None
  509. # update the llm config with the specified section
  510. if 'llm' in toml_config and llm_config_arg in toml_config['llm']:
  511. return LLMConfig(**toml_config['llm'][llm_config_arg])
  512. logger.opendevin_logger.debug(f'Loading from toml failed for {llm_config_arg}')
  513. return None
  514. # Command line arguments
  515. def get_parser() -> argparse.ArgumentParser:
  516. """Get the parser for the command line arguments."""
  517. parser = argparse.ArgumentParser(description='Run an agent with a specific task')
  518. parser.add_argument(
  519. '-d',
  520. '--directory',
  521. type=str,
  522. help='The working directory for the agent',
  523. )
  524. parser.add_argument(
  525. '-t', '--task', type=str, default='', help='The task for the agent to perform'
  526. )
  527. parser.add_argument(
  528. '-f',
  529. '--file',
  530. type=str,
  531. help='Path to a file containing the task. Overrides -t if both are provided.',
  532. )
  533. parser.add_argument(
  534. '-c',
  535. '--agent-cls',
  536. default=_DEFAULT_AGENT,
  537. type=str,
  538. help='Name of the default agent to use',
  539. )
  540. parser.add_argument(
  541. '-i',
  542. '--max-iterations',
  543. default=_MAX_ITERATIONS,
  544. type=int,
  545. help='The maximum number of iterations to run the agent',
  546. )
  547. parser.add_argument(
  548. '-b',
  549. '--max-budget-per-task',
  550. type=float,
  551. help='The maximum budget allowed per task, beyond which the agent will stop.',
  552. )
  553. # --eval configs are for evaluations only
  554. parser.add_argument(
  555. '--eval-output-dir',
  556. default='evaluation/evaluation_outputs/outputs',
  557. type=str,
  558. help='The directory to save evaluation output',
  559. )
  560. parser.add_argument(
  561. '--eval-n-limit',
  562. default=None,
  563. type=int,
  564. help='The number of instances to evaluate',
  565. )
  566. parser.add_argument(
  567. '--eval-num-workers',
  568. default=4,
  569. type=int,
  570. help='The number of workers to use for evaluation',
  571. )
  572. parser.add_argument(
  573. '--eval-note',
  574. default=None,
  575. type=str,
  576. help='The note to add to the evaluation directory',
  577. )
  578. parser.add_argument(
  579. '-l',
  580. '--llm-config',
  581. default=None,
  582. type=str,
  583. help='Replace default LLM ([llm] section in config.toml) config with the specified LLM config, e.g. "llama3" for [llm.llama3] section in config.toml',
  584. )
  585. return parser
  586. def parse_arguments() -> argparse.Namespace:
  587. """Parse the command line arguments."""
  588. parser = get_parser()
  589. parsed_args, _ = parser.parse_known_args()
  590. return parsed_args
  591. def load_app_config(set_logging_levels: bool = True) -> AppConfig:
  592. """Load the configuration from the config.toml file and environment variables.
  593. Args:
  594. set_logger_levels: Whether to set the global variables for logging levels.
  595. """
  596. config = AppConfig()
  597. load_from_toml(config)
  598. load_from_env(config, os.environ)
  599. finalize_config(config)
  600. if set_logging_levels:
  601. logger.DEBUG = config.debug
  602. logger.DISABLE_COLOR_PRINTING = config.disable_color
  603. return config