-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
242 lines (196 loc) · 8.75 KB
/
settings.py
File metadata and controls
242 lines (196 loc) · 8.75 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
Configuration settings for Agentic Interview System.
Loads configuration from environment variables (via .env file or system env).
Provides validation and defaults for LLM integration, authentication, and app config.
"""
import os
from typing import Tuple
from dotenv import load_dotenv
from logging_config import get_logger
logger = get_logger(__name__)
# Load .env file if it exists (for local development)
# In production, use system environment variables instead
load_dotenv()
# ==============================================================================
# Application Authentication
# ==============================================================================
# Simple shared password for app access (empty = no auth required)
APP_PASSWORD = os.getenv("APP_PASSWORD", "")
# ==============================================================================
# Organization Configuration
# ==============================================================================
# Default organization ID to use
try:
DEFAULT_ORG_ID = int(os.getenv("DEFAULT_ORG_ID", "1"))
except (ValueError, TypeError):
DEFAULT_ORG_ID = 1
logger.warning("Invalid DEFAULT_ORG_ID, using default 1")
# ==============================================================================
# LLM Provider Configuration
# ==============================================================================
LLM_PROVIDER = os.getenv("LLM_PROVIDER", "openai")
# ==============================================================================
# API Keys
# ==============================================================================
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY", "")
# ==============================================================================
# Model Selection
# ==============================================================================
# Default models for each provider
DEFAULT_OPENAI_MODEL = "gpt-4"
DEFAULT_ANTHROPIC_MODEL = "claude-3-5-sonnet-20241022"
# Get model from env or use provider-specific default
LLM_MODEL = os.getenv("LLM_MODEL")
if not LLM_MODEL:
if LLM_PROVIDER == "openai":
LLM_MODEL = DEFAULT_OPENAI_MODEL
elif LLM_PROVIDER == "anthropic":
LLM_MODEL = DEFAULT_ANTHROPIC_MODEL
else:
LLM_MODEL = DEFAULT_OPENAI_MODEL # Fallback
# ==============================================================================
# Generation Parameters
# ==============================================================================
# Temperature: 0.0-1.0 (lower = more deterministic)
try:
LLM_TEMPERATURE = float(os.getenv("LLM_TEMPERATURE", "0.3"))
# Clamp to valid range
LLM_TEMPERATURE = max(0.0, min(1.0, LLM_TEMPERATURE))
except (ValueError, TypeError):
LLM_TEMPERATURE = 0.3
logger.warning("Invalid LLM_TEMPERATURE, using default 0.3")
# API timeout in seconds
try:
LLM_TIMEOUT = int(os.getenv("LLM_TIMEOUT", "30"))
except (ValueError, TypeError):
LLM_TIMEOUT = 30
logger.warning("Invalid LLM_TIMEOUT, using default 30")
# Max retry attempts for transient failures
try:
LLM_MAX_RETRIES = int(os.getenv("LLM_MAX_RETRIES", "2"))
except (ValueError, TypeError):
LLM_MAX_RETRIES = 2
logger.warning("Invalid LLM_MAX_RETRIES, using default 2")
# ==============================================================================
# Rate Limiting Configuration
# ==============================================================================
# Maximum LLM calls per session (prevents runaway costs)
try:
LLM_MAX_CALLS_PER_SESSION = int(os.getenv("LLM_MAX_CALLS_PER_SESSION", "50"))
except (ValueError, TypeError):
LLM_MAX_CALLS_PER_SESSION = 50
logger.warning("Invalid LLM_MAX_CALLS_PER_SESSION, using default 50")
# Maximum LLM calls per minute (rate limiting)
try:
LLM_MAX_CALLS_PER_MINUTE = int(os.getenv("LLM_MAX_CALLS_PER_MINUTE", "10"))
except (ValueError, TypeError):
LLM_MAX_CALLS_PER_MINUTE = 10
logger.warning("Invalid LLM_MAX_CALLS_PER_MINUTE, using default 10")
# ==============================================================================
# DSPy Configuration (Phase 8)
# ==============================================================================
# Default evaluator type: "heuristic", "llm", or "dspy"
EVALUATOR_TYPE = os.getenv("EVALUATOR_TYPE", "heuristic")
# DSPy model (defaults to cost-effective gpt-4o-mini)
DSPY_MODEL = os.getenv("DSPY_MODEL", "gpt-4o-mini")
# Path to compiled DSPy module (optimized prompts)
DSPY_COMPILED_PATH = os.getenv("DSPY_COMPILED_PATH", "compiled_evaluator.json")
# DSPy temperature (0.0 for deterministic evaluation)
try:
DSPY_TEMPERATURE = float(os.getenv("DSPY_TEMPERATURE", "0.0"))
DSPY_TEMPERATURE = max(0.0, min(1.0, DSPY_TEMPERATURE))
except (ValueError, TypeError):
DSPY_TEMPERATURE = 0.0
logger.warning("Invalid DSPY_TEMPERATURE, using default 0.0")
# ==============================================================================
# Validation Functions
# ==============================================================================
def validate_llm_config() -> Tuple[bool, str]:
"""
Validate LLM configuration.
Returns:
Tuple of (is_valid, error_message)
If valid: (True, "")
If invalid: (False, "Error description")
"""
# Check provider is valid
valid_providers = ["openai", "anthropic", "mock"]
if LLM_PROVIDER not in valid_providers:
return False, f"Invalid LLM_PROVIDER '{LLM_PROVIDER}'. Must be one of: {valid_providers}"
# Check API key is configured for the selected provider
if LLM_PROVIDER == "openai":
if not OPENAI_API_KEY or OPENAI_API_KEY == "sk-your-openai-key-here":
return False, "OpenAI API key not configured. Set OPENAI_API_KEY in .env file."
elif LLM_PROVIDER == "anthropic":
if not ANTHROPIC_API_KEY or ANTHROPIC_API_KEY == "sk-ant-your-anthropic-key-here":
return False, "Anthropic API key not configured. Set ANTHROPIC_API_KEY in .env file."
# "mock" provider doesn't need API key
# All checks passed
return True, ""
def validate_dspy_config() -> Tuple[bool, str]:
"""
Validate DSPy configuration.
Returns:
Tuple of (is_valid, error_message)
If valid: (True, "")
If invalid: (False, "Error description")
"""
# Check that an API key is available
if not OPENAI_API_KEY and not ANTHROPIC_API_KEY:
return False, "No API key configured. Set OPENAI_API_KEY or ANTHROPIC_API_KEY."
# Check model is specified
if not DSPY_MODEL:
return False, "DSPY_MODEL not configured."
# Compiled path is optional (DSPy works without it, just unoptimized)
return True, ""
def get_api_key_for_provider(provider: str) -> str:
"""
Get the API key for the specified provider.
Args:
provider: "openai" or "anthropic"
Returns:
API key string
Raises:
ValueError: If provider is unknown or API key not configured
"""
if provider == "openai":
if not OPENAI_API_KEY or OPENAI_API_KEY == "sk-your-openai-key-here":
raise ValueError("OpenAI API key not configured")
return OPENAI_API_KEY
elif provider == "anthropic":
if not ANTHROPIC_API_KEY or ANTHROPIC_API_KEY == "sk-ant-your-anthropic-key-here":
raise ValueError("Anthropic API key not configured")
return ANTHROPIC_API_KEY
elif provider == "mock":
return "mock-key" # Mock doesn't need real key
else:
raise ValueError(f"Unknown provider: {provider}")
def print_config_summary():
"""Print current configuration (without exposing API keys)."""
print("=" * 60)
print("LLM Configuration Summary")
print("=" * 60)
print(f"Provider: {LLM_PROVIDER}")
print(f"Model: {LLM_MODEL}")
print(f"Temperature: {LLM_TEMPERATURE}")
print(f"Timeout: {LLM_TIMEOUT}s")
print(f"Max Retries: {LLM_MAX_RETRIES}")
# Show API key status (not the actual key!)
if LLM_PROVIDER == "openai":
status = "✓ Configured" if OPENAI_API_KEY and OPENAI_API_KEY != "sk-your-openai-key-here" else "✗ Not configured"
print(f"OpenAI API Key: {status}")
elif LLM_PROVIDER == "anthropic":
status = "✓ Configured" if ANTHROPIC_API_KEY and ANTHROPIC_API_KEY != "sk-ant-your-anthropic-key-here" else "✗ Not configured"
print(f"Anthropic API Key: {status}")
is_valid, error = validate_llm_config()
print(f"Configuration Valid: {'✓ Yes' if is_valid else '✗ No'}")
if error:
print(f"Error: {error}")
print("=" * 60)
# ==============================================================================
# Module-level validation (optional, for debugging)
# ==============================================================================
if __name__ == "__main__":
# When run directly, print config summary
print_config_summary()