config_utils.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import json
  2. from pathlib import Path
  3. import sys
  4. # The configuration is expected to be in a 'config' subdirectory relative to this script's location.
  5. CONFIG_DIR = Path(__file__).parent.parent / 'config'
  6. CONFIG_FILE = CONFIG_DIR / 'llm_config.json'
  7. DEFAULT_CONFIG = {
  8. "_comment": "Controls the output of the data extractors. 'shrink_json' (true/false) tokenizes all JSON keys to reduce file size. 'indentation_level' (e.g., 2, 4) controls pretty-printing; use 0 or null for compact, single-line output. `shrink_json` takes precedence on formatting.",
  9. "shrink_json": False,
  10. "indentation_level": 4,
  11. "ignored_folders": []
  12. }
  13. def get_config_path():
  14. """Returns the absolute path to the configuration file."""
  15. return CONFIG_FILE
  16. def load_config():
  17. """
  18. Loads the configuration from llm_config.json.
  19. If the file doesn't exist, it creates a default one.
  20. Returns the configuration dictionary.
  21. """
  22. if not CONFIG_FILE.is_file():
  23. print(f"Configuration file not found at {CONFIG_FILE}. Creating a default one.")
  24. try:
  25. CONFIG_DIR.mkdir(parents=True, exist_ok=True)
  26. with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
  27. json.dump(DEFAULT_CONFIG, f, indent=4)
  28. return DEFAULT_CONFIG
  29. except IOError as e:
  30. print(f"Error: Could not create default configuration file at {CONFIG_FILE}. {e}", file=sys.stderr)
  31. # Return default config to allow the script to proceed if possible
  32. return DEFAULT_CONFIG
  33. try:
  34. with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
  35. # Use a more robust way to load to handle comments if any
  36. config_text = ''.join(line for line in f if not line.strip().startswith('//'))
  37. config = json.loads(config_text)
  38. # Ensure all default keys are present
  39. for key, value in DEFAULT_CONFIG.items():
  40. config.setdefault(key, value)
  41. return config
  42. except (json.JSONDecodeError, IOError) as e:
  43. print(f"Warning: Could not read or parse {CONFIG_FILE}. Using default settings. Error: {e}", file=sys.stderr)
  44. return DEFAULT_CONFIG
  45. if __name__ == '__main__':
  46. # For testing purposes, you can run this script directly
  47. print(f"Config file path: {get_config_path()}")
  48. config = load_config()
  49. print("Loaded configuration:")
  50. print(json.dumps(config, indent=4))