config.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import copy
  2. import os
  3. import toml
  4. from dotenv import load_dotenv
  5. from opendevin.schema import ConfigType
  6. load_dotenv()
  7. DEFAULT_CONFIG: dict = {
  8. ConfigType.LLM_API_KEY: None,
  9. ConfigType.LLM_BASE_URL: None,
  10. ConfigType.WORKSPACE_DIR: os.path.join(os.getcwd(), 'workspace'),
  11. ConfigType.LLM_MODEL: 'gpt-3.5-turbo-1106',
  12. ConfigType.SANDBOX_CONTAINER_IMAGE: 'ghcr.io/opendevin/sandbox',
  13. ConfigType.RUN_AS_DEVIN: 'true',
  14. ConfigType.LLM_EMBEDDING_MODEL: 'local',
  15. ConfigType.LLM_NUM_RETRIES: 6,
  16. ConfigType.LLM_COOLDOWN_TIME: 1,
  17. ConfigType.DIRECTORY_REWRITE: '',
  18. ConfigType.MAX_ITERATIONS: 100,
  19. ConfigType.AGENT: 'MonologueAgent',
  20. }
  21. config_str = ''
  22. if os.path.exists('config.toml'):
  23. with open('config.toml', 'rb') as f:
  24. config_str = f.read().decode('utf-8')
  25. tomlConfig = toml.loads(config_str)
  26. config = DEFAULT_CONFIG.copy()
  27. for k, v in config.items():
  28. if k in os.environ:
  29. config[k] = os.environ[k]
  30. elif k in tomlConfig:
  31. config[k] = tomlConfig[k]
  32. def _get(key: str, default):
  33. value = config.get(key, default)
  34. if not value:
  35. value = os.environ.get(key, default)
  36. return value
  37. def get_or_error(key: str):
  38. """
  39. Get a key from the config, or raise an error if it doesn't exist.
  40. """
  41. value = get_or_none(key)
  42. if not value:
  43. raise KeyError(f"Please set '{key}' in `config.toml` or `.env`.")
  44. return value
  45. def get_or_default(key: str, default):
  46. """
  47. Get a key from the config, or return a default value if it doesn't exist.
  48. """
  49. return _get(key, default)
  50. def get_or_none(key: str):
  51. """
  52. Get a key from the config, or return None if it doesn't exist.
  53. """
  54. return _get(key, None)
  55. def get(key: str):
  56. """
  57. Get a key from the config, please make sure it exists.
  58. """
  59. return config.get(key)
  60. def get_fe_config() -> dict:
  61. """
  62. Get all the configuration values by performing a deep copy.
  63. """
  64. fe_config = copy.deepcopy(config)
  65. del fe_config['LLM_API_KEY']
  66. return fe_config