File size: 2,069 Bytes
5debd08 c0dd537 5debd08 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import json
import os
from dotenv import load_dotenv
# Constants
CONFIG_FILE = 'config.json'
# Load environment variables from .env
load_dotenv()
def save_config(data_path, persist_directory, collection_name):
"""
Save configuration to a JSON file.
This function accepts arguments and writes them to a config.json file.
Sensitive data (e.g., API keys) are not written to the file.
"""
# Default paths
if data_path is None:
data_path = '/tmp/data'
if persist_directory is None:
persist_directory = '/tmp/db'
config = {
'data_path': data_path,
'persist_directory': persist_directory,
'collection_name': collection_name
}
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=4) # Add indent for better readability
print(f"Configuration saved to {CONFIG_FILE}.")
def load_config():
"""
Load configuration from JSON file and environment variables.
Returns the complete configuration as a dictionary.
"""
try:
# Load JSON config file if it exists
if not os.path.exists(CONFIG_FILE):
raise FileNotFoundError(f"{CONFIG_FILE} not found. Please save the configuration first.")
with open(CONFIG_FILE, 'r') as f:
config = json.load(f)
# Validate required keys in config.json
required_keys = ['data_path', 'persist_directory', 'collection_name']
for key in required_keys:
if key not in config:
raise KeyError(f"Missing required configuration key: {key}")
# Add GROQ_API_KEY from environment variables (fallback to .env)
config['groq_api_key'] = os.getenv('GROQ_API_KEY')
if not config['groq_api_key']:
raise ValueError("GROQ_API_KEY is not set in environment variables.")
return config
except FileNotFoundError as e:
print(f"Error: {e}")
return None
except (json.JSONDecodeError, KeyError, ValueError) as e:
print(f"Configuration error: {e}")
return None |