-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathconfig.py
More file actions
412 lines (350 loc) · 15.1 KB
/
config.py
File metadata and controls
412 lines (350 loc) · 15.1 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import os
import json
import logging
import re
from pathlib import Path
from typing import List, Union, Dict, Any
logger = logging.getLogger(__name__)
from api.openai_client import OpenAIClient
from api.openrouter_client import OpenRouterClient
from api.bedrock_client import BedrockClient
from api.google_embedder_client import GoogleEmbedderClient
from api.azureai_client import AzureAIClient
from api.dashscope_client import DashscopeClient
from adalflow import GoogleGenAIClient, OllamaClient
# Get API keys from environment variables
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
OPENROUTER_API_KEY = os.environ.get('OPENROUTER_API_KEY')
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_SESSION_TOKEN = os.environ.get('AWS_SESSION_TOKEN')
AWS_REGION = os.environ.get('AWS_REGION')
AWS_ROLE_ARN = os.environ.get('AWS_ROLE_ARN')
# Set keys in environment (in case they're needed elsewhere in the code)
if OPENAI_API_KEY:
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
if GOOGLE_API_KEY:
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
if OPENROUTER_API_KEY:
os.environ["OPENROUTER_API_KEY"] = OPENROUTER_API_KEY
if AWS_ACCESS_KEY_ID:
os.environ["AWS_ACCESS_KEY_ID"] = AWS_ACCESS_KEY_ID
if AWS_SECRET_ACCESS_KEY:
os.environ["AWS_SECRET_ACCESS_KEY"] = AWS_SECRET_ACCESS_KEY
if AWS_SESSION_TOKEN:
os.environ["AWS_SESSION_TOKEN"] = AWS_SESSION_TOKEN
if AWS_REGION:
os.environ["AWS_REGION"] = AWS_REGION
if AWS_ROLE_ARN:
os.environ["AWS_ROLE_ARN"] = AWS_ROLE_ARN
# Wiki authentication settings
raw_auth_mode = os.environ.get('DEEPWIKI_AUTH_MODE', 'False')
WIKI_AUTH_MODE = raw_auth_mode.lower() in ['true', '1', 't']
WIKI_AUTH_CODE = os.environ.get('DEEPWIKI_AUTH_CODE', '')
# Embedder settings
EMBEDDER_TYPE = os.environ.get('DEEPWIKI_EMBEDDER_TYPE', 'openai').lower()
# Get configuration directory from environment variable, or use default if not set
CONFIG_DIR = os.environ.get('DEEPWIKI_CONFIG_DIR', None)
# Client class mapping
CLIENT_CLASSES = {
"GoogleGenAIClient": GoogleGenAIClient,
"GoogleEmbedderClient": GoogleEmbedderClient,
"OpenAIClient": OpenAIClient,
"OpenRouterClient": OpenRouterClient,
"OllamaClient": OllamaClient,
"BedrockClient": BedrockClient,
"AzureAIClient": AzureAIClient,
"DashscopeClient": DashscopeClient
}
def replace_env_placeholders(config: Union[Dict[str, Any], List[Any], str, Any]) -> Union[Dict[str, Any], List[Any], str, Any]:
"""
Recursively replace placeholders like "${ENV_VAR}" in string values
within a nested configuration structure (dicts, lists, strings)
with environment variable values. Logs a warning if a placeholder is not found.
"""
pattern = re.compile(r"\$\{([A-Z0-9_]+)\}")
def replacer(match: re.Match[str]) -> str:
env_var_name = match.group(1)
original_placeholder = match.group(0)
env_var_value = os.environ.get(env_var_name)
if env_var_value is None:
logger.warning(
f"Environment variable placeholder '{original_placeholder}' was not found in the environment. "
f"The placeholder string will be used as is."
)
return original_placeholder
return env_var_value
if isinstance(config, dict):
return {k: replace_env_placeholders(v) for k, v in config.items()}
elif isinstance(config, list):
return [replace_env_placeholders(item) for item in config]
elif isinstance(config, str):
return pattern.sub(replacer, config)
else:
# Handles numbers, booleans, None, etc.
return config
# Load JSON configuration file
def load_json_config(filename):
try:
# If environment variable is set, use the directory specified by it
if CONFIG_DIR:
config_path = Path(CONFIG_DIR) / filename
else:
# Otherwise use default directory
config_path = Path(__file__).parent / "config" / filename
logger.info(f"Loading configuration from {config_path}")
if not config_path.exists():
logger.warning(f"Configuration file {config_path} does not exist")
return {}
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
config = replace_env_placeholders(config)
return config
except Exception as e:
logger.error(f"Error loading configuration file {filename}: {str(e)}")
return {}
# Load generator model configuration
def load_generator_config():
generator_config = load_json_config("generator.json")
# Add client classes to each provider
if "providers" in generator_config:
for provider_id, provider_config in generator_config["providers"].items():
# Try to set client class from client_class
if provider_config.get("client_class") in CLIENT_CLASSES:
provider_config["model_client"] = CLIENT_CLASSES[provider_config["client_class"]]
# Fall back to default mapping based on provider_id
elif provider_id in ["google", "openai", "openrouter", "ollama", "bedrock", "azure", "dashscope"]:
default_map = {
"google": GoogleGenAIClient,
"openai": OpenAIClient,
"openrouter": OpenRouterClient,
"ollama": OllamaClient,
"bedrock": BedrockClient,
"azure": AzureAIClient,
"dashscope": DashscopeClient
}
provider_config["model_client"] = default_map[provider_id]
else:
logger.warning(f"Unknown provider or client class: {provider_id}")
return generator_config
# Load embedder configuration
def load_embedder_config():
embedder_config = load_json_config("embedder.json")
# Process client classes
for key in ["embedder", "embedder_ollama", "embedder_google", "embedder_bedrock"]:
if key in embedder_config and "client_class" in embedder_config[key]:
class_name = embedder_config[key]["client_class"]
if class_name in CLIENT_CLASSES:
embedder_config[key]["model_client"] = CLIENT_CLASSES[class_name]
return embedder_config
def get_embedder_config():
"""
Get the current embedder configuration based on DEEPWIKI_EMBEDDER_TYPE.
Returns:
dict: The embedder configuration with model_client resolved
"""
embedder_type = EMBEDDER_TYPE
if embedder_type == 'bedrock' and 'embedder_bedrock' in configs:
return configs.get("embedder_bedrock", {})
elif embedder_type == 'google' and 'embedder_google' in configs:
return configs.get("embedder_google", {})
elif embedder_type == 'ollama' and 'embedder_ollama' in configs:
return configs.get("embedder_ollama", {})
else:
return configs.get("embedder", {})
def is_ollama_embedder():
"""
Check if the current embedder configuration uses OllamaClient.
Returns:
bool: True if using OllamaClient, False otherwise
"""
embedder_config = get_embedder_config()
if not embedder_config:
return False
# Check if model_client is OllamaClient
model_client = embedder_config.get("model_client")
if model_client:
return model_client.__name__ == "OllamaClient"
# Fallback: check client_class string
client_class = embedder_config.get("client_class", "")
return client_class == "OllamaClient"
def is_google_embedder():
"""
Check if the current embedder configuration uses GoogleEmbedderClient.
Returns:
bool: True if using GoogleEmbedderClient, False otherwise
"""
embedder_config = get_embedder_config()
if not embedder_config:
return False
# Check if model_client is GoogleEmbedderClient
model_client = embedder_config.get("model_client")
if model_client:
return model_client.__name__ == "GoogleEmbedderClient"
# Fallback: check client_class string
client_class = embedder_config.get("client_class", "")
return client_class == "GoogleEmbedderClient"
def is_bedrock_embedder():
"""
Check if the current embedder configuration uses BedrockClient.
Returns:
bool: True if using BedrockClient, False otherwise
"""
embedder_config = get_embedder_config()
if not embedder_config:
return False
model_client = embedder_config.get("model_client")
if model_client:
return model_client.__name__ == "BedrockClient"
client_class = embedder_config.get("client_class", "")
return client_class == "BedrockClient"
def get_embedder_type():
"""
Get the current embedder type based on configuration.
Returns:
str: 'bedrock', 'ollama', 'google', or 'openai' (default)
"""
if is_bedrock_embedder():
return 'bedrock'
elif is_ollama_embedder():
return 'ollama'
elif is_google_embedder():
return 'google'
else:
return 'openai'
# Load repository and file filters configuration
def load_repo_config():
return load_json_config("repo.json")
# Load language configuration
def load_lang_config():
default_config = {
"supported_languages": {
"en": "English",
"ja": "Japanese (日本語)",
"zh": "Mandarin Chinese (中文)",
"zh-tw": "Traditional Chinese (繁體中文)",
"es": "Spanish (Español)",
"kr": "Korean (한국어)",
"vi": "Vietnamese (Tiếng Việt)",
"pt-br": "Brazilian Portuguese (Português Brasileiro)",
"fr": "Français (French)",
"ru": "Русский (Russian)"
},
"default": "en"
}
loaded_config = load_json_config("lang.json") # Let load_json_config handle path and loading
if not loaded_config:
return default_config
if "supported_languages" not in loaded_config or "default" not in loaded_config:
logger.warning("Language configuration file 'lang.json' is malformed. Using default language configuration.")
return default_config
return loaded_config
# Default excluded directories and files
DEFAULT_EXCLUDED_DIRS: List[str] = [
# Virtual environments and package managers
"./.venv/", "./venv/", "./env/", "./virtualenv/",
"./node_modules/", "./bower_components/", "./jspm_packages/",
# Version control
"./.git/", "./.svn/", "./.hg/", "./.bzr/",
# Cache and compiled files
"./__pycache__/", "./.pytest_cache/", "./.mypy_cache/", "./.ruff_cache/", "./.coverage/",
# Build and distribution
"./dist/", "./build/", "./out/", "./target/", "./bin/", "./obj/",
# Documentation
"./docs/", "./_docs/", "./site-docs/", "./_site/",
# IDE specific
"./.idea/", "./.vscode/", "./.vs/", "./.eclipse/", "./.settings/",
# Logs and temporary files
"./logs/", "./log/", "./tmp/", "./temp/",
]
DEFAULT_EXCLUDED_FILES: List[str] = [
"yarn.lock", "pnpm-lock.yaml", "npm-shrinkwrap.json", "poetry.lock",
"Pipfile.lock", "requirements.txt.lock", "Cargo.lock", "composer.lock",
".lock", ".DS_Store", "Thumbs.db", "desktop.ini", "*.lnk", ".env",
".env.*", "*.env", "*.cfg", "*.ini", ".flaskenv", ".gitignore",
".gitattributes", ".gitmodules", ".github", ".gitlab-ci.yml",
".prettierrc", ".eslintrc", ".eslintignore", ".stylelintrc",
".editorconfig", ".jshintrc", ".pylintrc", ".flake8", "mypy.ini",
"pyproject.toml", "tsconfig.json", "webpack.config.js", "babel.config.js",
"rollup.config.js", "jest.config.js", "karma.conf.js", "vite.config.js",
"next.config.js", "*.min.js", "*.min.css", "*.bundle.js", "*.bundle.css",
"*.map", "*.gz", "*.zip", "*.tar", "*.tgz", "*.rar", "*.7z", "*.iso",
"*.dmg", "*.img", "*.msix", "*.appx", "*.appxbundle", "*.xap", "*.ipa",
"*.deb", "*.rpm", "*.msi", "*.exe", "*.dll", "*.so", "*.dylib", "*.o",
"*.obj", "*.jar", "*.war", "*.ear", "*.jsm", "*.class", "*.pyc", "*.pyd",
"*.pyo", "__pycache__", "*.a", "*.lib", "*.lo", "*.la", "*.slo", "*.dSYM",
"*.egg", "*.egg-info", "*.dist-info", "*.eggs", "node_modules",
"bower_components", "jspm_packages", "lib-cov", "coverage", "htmlcov",
".nyc_output", ".tox", "dist", "build", "bld", "out", "bin", "target",
"packages/*/dist", "packages/*/build", ".output"
]
# Initialize empty configuration
configs = {}
# Load all configuration files
generator_config = load_generator_config()
embedder_config = load_embedder_config()
repo_config = load_repo_config()
lang_config = load_lang_config()
# Update configuration
if generator_config:
configs["default_provider"] = generator_config.get("default_provider", "google")
configs["providers"] = generator_config.get("providers", {})
# Update embedder configuration
if embedder_config:
for key in ["embedder", "embedder_ollama", "embedder_google", "embedder_bedrock", "retriever", "text_splitter", "code_splitter"]:
if key in embedder_config:
configs[key] = embedder_config[key]
# Update repository configuration
if repo_config:
for key in ["file_filters", "repository"]:
if key in repo_config:
configs[key] = repo_config[key]
# Update language configuration
if lang_config:
configs["lang_config"] = lang_config
def get_model_config(provider="google", model=None):
"""
Get configuration for the specified provider and model
Parameters:
provider (str): Model provider ('google', 'openai', 'openrouter', 'ollama', 'bedrock')
model (str): Model name, or None to use default model
Returns:
dict: Configuration containing model_client, model and other parameters
"""
# Get provider configuration
if "providers" not in configs:
raise ValueError("Provider configuration not loaded")
provider_config = configs["providers"].get(provider)
if not provider_config:
raise ValueError(f"Configuration for provider '{provider}' not found")
model_client = provider_config.get("model_client")
if not model_client:
raise ValueError(f"Model client not specified for provider '{provider}'")
# If model not provided, use default model for the provider
if not model:
model = provider_config.get("default_model")
if not model:
raise ValueError(f"No default model specified for provider '{provider}'")
# Get model parameters (if present)
model_params = {}
if model in provider_config.get("models", {}):
model_params = provider_config["models"][model]
else:
default_model = provider_config.get("default_model")
model_params = provider_config["models"][default_model]
# Prepare base configuration
result = {
"model_client": model_client,
}
# Provider-specific adjustments
if provider == "ollama":
# Ollama uses a slightly different parameter structure
if "options" in model_params:
result["model_kwargs"] = {"model": model, **model_params["options"]}
else:
result["model_kwargs"] = {"model": model}
else:
# Standard structure for other providers
result["model_kwargs"] = {"model": model, **model_params}
return result