moved from app.app_config
Browse files- core/config/config.py +72 -0
core/config/config.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
@dataclass
|
| 8 |
+
class InputConfig:
|
| 9 |
+
model_name: str = 'openai/gpt-3.5-turbo'
|
| 10 |
+
model_api_key: str = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
| 11 |
+
max_tokens: int = 1024
|
| 12 |
+
max_steps: int = 10
|
| 13 |
+
temperature: float = 0.2
|
| 14 |
+
timeout: float = 30.0
|
| 15 |
+
sleeptime: float = 0.0
|
| 16 |
+
force_max_steps: bool = True
|
| 17 |
+
|
| 18 |
+
@classmethod
|
| 19 |
+
def load(cls, env_file, config_file):
|
| 20 |
+
# Load env variables
|
| 21 |
+
load_dotenv(env_file)
|
| 22 |
+
env_dict = {
|
| 23 |
+
'model_name': os.getenv('MODEL_NAME', 'not set'),
|
| 24 |
+
'model_api_key': os.getenv('MODEL_API_KEY', 'not set')
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Load config JSON
|
| 28 |
+
with open(config_file, 'r') as f:
|
| 29 |
+
config_dict = json.load(f)
|
| 30 |
+
|
| 31 |
+
# Combine both
|
| 32 |
+
return cls(
|
| 33 |
+
model_name=env_dict.get('model_name', cls.model_name),
|
| 34 |
+
model_api_key=env_dict.get('model_api_key', cls.model_api_key),
|
| 35 |
+
max_tokens=config_dict.get('max_tokens', cls.max_tokens),
|
| 36 |
+
max_steps=config_dict.get('max_steps', cls.max_steps),
|
| 37 |
+
temperature=config_dict.get('temperature', cls.temperature),
|
| 38 |
+
timeout=config_dict.get('timeout', cls.timeout),
|
| 39 |
+
sleeptime=config_dict.get('sleeptime', cls.sleeptime),
|
| 40 |
+
force_max_steps=config_dict.get('force_max_steps', cls.force_max_steps)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def save(self, env_file, config_file):
|
| 44 |
+
# Read existing env content if it exists
|
| 45 |
+
env_vars = {}
|
| 46 |
+
if os.path.exists(env_file):
|
| 47 |
+
with open(env_file, 'r') as f:
|
| 48 |
+
for line in f:
|
| 49 |
+
if line.strip(): # Ignore empty lines
|
| 50 |
+
key, value = line.strip().split('=', 1)
|
| 51 |
+
env_vars[key] = value
|
| 52 |
+
|
| 53 |
+
# Update the necessary keys
|
| 54 |
+
env_vars['MODEL_API_KEY'] = self.model_api_key
|
| 55 |
+
env_vars['MODEL_NAME'] = self.model_name
|
| 56 |
+
|
| 57 |
+
# Write back to the .env file
|
| 58 |
+
with open(env_file, 'w') as f:
|
| 59 |
+
for key, value in env_vars.items():
|
| 60 |
+
f.write(f'{key}={value}\n')
|
| 61 |
+
|
| 62 |
+
# Save other parameters to input_config.json
|
| 63 |
+
config_dict = {
|
| 64 |
+
'max_tokens': self.max_tokens,
|
| 65 |
+
'max_steps': self.max_steps,
|
| 66 |
+
'temperature': self.temperature,
|
| 67 |
+
'timeout': self.timeout,
|
| 68 |
+
'sleeptime': self.sleeptime,
|
| 69 |
+
'force_max_steps': self.force_max_steps
|
| 70 |
+
}
|
| 71 |
+
with open(config_file, 'w') as f:
|
| 72 |
+
json.dump(config_dict, f, indent=4)
|