|
import json |
|
import os |
|
from dotenv import load_dotenv |
|
|
|
|
|
CONFIG_FILE = 'config.json' |
|
|
|
|
|
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. |
|
""" |
|
|
|
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) |
|
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: |
|
|
|
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) |
|
|
|
|
|
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}") |
|
|
|
|
|
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 |