Use a proxy for the global Config so we can set it programmatically
This commit is contained in:
parent
0e1f827da4
commit
52fbac06e4
2 changed files with 73 additions and 7 deletions
|
|
@ -102,6 +102,34 @@ a2a:
|
||||||
max_contexts: 1000
|
max_contexts: 1000
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Programmatic Configuration
|
||||||
|
|
||||||
|
When using haiku.rag as a Python library, you can configure it programmatically using `set_config()`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from haiku.rag.config import set_config, AppConfig
|
||||||
|
from haiku.rag.client import HaikuRAG
|
||||||
|
|
||||||
|
# Create custom configuration
|
||||||
|
custom_config = AppConfig(
|
||||||
|
qa={"provider": "openai", "model": "gpt-4o"},
|
||||||
|
embeddings={"provider": "ollama", "model": "qwen3-embedding"},
|
||||||
|
processing={"chunk_size": 512}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set the configuration globally
|
||||||
|
set_config(custom_config)
|
||||||
|
|
||||||
|
# All subsequent operations use this configuration
|
||||||
|
client = HaikuRAG(db_path)
|
||||||
|
```
|
||||||
|
|
||||||
|
This is useful for:
|
||||||
|
- Jupyter notebooks
|
||||||
|
- Python scripts
|
||||||
|
- Testing with different configurations
|
||||||
|
- Applications that need runtime configuration
|
||||||
|
|
||||||
## API Keys
|
## API Keys
|
||||||
|
|
||||||
API keys are configured through **environment variables**, not in the YAML file.
|
API keys are configured through **environment variables**, not in the YAML file.
|
||||||
|
|
|
||||||
|
|
@ -40,15 +40,53 @@ __all__ = [
|
||||||
"load_yaml_config",
|
"load_yaml_config",
|
||||||
"generate_default_config",
|
"generate_default_config",
|
||||||
"load_config_from_env",
|
"load_config_from_env",
|
||||||
|
"set_config",
|
||||||
]
|
]
|
||||||
|
|
||||||
# Load config from YAML file or use defaults
|
|
||||||
config_path = find_config_file(None)
|
class ConfigProxy:
|
||||||
if config_path:
|
"""Proxy for the global configuration that allows runtime updates."""
|
||||||
yaml_data = load_yaml_config(config_path)
|
|
||||||
Config = AppConfig.model_validate(yaml_data)
|
def __init__(self):
|
||||||
else:
|
# Load config from YAML file or use defaults
|
||||||
Config = AppConfig()
|
config_path = find_config_file(None)
|
||||||
|
if config_path:
|
||||||
|
yaml_data = load_yaml_config(config_path)
|
||||||
|
self._config = AppConfig.model_validate(yaml_data)
|
||||||
|
else:
|
||||||
|
self._config = AppConfig()
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
"""Proxy attribute access to the underlying config."""
|
||||||
|
return getattr(self._config, name)
|
||||||
|
|
||||||
|
def set(self, config: AppConfig) -> None:
|
||||||
|
"""Replace the current configuration."""
|
||||||
|
self._config = config
|
||||||
|
|
||||||
|
|
||||||
|
# Create the global Config instance
|
||||||
|
Config = ConfigProxy()
|
||||||
|
|
||||||
# Check for deprecated .env file
|
# Check for deprecated .env file
|
||||||
check_for_deprecated_env()
|
check_for_deprecated_env()
|
||||||
|
|
||||||
|
|
||||||
|
def set_config(config: AppConfig) -> None:
|
||||||
|
"""Set the global configuration programmatically.
|
||||||
|
|
||||||
|
This allows library users to configure haiku.rag without needing
|
||||||
|
a YAML file or environment variables.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config: The AppConfig instance to use globally.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> from haiku.rag.config import set_config, AppConfig
|
||||||
|
>>> custom_config = AppConfig(
|
||||||
|
... qa={"provider": "openai", "model": "gpt-4o"},
|
||||||
|
... embeddings={"provider": "voyage", "model": "voyage-3"}
|
||||||
|
... )
|
||||||
|
>>> set_config(custom_config)
|
||||||
|
"""
|
||||||
|
Config.set(config)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue