agent-task-executor/config/config_loader.py
zhukang dd797ab5e4 feat: initial commit of agent task executor framework
- Add core task execution framework
- Add LLM integration with DeepSeek
- Add text analysis task implementation
- Add configuration management
- Add tests and documentation
2025-01-14 20:53:09 +08:00

68 lines
1.9 KiB
Python

import os
from typing import Dict, Any
import yaml
from pathlib import Path
from dotenv import load_dotenv
from .secure_config import SecureConfig
def get_api_settings(config: Dict[str, Any], secure_config: Dict[str, Any]) -> Dict[str, str]:
"""Get API settings from config and environment variables."""
llm_config = config["llm"]
provider = llm_config["provider"]
# Get API key with priority:
# 1. Environment variables
# 2. Secure config
# 3. Regular config
api_key = (
os.getenv("LLM_API_KEY") or
os.getenv("OPENAI_API_KEY") or
secure_config.get("api_key") or
llm_config["api"].get("api_key")
)
# Get API base URL with fallback chain:
# 1. Environment variable
# 2. Config api.api_base
# 3. Provider-specific default
api_base = (
os.getenv("LLM_API_BASE") or
llm_config["api"].get("api_base") or
llm_config.get(provider, {}).get("api_base")
)
return {
"api_key": api_key,
"api_base": api_base
}
def save_api_key(api_key: str):
"""Securely save API key."""
secure = SecureConfig()
secure_path = Path(os.getcwd()) / "config" / "secure.json"
secure.save_secure_config({"api_key": api_key}, secure_path)
def load_config() -> Dict[str, Any]:
"""Load configuration from YAML file and secure storage."""
load_dotenv()
config_dir = Path(os.getcwd()) / "config"
# Load main config
with open(config_dir / "llm_config.yaml", "r") as f:
config = yaml.safe_load(f)
# Load secure config
secure = SecureConfig()
secure_config = secure.load_secure_config(config_dir / "secure.json")
# Get API settings
api_settings = get_api_settings(config, secure_config)
# Update config with API settings
llm_config = config["llm"]
llm_config["api_key"] = api_settings["api_key"]
llm_config["api_base"] = api_settings["api_base"]
return config