config.py 1.6 KB

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