File size: 8,881 Bytes
e0aa230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""

Configuration Manager Module



This module handles loading and managing configuration settings

for the RAG AI system.

"""

import os
import yaml
import logging
from typing import Dict, Any, Optional
from pathlib import Path


class ConfigManager:
    """

    Manages configuration settings for the RAG AI system.



    Features:

    - YAML configuration file loading

    - Environment variable override

    - Default configuration values

    - Configuration validation

    """

    def __init__(self, config_path: Optional[str] = None):
        """

        Initialize the ConfigManager.



        Args:

            config_path: Path to the configuration file (defaults to config/config.yaml)

        """
        self.logger = logging.getLogger(__name__)

        # Set default config path
        if config_path is None:
            config_path = os.path.join(
                os.path.dirname(__file__), "..", "..", "config", "config.yaml"
            )

        self.config_path = Path(config_path)
        self.config = self._load_config()

    def _load_config(self) -> Dict[str, Any]:
        """

        Load configuration from file and environment variables.



        Returns:

            Configuration dictionary

        """
        # Start with default configuration
        config = self._get_default_config()

        # Load from YAML file if it exists
        if self.config_path.exists():
            try:
                with open(self.config_path, "r", encoding="utf-8") as f:
                    file_config = yaml.safe_load(f) or {}
                    config = self._merge_configs(config, file_config)
                self.logger.info(f"Loaded configuration from {self.config_path}")
            except Exception as e:
                self.logger.warning(
                    f"Failed to load config file {self.config_path}: {str(e)}"
                )
        else:
            self.logger.warning(f"Config file not found: {self.config_path}")

        # Override with environment variables
        config = self._apply_env_overrides(config)

        # Validate configuration
        self._validate_config(config)

        return config

    def _get_default_config(self) -> Dict[str, Any]:
        """

        Get default configuration values.



        Returns:

            Default configuration dictionary

        """
        return {
            "api_keys": {
                "gemini_api_key": "",
                "pinecone_api_key": "",
                "openai_api_key": "",
            },
            "vector_db": {
                "provider": "pinecone",
                "index_name": "rag-ai-index",
                "dimension": 3072,  # ✅ Fixed: Match Gemini embedding dimension
                "metric": "cosine",
                "environment": "us-west1-gcp",
            },
            "embedding": {
                "model": "gemini-embedding-exp-03-07",
                "batch_size": 5,
                "max_retries": 3,
                "retry_delay": 1,
            },
            "document_processing": {
                "chunk_size": 1000,
                "chunk_overlap": 200,
                "min_chunk_size": 100,
                "max_file_size_mb": 50,
            },
            "url_processing": {
                "max_depth": 1,
                "follow_links": True,
                "max_pages": 10,
                "timeout": 10,
            },
            "rag": {
                "top_k": 5,
                "similarity_threshold": 0.7,
                "max_context_length": 4000,
                "model": "gpt-3.5-turbo",
                "max_tokens": 500,
                "temperature": 0.7,
            },
            "ui": {
                "title": "AI Embedded Knowledge Agent",
                "description": "Upload documents or provide URLs to build your knowledge base, then ask questions!",
                "theme": "default",
                "share": False,
                "port": 7860,
            },
            "logging": {
                "level": "INFO",
                "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
            },
        }

    def _merge_configs(

        self, base: Dict[str, Any], override: Dict[str, Any]

    ) -> Dict[str, Any]:
        """

        Recursively merge two configuration dictionaries.



        Args:

            base: Base configuration dictionary

            override: Override configuration dictionary



        Returns:

            Merged configuration dictionary

        """
        result = base.copy()

        for key, value in override.items():
            if (
                key in result
                and isinstance(result[key], dict)
                and isinstance(value, dict)
            ):
                result[key] = self._merge_configs(result[key], value)
            else:
                result[key] = value

        return result

    def _apply_env_overrides(self, config: Dict[str, Any]) -> Dict[str, Any]:
        """

        Apply environment variable overrides to configuration.



        Args:

            config: Configuration dictionary



        Returns:

            Configuration with environment overrides applied

        """
        # API Keys
        if os.environ.get("GEMINI_API_KEY"):
            config["api_keys"]["gemini_api_key"] = os.environ["GEMINI_API_KEY"]

        if os.environ.get("PINECONE_API_KEY"):
            config["api_keys"]["pinecone_api_key"] = os.environ["PINECONE_API_KEY"]

        if os.environ.get("OPENAI_API_KEY"):
            config["api_keys"]["openai_api_key"] = os.environ["OPENAI_API_KEY"]

        # Pinecone settings
        if os.environ.get("PINECONE_ENVIRONMENT"):
            config["vector_db"]["environment"] = os.environ["PINECONE_ENVIRONMENT"]

        if os.environ.get("PINECONE_INDEX_NAME"):
            config["vector_db"]["index_name"] = os.environ["PINECONE_INDEX_NAME"]

        # UI settings
        if os.environ.get("GRADIO_SHARE"):
            config["ui"]["share"] = os.environ["GRADIO_SHARE"].lower() == "true"

        if os.environ.get("PORT"):
            try:
                config["ui"]["port"] = int(os.environ["PORT"])
            except ValueError:
                self.logger.warning(f"Invalid PORT value: {os.environ['PORT']}")

        return config

    def _validate_config(self, config: Dict[str, Any]) -> None:
        """

        Validate configuration values.



        Args:

            config: Configuration dictionary to validate

        """
        # Check required API keys
        if not config["api_keys"]["gemini_api_key"]:
            self.logger.warning("Gemini API key not configured")

        if not config["api_keys"]["pinecone_api_key"]:
            self.logger.warning("Pinecone API key not configured")

        # Validate numeric values
        if config["document_processing"]["chunk_size"] <= 0:
            raise ValueError("chunk_size must be positive")

        if config["rag"]["top_k"] <= 0:
            raise ValueError("top_k must be positive")

        if not 0 <= config["rag"]["similarity_threshold"] <= 1:
            raise ValueError("similarity_threshold must be between 0 and 1")

    def get(self, key: str, default: Any = None) -> Any:
        """

        Get a configuration value using dot notation.



        Args:

            key: Configuration key (e.g., 'vector_db.index_name')

            default: Default value if key not found



        Returns:

            Configuration value

        """
        keys = key.split(".")
        value = self.config

        try:
            for k in keys:
                value = value[k]
            return value
        except (KeyError, TypeError):
            return default

    def set(self, key: str, value: Any) -> None:
        """

        Set a configuration value using dot notation.



        Args:

            key: Configuration key (e.g., 'vector_db.index_name')

            value: Value to set

        """
        keys = key.split(".")
        config = self.config

        for k in keys[:-1]:
            if k not in config:
                config[k] = {}
            config = config[k]

        config[keys[-1]] = value

    def get_section(self, section: str) -> Dict[str, Any]:
        """

        Get an entire configuration section.



        Args:

            section: Section name



        Returns:

            Configuration section dictionary

        """
        return self.config.get(section, {})

    def reload(self) -> None:
        """Reload configuration from file."""
        self.config = self._load_config()
        self.logger.info("Configuration reloaded")