| 149 | max_migrate_conversations: int = 500 |
| 150 | |
| 151 | class AppConfig: |
| 152 | config_paths = ["config.yaml", "config_llm.yaml", "config_embedding.yaml", "config_retrieval.yaml", |
| 153 | "config_webserver.yaml", "config_nlweb.yaml", "config_conv_store.yaml", "config_oauth.yaml"] |
| 154 | |
| 155 | def __init__(self): |
| 156 | load_dotenv() |
| 157 | # Set config directory - can be overridden by NLWEB_CONFIG_DIR environment variable |
| 158 | self.config_directory = self._get_config_directory() |
| 159 | self.base_output_directory = self._get_base_output_directory() |
| 160 | self.load_llm_config() |
| 161 | self.load_embedding_config() |
| 162 | self.load_retrieval_config() |
| 163 | self.load_webserver_config() |
| 164 | self.load_nlweb_config() |
| 165 | self.load_sites_config() |
| 166 | self.load_conversation_storage_config() |
| 167 | self.load_oauth_config() |
| 168 | |
| 169 | def _get_config_directory(self) -> str: |
| 170 | """ |
| 171 | Get the configuration directory from environment variable or use default. |
| 172 | Default is NLWeb/config relative to the code directory. |
| 173 | """ |
| 174 | # Check for environment variable first |
| 175 | config_dir = os.getenv('NLWEB_CONFIG_DIR') |
| 176 | if config_dir: |
| 177 | # Expand any environment variables or ~ in the path |
| 178 | config_dir = os.path.expanduser(os.path.expandvars(config_dir)) |
| 179 | if not os.path.exists(config_dir): |
| 180 | print(f"Warning: Configured config directory {config_dir} does not exist. Using default.") |
| 181 | config_dir = None |
| 182 | |
| 183 | if not config_dir: |
| 184 | # Default: go up three levels from core directory to NLWeb, then to config |
| 185 | # core -> python -> code -> NLWeb |
| 186 | current_dir = os.path.dirname(os.path.abspath(__file__)) # core directory |
| 187 | python_dir = os.path.dirname(current_dir) # python directory |
| 188 | code_dir = os.path.dirname(python_dir) # code directory |
| 189 | nlweb_dir = os.path.dirname(code_dir) # NLWeb directory |
| 190 | config_dir = os.path.join(nlweb_dir, 'config') |
| 191 | |
| 192 | return os.path.abspath(config_dir) |
| 193 | |
| 194 | def _get_base_output_directory(self) -> str | None: |
| 195 | """ |
| 196 | Get the base directory for all output files from the environment variable. |
| 197 | Returns None if the environment variable is not set. |
| 198 | """ |
| 199 | base_dir = os.getenv('NLWEB_OUTPUT_DIR') |
| 200 | if base_dir and not os.path.exists(base_dir): |
| 201 | try: |
| 202 | os.makedirs(base_dir, exist_ok=True) |
| 203 | print(f"Created output directory: {base_dir}") |
| 204 | except Exception as e: |
| 205 | print(f"Warning: Failed to create output directory {base_dir}: {e}") |
| 206 | return None |
| 207 | return base_dir |
| 208 | |